NeeL NeeL - 1 year ago 83
Android Question

How to prevent a key to be inserted in an EditText if it comes from a specific keyboard

i'm working with a bluetooth barcode scanner acting like a bluetooth keyboard, i want to handle the keys when my user scan something but i know he can focus any EditText in the activity so i want to detect which keyboard he uses in my KeyEvent like this :

if KeyEvent.getDevice().getName().equals("Datalogic Scanner") //do stuff
else //let the edittext add the letter

and prevent the EditText to be edited and instead store and process the values the scanner send.

I tried returning true in dispatchKeyEvent in my activity,
I tried returning true in onKeyDown in my activity,
I tried returning true in a OnKeyListener on my EditText and overriding OnKeyDown on my EditText class, but nothing works, the text still get inserted in my EditText.

Any idea ?

Answer Source

Try to use the dispatchKeyEvent like that :

public boolean dispatchKeyEvent(KeyEvent event) {
    return scanManager.handleDispatchKeyEvent(event);

And in the scanManager class, you should do something like that :

public boolean handleDispatchKeyEvent(KeyEvent event) {
// handle the event
return true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download