38

I need to detect every key press on either a soft or hard keyboard in EditText. 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 I 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. Is there any solution to know all the keypresses (including back, shift, enter... also)?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
jettimadhuChowdary
  • 1,058
  • 1
  • 13
  • 23

4 Answers4

63

If you have an EditText, then you can use the TextWatcher interface. In my code, search_edit is an EditText.

search_edit.addTextChangedListener(new TextWatcher() {          
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {                                   
        //here is your code
        myadapter.getFilter().filter(s);
        listview.setAdapter(myadapter); 
    }                       
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub                          
    }                       
    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub                  
    }
});
0xCursor
  • 2,242
  • 4
  • 15
  • 33
Harshid
  • 5,701
  • 4
  • 37
  • 50
9

Implement this:

 et_code_1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    /* do something */
                }
            return true;
            }
        });

Update:

As you would like to implement a Soft key listener, you can implement TextWatcher. Here is an example: How to use the TextWatcher class in Android?

Shomu
  • 2,734
  • 24
  • 32
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
3

Use addTextChangedListener(TextWatcher watcher) and implement TextWatcher interfaace.

Ron
  • 24,175
  • 8
  • 56
  • 97
0

You can use setOnKeyListener

Work only hardware keys for ex:- backspace key, enter key etc.

binding.edt1.setOnKeyListener { view, i, keyEvent -> 
    true
}
RusArtM
  • 1,116
  • 3
  • 15
  • 22
Vikram
  • 1
  • 2