How can I detect key presses reliably with a hard or soft keyboard?
My app remotely controls another device over wifi, and I need to detect every key press on either a soft or hard keyboard. I don't really need an EditText because I just need to send the characters one at a time as they are pressed and don't need the final text string.
I have tried using an EditText with OnKeyPress, but ran into the problems here with not getting key presses with soft keyboards. And TextWatcher isn't a good option because I need each key press.
I'll use an EditText if I have to, but would prefer not to. What I really want is to:
Any suggestions?
android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File. edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears.
Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.
Well you could override the [onKeyDown(int keyCode, KeyEvent event)
][1] and(or) [onKeyUp(int keyCode, KeyEvent event)
][2]
methods in the applications activity class, this would allow you to get notification even about such keys as the back key and other hardware keys...
Note: you can get notification about trackball movement and so on...
[1]: http://developer.android.com/reference/android/view/View.html#onKeyDown(int, android.view.KeyEvent) [2]: http://developer.android.com/reference/android/view/View.html#onKeyUp(int, android.view.KeyEvent)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With