dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22k stars 1.72k forks source link

Disable soft / virtual keyboard automatic popping up on Android devices with physical keyboards #15698

Open FrantisekPregis opened 1 year ago

FrantisekPregis commented 1 year ago

Description

On mobile devices with physical keyboards it would be convenient to have a possibility to disable the soft keyboard. Not hide it when it shows up (this can be done with KeyboardExtensions.HideKeyboardAsync in the MAUI Community Toolkit), but prevent it from popping up. Enterprise applications may have several consecutive entry fields and because the virtual keyboard overlaps them, the user doesn´t have an overview of what has to be inputted. It should be possible to turn this feature on and off when the application is running, so the soft keyboard automatic popping up may be turned on e.g. via a special button, or when the app is in a certain state.

Public API Changes

The best place for this setting would probably be on the Entry element level:

XAML (including binding): Entry x:Name="entry" DisableAutomaticSoftKeyboardPopUp="true"

code behind: entry.DisableAutomaticSoftKeyboardPopUp = true

Intended Use-Case

This could be useful in case of longer forms (typically in enterprise apps, which also more frequently run on devices with physical keyboards). I am working on an app with many forms, which are generated dynamically. The user should immediatelly see all the available fields, so she can e.g. decide whether all required data are available before starting inputting. Because the soft keyboard obstructs the following fields, I have to hide it after each focus, which doesn´t look nice. On the other hand, special characters not available on the physical keyboard may be needed, so there should be a possibility to enable the soft keyboard again. The function entry.ShowKeyboardAsync() from the toolkit should work even if the automatic popping up is disabled, so we have a way how to "force" show the keyboard if needed (e.g. in the Entry_Focused event, when certain conditions are met).

persiabudi28 commented 1 year ago

Yes, if necessary, code can be created to permanently hide the keyboard while the application is running, for example, keyboardvirtual.permanentHide=true, and to open it again when the application is closed or upon a specific command, keyboardvirtual.permanentHide=false.

tjiakwokyung28 commented 1 year ago

Yes, It's like a light switch, turning the virtual keyboard on to display or off to disable the virtual keyboard. Make it into four options keyboard: on, off, hidden or unhidden.

usausa commented 1 year ago

I too need this feature. I am also developing in MAUI for an Android device that also has a physical keyboard.

Also, is it possible to change the default behavior of Entry/EntryHandler to disable the physical keyboard display at this time? I see the following code in the MAUI source, is it possible to customize CommandMapper to disable the keyboard display behavior even until this feature is supported?

public static CommandMapper<IEntry, IEntryHandler> CommandMapper = new(ViewCommandMapper)
{
#if ANDROID
    [nameof(IEntry.Focus)] = MapFocus
#endif
};
internal static new void RemapForControls()
{
    // Adjust the mappings to preserve Controls.Entry legacy behaviors
    EntryHandler.Mapper = ControlsEntryMapper;

#if ANDROID
    EntryHandler.CommandMapper.PrependToMapping(nameof(IEntry.Focus), MapFocus);
#endif
}
FrantisekPregis commented 1 year ago

If anyone knows about a workaround for the time being, please share...

usausa commented 1 year ago

The following code could disable the automatic virtual keyboard display.


public static MauiApp CreateMauiApp()
{
...
    Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoKeyboardEntry", (handler, entry) =>
    {
#if ANDROID
        handler.PlatformView.ShowSoftInputOnFocus = false;
#endif
    });

    return builder.Build();
}
FrantisekPregis commented 1 year ago

Thanks @usausa , this looks promising! The keyboard doesn´t show on focus, which is good, after starting typing on the physical keyboard it pops up, which may be OK, I must try it out in concrete scenarios. After pressing physical Enter the soft keyboard doesn´t hide, which I assume can be done with KeyboardExtensions.HideKeyboardAsync from the toolkit. I will experiment with it further and give some more details later.

FrantisekPregis commented 1 year ago

So combining the workaround from @usausa and KeyboardExtensions.HideKeyboardAsync leads to more elegant behaviour:

Ideally the soft keyboard should not pop up at all, in that case there should be a way how to enable it again. With the workaround combination it can be opened only by triggering it with the physical keyboard.

ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

tjiakwokyung28 commented 1 year ago

The following code could disable the automatic virtual keyboard display.



public static MauiApp CreateMauiApp()

{

...

    Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoKeyboardEntry", (handler, entry) =>

    {

#if ANDROID

        handler.PlatformView.ShowSoftInputOnFocus = false;

#endif

    });

    return builder.Build();

}

Code for iOS?

Quaybe commented 6 months ago

The following code could disable the automatic virtual keyboard display.

public static MauiApp CreateMauiApp()
{
...
    Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoKeyboardEntry", (handler, entry) =>
    {
#if ANDROID
        handler.PlatformView.ShowSoftInputOnFocus = false;
#endif
    });

    return builder.Build();
}

Any idea how to do the same for iOS?

christoroth commented 23 hours ago

The workaround isn't stable enough for me. Moving between fields has a habit of making the soft keyboard reappear.
I've tried hiding it but as already mentioned the flicker on the screen is unacceptable. My app and the devices it runs on are driven mostly by barcode scans so we rarely need to use an actual keyboard.

Even though I've got handler mapper changes in place, I see this in the vs output:

[InputMethodManager] showSoftInput() view=crc6452ffdc5b34af3a0f.MauiAppCompatEditText{163bd64 VFED..CL. .F...... 8,8-1842,142 aid=1073741884} flags=0 reason=SHOW_SOFT_INPUT

Without knowing what I'm really looking at, I've been searching the maui code and I can see calls to showSoftInput that don't check any properties. I was hoping to see the calls check a property and then know what it is I need to set to supress it but nothing of the sort seems to exist.

This case like so many Maui cases is over a year old. People want to make apps on different types of Android devices, that are more than just data entry. The soft keyboard takes up such a large amount of the screen and in our scenarios it is not necessary and it appearing and disappearing frequently (or staying present) ruins the app.

usausa commented 4 hours ago

Please, please, please provide an official method to disable it. The automatic keyboard-controlled behavior is a more detrimental factor to the value of your application than you may think.