Quick background. I've used Xamarin for years for iOS and over last 12 months have been using Forms to developer iOS/Android apps. I haven't developed specific Xamarin Android apps before. I have an app which, although is primarily for Android, is developed in Xamarin Forms. The app is run on an android barcode scanner device which has physical buttons on it. So there are a couple of things I am trying to do which I am struggling with.
Detecting Keys
I want to be able to detect the Scan button WITHOUT being in an input field. I originally started by writing my own PageRenderer and overriding the virtual OnKeyDown method. However, this never gets called.
The only way I have been able to get something to sort of work is by having my own EntryRenderer and overriding the virtual DispatchKeyEvent. I can detect the Scan button and respond accordingly. This works but the focus has to be in the Entry control. I want to be able to press the hardware Scan button and invoke it without being the control being in focus. How might I do this?
Hiding Keyboard
I have an Entry field to store the barcode. The idea being the user can scan a barcode or enter the number from the physical numeric keys (not the soft keyboard). So in this field I want to be able to hide the soft keyboard, so I can see more information on the screen. I can hide the keyboard using the InputMethodManager however the issue I have is where to call it from.
I think I need to have it in my EntryRenderer but I can't find a suitable place to put it. To test it I wrote my DispatchKeyEvent so that when a key was pressed it would hide the keyboard. This showed the keyboard could be hidden but obviously pressing a key to hide it, isn't very good :-)
Ideally I want it called when the control gets focus. Even though there are lots of methods that can be overridden many of these that look like might be suitable just don't ever get called. OnElementChanged and DispatchKeyEvent will fire but DispatchSetActivated, OnFocusChanged do not. OnAttachedToWindow will fire but WindowToken is not available at that point (still null) to pass to the InputMethodManager.HideSoftInputFromWindow method.
Thanks