masonyc / Xamarin.KeyboardHelper

Xamarin.Forms keyboard helper
MIT License
99 stars 24 forks source link

Is it possible to *prevent* rather than show and hide keyboard when focused programatically? #11

Closed dinobu closed 5 years ago

dinobu commented 5 years ago

As we use external bar-code scanner to scan into the field, we dont want to show, then hide keyboard when we do Entry.Focus() call in code.

We need this in order to prevent confusing behavior where keyboard keeps popping up then closing for no obvious reason when user is scanning into the Entry field in question.

Is it possible to prevent keyboard from popping up entirely when programmatically focusing on an Entry from code?

masonyc commented 5 years ago

Currently I don't think there is a perfect way to prevent keyboard popping up in Xamarin Forms, but some scanner can prevent the keyboard pop up in their scanner settings.

dinobu commented 5 years ago

Currently I don't think there is a perfect way to prevent keyboard popping up in Xamarin Forms, but some scanner can prevent the keyboard pop up in their scanner settings.

External barcode scanners dont have that functionality and it makes no sense to have scanner guide application behavior. Thanks. Also, I see your library changed the name entirely. I guess for me to use it, I have to first uninstall old XamarinEnableKeyboardEffect and then install new Xamarin.KeyboardHelper? Thanks

masonyc commented 5 years ago

As I said, some scanner models can have that function and I am just giving you some suggestions since you want to do something that is currently not achievable in Xamarin Forms. And yes, you have to uninstall the old one and then install the Xamarin.KeyboardHelper.

thudugala commented 5 years ago

image

dinobu commented 5 years ago

@thudugala sorry but that is just not something that can be even remotely considered. It is not about preventing keyboard entirely but preventing it from showing on programmatic call to Focus()

thudugala commented 5 years ago

@dinobu yes I know, that is why this plugin exist. But I assume, that is why, there is no support from Xamarin.Forms.

dinobu commented 5 years ago

@thudugala So, If I understand correctly, this plugin will automatically focus on Entry that uses effect when that Entry is rendered and since focus shows the keyboard, this plugin will hide it immediately after it is shown. Is that correct?

thudugala commented 5 years ago

@dinobu this plugin will automatically focus on Entry that uses effect and if effects:KeyboardEffect.RequestFocus="True" when that Entry is rendered.

effects:KeyboardEffect.RequestFocus="True" will not show the keyboard if effects:KeyboardEffect.EnableKeyboard="False"

Entry.Focus() will shows the keyboard even if effects:KeyboardEffect.EnableKeyboard="False" this plugin will hide it immediately after it is shown.

if you do not call Entry.Focus() by code, keyboard will not show up.

Then why we have effects:KeyboardEffect.RequestFocus="True" ?

calling Entry.Focus() in page ViewIsAppearing will not focus on the entry. effects:KeyboardEffect.RequestFocus="True" will do that for you.

dinobu commented 5 years ago

@thudugala again, very useful info, thanks. I'd suggest putting that into your Wiki

dinobu commented 5 years ago

@dinobu this plugin will automatically focus on Entry that uses effect and if effects:KeyboardEffect.RequestFocus="True" when that Entry is rendered. Does this means the Entry has to be visible?

effects:KeyboardEffect.RequestFocus="True" will not show the keyboard if effects:KeyboardEffect.EnableKeyboard="False" I am confused with this. You are saying this setting will prevent keyboard from popping up, your colleague is saying "there is no perfect way to prevent keyboard from popping up in Xamarin.forms". Would you mind elaborating on this one and thanks

Entry.Focus() will shows the keyboard even if effects:KeyboardEffect.EnableKeyboard="False" this plugin will hide it immediately after it is shown.

if you do not call Entry.Focus() by code, keyboard will not show up.

Then why we have effects:KeyboardEffect.RequestFocus="True" ?

calling Entry.Focus() in page ViewIsAppearing will not focus on the entry. effects:KeyboardEffect.RequestFocus="True" will do that for you. ViewIsAppearing() is not part of Xamarin.Forms API, what method are you referring to?

masonyc commented 5 years ago

@dinobu effects:KeyboardEffect.RequestFocus="True"is calling focus in native code, so when the view is rendered, it will automatically focus the entry using this property. In our case, we have to use this to require focus when view rendered, if you are using page.xaml.cs to require a focus on appearing, you don't have to use this property.

effects:KeyboardEffect.RequestFocus="True" will not show the keyboard if effects:KeyboardEffect.EnableKeyboard="False"

If you have this setting, when the view rendered, the keyboard will not popup, however, if you call Entry.focus() after, the keyboard will still show and hide.

dinobu commented 5 years ago

@masonyc Thanks for explanation. Here is my biggest challenge with that though.

  1. The Entry is focused once rendered - that is great. But say it gets rendered and it is focused, all is fine but I tap on a button. Now the focus is lost and I have to call Focus() to focus it again
  2. Since no UWP support, I have to call Focus even in OnAppearing. Any plans to add UWP support?

Why is #1 problem? Because I have to call Focus all over the code. If I have 10 buttons, they all have to Focus at the end so I keep focus on that Entry once button command completes in order to be able to scan into it again

Why is #2 problem? For same reason, I still need to call Focus like in #1 but in UWP, I also have to call focus in OnAppearing for example as the entry will not get focused at all on UWP due to lack of support for UWP. But since Focus now shows keyboard, I also have to call my custom HideKeyboard to hide it. This leads to competition btw Focus and HideKeyboard due to call to these methods from many places. For example, my button command executes and I want to focus on Entry to be able to do what I do, so I call Focus > this focuses control > keyboard pops up > I call HideKeyboard which hides it > navigation moves to another view or loads another contentView and then refocuses on Entry > this shows keyboard again > so, I have to hide it again. This grows quickly into competition btw Focus and HideKeyboard where keyboard was hidden at correct place but then down the code execution, some places was called to Focus on Entry again, causing keyboard to pupup.

masonyc commented 5 years ago

@dinob

  1. You can try adding the effect to the new entry you want to focus on and removing the effect from last focused entry when you click the button to see if it can achieve what you want. Not tested just something I think may help your situation.

  2. There is no plan to do it. You are free to fork the project and add the UWP part yourself in the forked project.

wangyankun33 commented 5 years ago

当我们使用外部条形码扫描仪扫描到现场时,我们不想显示,然后在我们Entry.Focus()调用代码时隐藏键盘。

我们需要这样做以防止混淆行为,当用户扫描到相关的Entry字段时,键盘不断弹出然后关闭,没有明显的原因。

当以编程方式关注代码中的条目时,是否可以防止键盘完全弹出?

I also have this requirement. When I enter the page, I get the focus, but I don't need to display the keyboard. Clicking on the Entry again will display it. Can you solve the problem and solve it?

masonyc commented 5 years ago

Xamarin.Forms的Entry实现方法目前不能完全防止键盘弹出 https://github.com/xamarin/Xamarin.Forms/issues/4555

Xamarin.Forms' implementation on Entry can not prevent keyboard popping out completely. Issue link on Xamarin Forms https://github.com/xamarin/Xamarin.Forms/issues/4555