Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soft keyboard without an EditText & detecting key presses

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:

  • Bring up a soft keyboard when the user hits a Search button
  • User presses keys and I transmit the codes to remote device. Don't really need to see anything on screen in an EditText since it will be shown on the remote device
  • User presses the custom Done button on the soft keyboard to close it

Any suggestions?

like image 461
Gregg Reno Avatar asked Nov 23 '10 14:11

Gregg Reno


People also ask

How do I show soft keyboard when Edittext is focused?

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.

How do I hide the soft keyboard on Android after clicking outside Edittext Kotlin?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I get rid of soft keyboard on Android?

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.


1 Answers

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)

like image 172
Marek Szanyi Avatar answered Oct 11 '22 05:10

Marek Szanyi