How to globally detect a key event

I have an Android device with a barcode reader. So I want to write the code into a Label whenever a barcode is read.

In other words how to capture a key press event (for example) without having a text input focused?

Thanks.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Which device? Usually a device with an integrated barcode scanner should have been tsnown API
  • oscarmtzz22oscarmtzz22 ESMember
    > @AlessandroCaliaro said:
    > Which device? Usually a device with an integrated barcode scanner should have been tsnown API

    It is a CipherLAB RS31.
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    https://www.cipherlab.com/s/2/product-249384/Touch-Mobile-Computer-RS31-Series.html

    Here there is something about Xamarin Binding... you can try to contact them...
  • What you can do, is write this in MainActiviy .cs:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
    static ReaderManager m_RM = null;

        static TextView Scan_Data;
        static String sDataStr = "";
        private IntentFilter filter;
        private DataReceiver myDataReceiver;
        static Spinner spinner;
    
        int Beep_item = -1;
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            base.OnCreate(bundle);
    
            m_RM = ReaderManager.InitInstance(this);
    
            myDataReceiver = new DataReceiver();
    
            filter = new IntentFilter();
            filter.AddAction(GeneralString.IntentREADERSERVICECONNECTED);
            filter.AddAction(GeneralString.IntentSOFTTRIGGERDATA);
            filter.AddAction(GeneralString.IntentPASSTOAPP);
    
            RegisterReceiver(myDataReceiver, filter);
    
    
    
    
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    
        class DataReceiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Android.Content.Intent intent)
            {
                // 如果收到Intent_SOFTTRIGGER_DATA這個字串的intent的話
                if (intent.Action == GeneralString.IntentSOFTTRIGGERDATA || intent.Action == GeneralString.IntentPASSTOAPP)
                {
                    sDataStr = "";
                    // 把intent裡夾帶的資料取出來
                    sDataStr = intent.GetStringExtra(GeneralString.BcReaderData);
    
                    Toast.MakeText(context, sDataStr, ToastLength.Short).Show();
    
                       var mp = new MainPage();
    
                  //  Xamarin.Forms.Device.StartTimer(TimeSpan.FromSeconds(1), () => {
                        Xamarin.Forms.MessagingCenter.Send<MainPage,string>((MainPage)mp, "Hi",sDataStr);
                    //    return true;
                   // });
                    //  Scan_Data.Text = sDataStr;
                }
    
    
            }
        }
    }
    

    and then in MainPage.xaml.cs you have to create this method:

    private void ContentPage_Appearing(object sender1, EventArgs e)
        {
            base.OnAppearing();
            MessagingCenter.Subscribe<MainPage,string>(this, "Hi", (sender,arg) => {
                // do something whenever the "Hi" message is sent
                Device.BeginInvokeOnMainThread(() => {
                    teste.Text = "Received Barcode:" + arg;
                });
            });
        }
    
  • Gentlemen, can I ask where did you got those values?

        GeneralString.IntentREADERSERVICECONNECTED
        GeneralString.IntentSOFTTRIGGERDATA
        GeneralString.IntentPASSTOAPP
    
Sign In or Register to comment.