NeeL
NeeL

Reputation: 720

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 ?

Upvotes: 1

Views: 940

Answers (1)

vmathieu
vmathieu

Reputation: 220

Try to use the dispatchKeyEvent like that :

@Override
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;
} 

Upvotes: 1

Related Questions