Trigger Button Click Event on Press Enter Key (Not Entry Completed Event)

tamalausttamalaust Member ✭✭

Hello Everyone,

I am using MC3000 Zebra barcode scnanner . This device runs on Android OS. I wrote an application using Xamarin Form (SCL) and deployed in this device.
I am facing some issues regarding capturing device keyboard event inside application.

As example, say, I am in some page abc.xaml. I have a submit button in this page. Anytime I press enter in the device keyboard , I should trigger submit button.
Now , when I am pressing enter from keyboard , my focus can be in some Entry or page itself (e.g. come to page and did nothing). I dont want to use completed event on entry to capture enter key press. Because user might not need to enter anything. Workflow does not always require user to type anything.

I saw most of the suggetions are related to using completed event with Entry but this will not work if the user just want to submit the page data .

Now, I have tried one bad way (I think) that to put focus on submit button . Unfortunately it is not consistent solution .

Any suggestion?

Appreciate it.

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited July 2018

    You can grab DispatchKeyEvent from the Android MainActivity - then send up a MessageCenter event. The shared layer can then hear that and react as you see fit.
    Here's where I'm listening for the Volume.Up button to be pressed and send out a message.
    Is some mocking code that lies and tells our app that we are moving. It does by raising the MessageCenter message reporting itself as a different class (the KeyboardMonitor class. As far as the shared app layer is concerned the KeyboardMonitor is saying the Up key was pressed. It doesn't know if it came from Android or iOS.

            public override bool DispatchKeyEvent(KeyEvent e)
            {
            #if DEBUG
                if (e.KeyCode == Keycode.VolumeUp)
                {
                    var action = e.Action;
                    if (action == KeyEventActions.Up)
                    {//So we don't trigger on both up and down
                        MessagingCenter.Send(App.KeyboardMonitor, nameof(MagicButton), new MagicButtonPressed{ e.Keycode });
                        return true;//Handled so don't change the volume. Too bad doesn't work as documented.
                    }
                }
            #endif
                return base.DispatchKeyEvent(e);
            }
    
  • tamalausttamalaust Member ✭✭
    edited July 2018

    Hi @ClintStLaurent,

    Thank you very much for your help. It works.

    Thanks again.

Sign In or Register to comment.