7

I am working on a barcode reader app for Android and Windows. On Android, I receive a system broadcast for every barcode scanned, but on Windows the typical configuration is a keyboard entry sent from the barcode scanner. So what I am trying to do is capture the KeyDown/KeyPress event so that I can add all characters received into a temporary string and then submit to my app as a "barcode read event" as soon as "Enter" is received.

However, I am unable to find KeyDown/KeyPressed events in any of the controls. Is that possible at all? If so, where do I look? The closest (I think) I have gotten is this description of how to use the App lifecycle events: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/app-lifecycle

Thanks and best regards,

Joerg.

Joerg
  • 177
  • 2
  • 11
  • 4
    Desktop-like keyboard support does not yet exist; this would be part of [open proposal: Global Key Hooks](https://github.com/dotnet/maui/issues/3739). FOR NOW, have you tried creating a page with an [`Entry`](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/entry), clicking on that `Entry`, then using the scanner? If the scanner and bluetooth are set up, Keys should be received by the focused view. Entry's `TextChanged` event may be helpful. Google for similar questions related to `Xamarin.Forms` or `Xamarin.Android`; you can use any technique that worked for those. – ToolmakerSteve May 21 '22 at 02:05
  • Thank you @ToolmakerSteve, I did in fact use an `Entry` as a workaround in the meantime, but I still wondered if there was a way to actually capture the KeyPress anywhere in the app, without depending on the focus item. Your proposal confirms there is not (for the moment), so that answers my question. Thank you! – Joerg May 21 '22 at 14:01
  • Same issue here. I'm trying to capture the P1, P2, or F1 to F12 keys but TextChanged doesn't fire. I'm wondering if there is something available from Zebra DataWedge. – D. Kermott Aug 19 '22 at 17:10

3 Answers3

1

If you are using an Entry you don't need to read all keys, instead you can subscribe to Completed event witch is fired when Enter is readed into the Entry.

So many barcode readers can be configured to insert Enter as last part of barcode readed.

MyBarcodeReader()
{ 
    MyBarCodeEntry.Completed += MyBarCodeEntry_Completed;
}

private void MyBarCodeEntry_Completed(object sender, EventArgs e)
{
   /// Do your JOB
}

EDIT JUL 8 2023:

Unfortunately the Completed event appears to be unpredictable and this was causing false positive reading. So I had to change my approach and decided to add a listener to my Entry binding source and analize if the results ends with an \r. This solve all my false positives.

Something like this

public static readonly BindableProperty FilterProperty =
BindableProperty.Create(nameof(Filter), typeof(string), typeof(QuickSearch), null, propertyChanged: OnFilterChanged);
public string Filter
{
    get { return (string)GetValue(FilterProperty); }
    set { SetValue(FilterProperty, value); }
}
private static async void OnFilterChanged(BindableObject bindable, object oldValue, object newValue)
{
    if (bindable is QuickSearch q)
        await q.ProccessFilter();
}

private void ProccessFilter()
{
    CapturedCode = Filter?.Replace("\r", "") ?? string.Empty;
    Regex regex = new(SearchParms.RegexTemplate);
    if (Filter == null || regex.IsMatch((string)CapturedCode))
    {
        if (Filter != null && Filter.EndsWith("\r"))
            await ProccessCodeCompleted();
        return;
    }
} 

Maybe this is caused by maui bugs. (Maui is a pretty good tool but still so buggy)

Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101
  • Thank you @juan-pablo-gomez. I am aware of this, but the idea was to not have to use an Entry control at all... Sorry for maybe not being explicit enough... – Joerg Dec 16 '22 at 13:25
  • @Joerg Do you have any news with your approach? – Juan Pablo Gomez Dec 16 '22 at 18:30
  • To work around this issue I resorted to adding an `Entry` control visible just on Windows Platforms... – Joerg Dec 19 '22 at 13:46
  • @Joerg Be carefull with COMPLETED event I'm experiencing troubles on some custmer machines making totally impreditcable COMPLETED be fired, see EDIT – Juan Pablo Gomez Jul 09 '23 at 03:35
0

For windows it is better to hook into the native keyboard system, which makes sure you always receive input.

When using an Entry you're never sure that an entry has focus and is capable of receiving input.

A great tool for this is SharpHook.

Andrew D. Bond
  • 902
  • 1
  • 11
  • 11
DeMama
  • 1,134
  • 3
  • 13
  • 32
0

You can obtain the UIElement on which the key pressed events should be detected from Handler.PlatformView. And then hook up the native events.

var handler = mauiView.Handler;
UIElement? nativeView = handler?.PlatformView as UIElement;
if (nativeView != null)
{
    nativeView.KeyDown += this.PlatformView_KeyDown;
    nativeView.KeyUp += this.PlatformView_KeyUp;
    nativeView.PreviewKeyDown += this.PlatformView_PreviewKeyDown;
}

Here, mauiView is the view on which the key pressed events should be detected. Please note that this view should be focused for the key pressed events to be detected.

Navaneeth
  • 102
  • 9