2

I'm working on a remote control app and need to detect when a user pressed the <x delete button on an empty text field to send this character over and delete something from a remote machine.

How can I detect that the user pressed <x Delete button on Android keyboard?

I tried to ad on key listener, but that did not seem to work.

final EditText edittext = (EditText) findViewById(R.id.editText);
edittext.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        Log.i("KEYCODE", "" + keyCode);
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
            // Perform action on key press
            return false;
        } else if (edittext.length() == 0) {
            if(keyCode == KeyEvent.KEYCODE_DEL) {
                //perform erase when the text field is empty
            }
        }
        return false;
    }
});
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • BACKSPACE, TAB, ENTER, RETURN, ESC, and DELETE) do not require checking to see if the key is coded; for those keys, you should simply use the key variable directly46 is keyCode fore delete – MaxZoom May 01 '15 at 19:14
  • I just added a button to the screen that is the equivalent of delete. I show/hide it with the keyboard – Alex Stone May 06 '15 at 17:28
  • Cool, would it be possible to see? – MaxZoom May 06 '15 at 18:28
  • I've started this question to handle the implementation of the delete button, an equivalent of a soft keyboard delete: http://stackoverflow.com/questions/30103895/android-implement-delete-button-to-perform-deletion-operations-on-edittext – Alex Stone May 07 '15 at 14:23
  • Did you find the solution for this problem? onKeyListener works for hardware keyboard and for software keyboards works override the InputConnection in the editText (new custom InputConnectionWrapper with override deleteSurroundingText method), but I prefer a single solution – Eme Jan 14 '16 at 11:02
  • Possible duplicate of [Android EditText delete(backspace) key event](https://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event) – SadeepDarshana Jun 06 '17 at 08:34

3 Answers3

2

Use the method DispatchKeyEvent :

@Override public boolean dispatchKeyEvent(@NonNull KeyEvent event) { 
    if (event.getAction() == KeyEvent.ACTION_DOWN
        && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
        // ... 
    }
    return super.dispatchKeyEvent(event);
}

(stolen from here)

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
1

I know I am late, but hope this will help someone else. There is no direct way to do this using a listener, but if you look into the documentation of onTextChanged Listener you will find this:

This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before.

So in case of a backspace you will have this condition:

Within s(the editable view), 0 characters beginning at start(depends where the cursor is) have replaced old text that had length before(it's value will be generally 1, unless user first selected some text and then hit backspace)

So if count == 0 you can assume that delete key was pressed.

Ravjit Singh
  • 798
  • 5
  • 17
0

You can't use a View.OnKeyListener for software keyboards.

View.OnKeyListener:

Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

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

ByteHamster
  • 4,884
  • 9
  • 38
  • 53