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");; } };
bobetko Nov 26 '10 at 20:32 2010-11-26 20:32
source share