I have a physical barcode scanner and I want to get it's input, i.e the barcode, in the app without having a focused EditText.
I tried adding a KeyListener in my Activity. However, none of its implemented methods (onKeyUp, onKeyDown etc) was called.
Then I added the dispatchKeyEvent, which worked, but is never called as many times as the barcode length. Instead, before the barcode is read, some random button in my view gets focus from the barcode scanner.
String barcode = "";
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
char pressedKey = (char) e.getUnicodeChar();
barcode += pressedKey;
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Toast.makeText(getApplicationContext(), "barcode--->>>" + barcode, Toast.LENGTH_LONG)
.show();
}
return super.dispatchKeyEvent(e);
}
I've seen a few questions out there in SO but none really gave a concrete answer.
EditTextand my only conclusion is that after sending the barcode number one by one it emits anENTER- XeniaSis