I hope this helps:
- Create a class that implements interface
Android.Views.View.IOnKeyListener
and define a relevant delegate:
#if ANDROID
using Android.Runtime;
using Android.Views;
#endif
namespace MyNamespace;
#if ANDROID
/// <summary>
/// A delegate for a listener function to handle key events.
/// </summary>
/// <param name="v">The view that received the key event.</param>
/// <param name="keyCode">The code of the pressed key.</param>
/// <param name="e">Extra information about the key event.</param>
/// <returns>True if the listener function has consumed the event, false otherwise.</returns>
public delegate bool OnKeyDelegate(Android.Views.View v, [GeneratedEnum] Keycode keyCode, KeyEvent e);
public class KeyListener : Java.Lang.Object, Android.Views.View.IOnKeyListener
{
/// <value>
/// The function that will handle the key events.
/// </value>
private readonly OnKeyDelegate OnKeyDelegate;
/// <summary>
/// Sets the function that will handle the key events.
/// </summary>
/// <param name="onKeyDelegate"></param>
public KeyListener(OnKeyDelegate onKeyDelegate)
{
OnKeyDelegate = onKeyDelegate;
}
/// <summary>
/// Calls the specified listener function to handle the key events.
/// </summary>
/// <param name="v">The view that received the key event.</param>
/// <param name="keyCode">The code of the pressed key.</param>
/// <param name="e">Extra information about the key event.</param>
/// <returns>True if the listener function has consumed the event, false otherwise.</returns>
public bool OnKey(Android.Views.View v, [GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (OnKeyDelegate == null) return false;
return OnKeyDelegate(v, keyCode, e); // true will intercept keyboard clicks
}
}
#endif
- In the page with your barcode entry (named "BarcodeEntry", for example), define this function:
/// <summary>
/// Set controls' key handlers.
/// </summary>
private void SetKeyHandler()
{
#if ANDROID
if ((BarcodeEntry != null) && (BarcodeEntry.Handler != null))
(BarcodeEntry.Handler.PlatformView as AppCompatEditText).SetOnKeyListener(new KeyListener(OnBarcodeEntryKey));
#endif
}
- Define the keyboard handler function for the entry (the example handles only Escape and Down-Arrow keys):
#if ANDROID
/// <summary>
/// A listener function to handle the key events.
/// </summary>
/// <param name="v">The view that received the key event.</param>
/// <param name="keyCode">The code of the pressed key.</param>
/// <param name="e">Extra information about the key event.</param>
/// <returns>True if the listener function has consumed the event, false otherwise.</returns>
private bool OnBarcodeEntryKey(Android.Views.View v, [GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (e.Action == KeyEventActions.Down)
switch (keyCode)
{
case Keycode.DpadDown:
// Your code here
return true;
case Keycode.Escape:
// Your code here
return true;
default:
return false;
}
return false;
}
#endif
- Last (but very important), add a call to SetKeyHandler() function to the Load event of page with barcode entry:
/// <summary>
/// Initializes page on loading.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event arguments.</param>
private void Page_Loaded(object sender, EventArgs e)
{
SetKeyHandlers();
// Your code here
}