OnKeyListener does not work on a virtual keyboard

I do not understand why this piece of code does not work. Only return and return keys are detected. The listener does not fire for any other key. My device is the Nexus One.

I tried to override the OnKeyDown method and this is even worse. The only button detected was the equipment return button.

I see around the proposal to use TextWatcher and onTextChanged, although this may work in some cases, this is not a real job. For example, if the text field is empty, you will not find that the user clicks the BackSpace button. So any ideas?

TextView txtInput = (TextView)findViewById(R.id.txtInput); txtInput.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { makeToast(keyCode + " key pressed"); return true; } }); 
+23
android onkeydown onkeypress virtual-keyboard
Nov 26 2018-10-11T00:
source share
6 answers

Ok I finally figured out how to do what I want, and I'm not proud of Android for this.

I am writing a server / client application, where on the client I need to open SoftKeyboard and send the keys pressed (characters and DEL key) ... Each time the key is pressed, this character is sent to the server. If DEL is pressed, I send the {BS} sequence to the server.

To do this, I had to implement TextWatcher and onTextChange, which works well, except when EditText is empty and the user presses the DEL key. Since there are no changes to the EditText, it is not possible to detect that the DEL key is pressed.

In addition to TextWatcher, I had to implement onKeyListener, which I bound to the EditText control. This onKeyListener ignores all keys on the SoftKeyboard except DEL and RETURN. Do not know why? Maybe a mistake?

Here is my code:

  TextView txtInput = (TextView)findViewById(R.id.txtInput); txtInput.addTextChangedListener(inputTextWatcher); txtInput.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { Log.d(TAG, keyCode + " character(code) to send"); return false; } }); 

and TextWatcher ....

 private TextWatcher inputTextWatcher = new TextWatcher() { public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d(TAG, s.charAt(count-1) + " character to send");; } }; 
+19
Nov 26 '10 at 20:32
source share

You made one mistake here. it should return true if you handled the event. If you want to allow event processing by the next receiver , return false
You always come back to the truth

+4
Nov 26 '10 at 4:35
source share

Note: input name in your editor.

 <EditText android:id="@+id/select_category" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textCapSentences|textAutoCorrect" > edittext.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) { //do something here. return true; } return false; } }); 
+3
Mar 19 '15 at 7:21
source share

Have you tried using a special subclass for this view?

I had a similar problem with EditText. The code below worked:

 private OnKeyListener keyListener = new EditText.OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { EditText et = (EditText) v; Log.i("APPEVENT", "Key hit: "+ event); //... return false; } }; 

But if I used View.OnKeyListener , I only write back space and enter. This could be a similar issue with TextView . Perhaps subclass keyword listeners are sensitive to more events for a given View subclass.

+2
Feb 07 '13 at 13:32
source share

We also had a problem with TextWatcher on the iPhone - if the buffer is empty, the keyboard does not send a del event. We worked on this by preloading the keyboard buffer with 1000 characters. Fortunately, our application hid the edit box behind the keyboard, so 1000 characters are not visible. This is ugly, but it works (unless the user hit 1000 deletions in a row before entering any data!)

0
Jan 05 '11 at 5:23
source share

Link from:

http://developer.android.com/reference/android/view/View.OnKeyListener.html

View.OnKeyListener

Class Overview Defines the interface for the callback that will be called when a hardware key event is sent to this view. The callback will be called before the key event is passed to the view. This is only useful for hardware keyboards; a software input method is not required to run this listener.

It seems OnKeyListener was designed for hardware keys only!

0
Sep 10 '19 at 10:31 on
source share



All Articles