How to detect key presses when nothing has focus.

KarenCateKarenCate USMember ✭✭
edited August 2018 in Xamarin.Forms


I am writing an app for an Android-based scanner. Scanned data is sent to the app as keystrokes (even though the keyboard is not displayed). This works great as long as an entry field has focus. However, it is pretty easy to have nothing focused, so then when the keystrokes come in, they are ignored.

I need a solution for when nothing has focus, so any sort of custom renderer won't help.

Because there are multiple entry fields on the screen, I cannot force focus to an entry field in an Unfocused event, because I can't tell if the control is losing focus because the user tapped on another entry field, or if it's because they've tapped "nowhere", or ... whatever other creative way there is to unfocus to nothing.

I'm using Xamarin Forms, but because this is for a specific Android-based device, I don't need it to work on any other platform.

Any ideas?



  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I would say this describes what I would try.

  • KarenCateKarenCate USMember ✭✭

    Thanks, @AdamMeaney. Sadly it says, "Note: When handling keyboard events with the KeyEvent class and related APIs, you should expect that such keyboard events come only from a hardware keyboard. You should never rely on receiving key events for any key on a soft input method (an on-screen keyboard)."

    I would think that input from the scanner would be considered a "soft input method", as it's not any sort of physical key being pressed.

    Plus... I'm not the most sophisticated of Xamarin programmers. I don't know how I'd access those events from within Xamarin Forms.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I have found that scanner tend to be like hardware keyboards, so I figured it was worth a shot.

    I would give it a try, see if you can get the events in your Android Activity.

    If you can, you are looking to use Xamarin's Dependency Service to inject the results into your own code, or Messaging Center to move the events to the Forms level.

Sign In or Register to comment.