Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve USB Code scanning Gun to obtain scanning content by Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "Android how to achieve USB code scanning gun to obtain scanning content". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Recently, we have done a project on external code scanning on Android equipment, and here we record the problem of getting content from Android USB code scanning gun.

First of all, I use USB HID code scanning gun, plug and play, as long as there is an EditText with focus on the interface, you can get the scanning content of code scanning gun.

Doesn't it feel very simple, but what I'm talking about here today is to get the content of the scan gun without EditText.

The USB HID code scanner converts the scanned content into a keyboard event, which corresponds to the KeyEvent event in Android, so we just need to put it in our activity

Override the onKeyDown method

@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {checkLetterStatus (event); keyCodeToNum (keyCode); if (keyCode = = KeyEvent.KEYCODE_ENTER & & event.getAction () = = KeyEvent.ACTION_DOWN) {Log.e ("keyboard event", buffer.toString ()); buffer.delete (0, buffer.length ()); return true;} return false;}

As we said above, the code scan gun responds to our keyboard events, so every time the code scan gun scans a character, it is equivalent to pressing the corresponding key on our keyboard, that is, keyCode, so we only need to deal with this keyCode.

Next, I will check whether the case of checkLetterStatus () and keyCodeToNum () convert the corresponding numbers and letters according to the corresponding keycode.

/ / check the shift key private void checkLetterStatus (KeyEvent event) {int keyCode = event.getKeyCode (); if (keyCode = = KeyEvent.KEYCODE_SHIFT_RIGHT | | keyCode = = KeyEvent.KEYCODE_SHIFT_LEFT) {if (event.getAction () = = KeyEvent.ACTION_DOWN) {/ / press the shift key to indicate uppercase mCaps = true } else {/ / release the shift key to indicate that the lowercase mCaps = false;} / / get the corresponding letters and numbers private void keyCodeToNum (int keycode) {if (keycode > = KeyEvent.KEYCODE_A & & keycode = KeyEvent.KEYCODE_0 & & keycode) according to keycode.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 292

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report