0
mainNotes.setOnKeyListener(new View.OnKeyListener() {
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if ((keyCode != KeyEvent.KEYCODE_SLASH) && (titleMod = true) && (keyCode != 46) && (keyCode != KeyEvent.KEYCODE_SLASH) && (keyCode != KeyEvent.KEYCODE_ENTER)) {
                            Toast.makeText(MainActivity.this, "not a slash", Toast.LENGTH_SHORT).show();
                            titleEnd += 1;
                            slashCount = 1;
                        } else if ((keyCode == KeyEvent.KEYCODE_DEL) && (titleMod = true)) {
                            Toast.makeText(MainActivity.this, "deleted", Toast.LENGTH_SHORT).show();
                            titleEnd -= 1;}

The code does not recognize that delete is pressed. It works for all other keypresses like slash, other text and so on. It doesn't even register that delete is pressed?

Yudhishthir Singh
  • 2,941
  • 2
  • 23
  • 42
  • What key are you actually pressing when you're testing this code? – MarsAtomic May 31 '20 at 16:21
  • @MarsAtomic The delete key on my PC. Also, I just noticed it doesn't recognize the key presses on the software kyeboard on the emulator...? Maybe that has something to do with it? –  May 31 '20 at 16:28
  • KEYCODE_DEL corresponds to your backspace key. Standard Android keyboards don't actually have a delete key at all, so you need to be pressing backspace. – MarsAtomic May 31 '20 at 16:30
  • @MarsAtomic Got it. The OnKeyListener isn't recognizing any of the key presses from the android keyboard. Do you know why? –  May 31 '20 at 16:34

1 Answers1

0

KeyEvent.KEYCODE_DEL corresponds to your backspace key. Standard Android keyboards don't actually have a delete key at all, so you need to be pressing backspace when testing on emulator via keyboard.

To make sure that soft key events are detected, avoid using the KeyListener interface because it's unlikely to be recognized by a software keyboard or emulator. From the official documentation:

Key presses on soft input methods are not required to trigger the methods in this listener, and are in fact discouraged to do so. The default android keyboard will not trigger these for any key to any application targetting Jelly Bean or later, and will only deliver it for some key presses to applications targetting Ice Cream Sandwich or earlier.

Instead, try using KeyboardView.OnKeyboardActionListener.

Sample code may be found here.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56