1

I have a circumstance where a bluetooth barcode scanner is acting as a hardware keyboard in an Xamarin Forms application. The barcodes are encoded as such to contain a bit of information that needs to be parsed into different fields.

Unfortunately, some of the data is a tab character. iOS receives this hardware event in my app and once the tab pumps through, the next input field is focused and the barcode data continues to dump into that (the barcode scanner reads the data through "sequentially", as if to simulate you were typing it in).

At any rate; I'm having a heck of a time intercepting the "tab" character in an Entry (UITextView) in Xamarin/iOS.

I've set up an extension on Entry and associated a custom renderer with it (MyRenderer : EntryRenderer). Searching suggests that using Control.ShouldChangeTextInRange should work for character remapping, but the tab event happens on a different level it appears.

How can I stop a hardware keyboard "Tab" character in iOS from focusing onto the next field?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
thinice
  • 700
  • 5
  • 19

1 Answers1

2

You could use KeyCommands to filter the Tab action in your EntryRenderer like this:

public override UIKeyCommand[] KeyCommands
{
    get
    {
        UIKeyCommand newForwardKeyCommand = UIKeyCommand.Create(new NSString("\t"), 0, new ObjCRuntime.Selector("Excute"));
        UIKeyCommand newBackKeyCommand = UIKeyCommand.Create(new NSString("\t"), UIKeyModifierFlags.Shift, new ObjCRuntime.Selector("Excute"));
        UIKeyCommand[] keyCommands = { newForwardKeyCommand, newBackKeyCommand };
        return keyCommands;
    }
}

[Export("Excute")]
private void Excute()
{
    Console.Write("Excute!");
}

Reference:https://stackoverflow.com/a/30876304/5474400

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Kevin Li
  • 2,258
  • 1
  • 9
  • 18
  • I was trying something like this but I couldn't for the life of me figure out how to tie in a selector - where did you learn about the `[Export()]` annotation? – thinice Aug 11 '17 at 13:55
  • I love you. That was exactly what I needed. – thinice Aug 11 '17 at 14:03
  • @thinice, I found a sample [here](https://developer.xamarin.com/api/member/MonoTouch.Foundation.NSObject.PerformSelector/p/MonoTouch.ObjCRuntime.Selector/MonoTouch.Foundation.NSObject/System.Double/#Remarks) before. Glad to hear it solves your issue : )! – Kevin Li Aug 14 '17 at 01:31