Entry completed event not firing on Android with carriage return from barcode scan; works on Windows

70 Views Asked by At

I'm developing a .NET MAUI app where I'm facing a problem: The Entry completed event doesn't trigger when I scan a barcode on Android, although it works on Windows. My barcode scanner is set up to send a carriage return (not an Enter key) after each scan, which should trigger the completed event.

<Entry x:Name="barcodeEntry" Placeholder="Scan barcode here" Completed="OnBarcodeCompleted" />
private void OnBarcodeCompleted(object sender, EventArgs e)
{
    // Handle the barcode
}

This setup works as expected on Windows, but on Android, the completed event does not fire after scanning. I've confirmed the scanner sends a carriage return at the end of each scan and tested on multiple android devices, but the problem remains.

Has anyone else experienced this or can offer some insights into what might be going wrong or how to resolve this issue?

1

There are 1 best solutions below

0
alexgavru On

I'll propose a different approach to achieve what you need and then try to come with some possible explanation for the platform differences.

Alternative solution

You can bind to the Barcode text and process text changes yourself:

View:

 <Entry Placeholder="Scan barcode here" Text="{Binding BarcodeText}" />

ViewModel:

[ObservableProperty]
string _barcodeText = string.Empty;

partial void OnBarcodeTextChanged(string? oldValue, string newValue)
{
    if (newValue.Contains("\r")) // Or any other condition
    {
        //TODO: Execute your code here
    }
}

In this example I'm using MVVM Toolkit.

If you don't want to use MVVM Toolkit, you can use TextChanged event and further process the input:

<Entry Placeholder="Enter Name" Text="{Binding BarcodeText}" TextChanged="InputView_OnTextChanged" />

Why is this happening?

From the docs:

The Completed event is raised when the user has ended input by pressing the return key on the keyboard, or by pressing the Tab key on Windows.

From the source code, this is the Android condition to fire Completed event:

if (actionId == ImeAction.Done || actionId == _currentInputImeFlag || (actionId == ImeAction.ImeNull && e.KeyCode == Keycode.Enter && e.Action == KeyEventActions.Up))

WPF condition:

if (keyEventArgs.Key == Key.Enter)

On WPF, it seems that the '\r' character is associated with Key.Enter. So maybe this is where the difference comes from.