I have a barcode app where I need to disable the keyboard for an entry control.
I found this and it works great. Preventing software keyboard from opening when clicking on an entry field in MAUI?
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoKeyboardEntry", (handler, entry) =>
{
#if ANDROID
handler.PlatformView.ShowSoftInputOnFocus = false;
#endif
})
But I also have to support a manual entry mode.
I tried this but it doesn't work.
MyEntry.ShowKeyboardAsync(CancellationToken.None);
How do I enable the keyboard while the app is running after disabling it on start up using this method?
Edit: So many people have been asking about how to handle this so here is my solution on how to use an Entry to get HID barcode scan input. I have a keep focus checkbox that when checked keeps the focus on the Entry and DISABLES TOTALLY the soft keyboard from displaying for the one Entry control! When the checkbox is unchecked the keyboard is re-enabled for the Entry. Perfect!
A BIG Thanks to Liyun Zhang for the enable/disable keyboard part.
// For HID barcode scanning
// Checking this checkbox will
// Keep the focus on the barcode Entry and
// Also disable the Entry keyboard!
<CheckBox x:Name="cb_KeepFocus" CheckedChanged="OnKeepFocusClicked"/>
// Entry that accepts the HID barcode
<Entry x:Name="tx_ConfirmationNumber"/>
public MainPage()
{
InitializeComponent();
// If the barcode Entry looses focus set it back every 3 seconds
var KeepFocusTimer = Application.Current.Dispatcher.CreateTimer();
KeepFocusTimer.Interval = TimeSpan.FromSeconds(3);
KeepFocusTimer.Tick += (s, e) => OnKeepFocusTimer();
KeepFocusTimer.Start();
}
private void OnKeepFocusTimer()
{
MainThread.BeginInvokeOnMainThread(() =>
{
if (cb_KeepFocus.IsChecked)
// Only set the focus if it isn't already set
if (!tx_ConfirmationNumber.IsFocused)
tx_ConfirmationNumber.Focus();
});
}
// Turns on and off the keep focus and keyboard for the HID Entry
private void OnKeepFocusClicked(object sender, CheckedChangedEventArgs e)
{
if (cb_KeepFocus.IsChecked && (!tx_ConfirmationNumber.IsFocused))
tx_ConfirmationNumber.Focus();
if (cb_KeepFocus.IsChecked)
DisableKeyboard();
else
EnableKeyboard();
}
private void DisableKeyboard()
{
#if ANDROID
(tx_ConfirmationNumber.Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ShowSoftInputOnFocus = false;
#endif
}
private void EnableKeyboard()
{
#if ANDROID
(tx_ConfirmationNumber.Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ShowSoftInputOnFocus = true;
#endif
}
The easiest way to do that is using the following code:
#if ANDROID
( MyEntry.Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ShowSoftInputOnFocus = true;
#endif
And you can also custom the handler to disable or enable it:
CustomHandler.cs:
namespace MauiAppTest.Platforms.Android
{
public class MyHandler : EntryHandler
{
public void DisableKeyboard()
{
PlatformView.ShowSoftInputOnFocus = false;
}
public void EnableKeyboard()
{
PlatformView.ShowSoftInputOnFocus = true;
}
}
}
Use it in the MauiProgram.cs:
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers((handlers) =>
{
#if ANDROID
handlers.AddHandler(typeof(Entry),typeof(Platforms.Android.MyHandler));
#endif
});
Call the method in the code behind:
#if ANDROID
(MyEntry.Handler as Platforms.Android.MyHandler).DisableKeyboard();
#endif
In addition, when you change the property, you need to refocus the entry to show the KeyBoard.