Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override delete key on Android?

I mostly fixed the problem with these lines in dispatchKeyEvent:

byte[] cmdLeft = { (byte) 27, (byte) '[', (byte) 'D' };
byte[] cmdErase = { (byte) 27, (byte) '[', (byte) 'P' };


mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
mSession.appendToEmulator(cmdErase, 0, cmdErase.length);

The only problem now is that if I select the editText and hit delete then one character is deleted but two appear to be on screen. so if I write enable and hit delete it will change to enab but what would actually be sent is enabl

I overrode dispatchKeyEvent, and it kind of works. If the editText is selected, the terminal deletes characters over serial now, so that is a good step. However the main problem still exists that if the terminal is selected itself, weird little boxes are written to the screen instead of deleting a character. Well one is written, and if I keep pressing delete it stays at that one box, but next time I type the amount of deletes I pressed comes up as boxes. It's very odd...

It's like it is just overridden for the edittext and not for the terminal.

Weird little boxes in all their glory:

Enter image description here

public boolean dispatchKeyEvent(KeyEvent event) {
    if (event != null && event.getAction() == KeyEvent.ACTION_UP) {

        return false;
    }
    if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {

        try {
            sendOverSerial("\b".getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return super.dispatchKeyEvent(event);
};

I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data. I send data over the connection via a text box below the terminal or by typing in the terminal itself and hitting enter on the keyboard in both cases. It will only ever be a soft keyboard that is used. If I send an incorrect string I am in an unrecoverable state due to not having a delete key implementation. Backspace in my editTxt works fine, I just want it to work when the terminal is highlighted and I am writing in that.

At the moment if I press delete a little odd box character comes up and nothing else happens, I get an exception in the log some times(http://i.imgur.com/wMRaLPX.png). What I want to know is how to I go about changing the delete keys functionality so that when I press it I can send a delete character like this but also retain the ability to delete characters in the edittext box etc:

sendOverSerial("\b".getBytes("UTF-8"))

This sends a correct back space, I just need to incorporate it.

But the soft-keyboard doesn't seem to register key presses? i keep getting a keycode of 0 and only enter will work.

I am currently trying out https://stackoverflow.com/questions/4...62035_11377462, but any other suggestions would be great, as about 10 suggestions haven't worked so far. My backspace wouldn't be associated with an editText, but a terminal View. I can't even detect the delete key being pressed.

like image 332
Paul Avatar asked Jan 14 '13 11:01

Paul


People also ask

What can I use instead of delete button?

Every keyboard has a delete/backspace key to delete backward, but if it doesn't have a “delete forward” key ⌦, simply hold the fn (function) key and press the delete key. If preferred, you can also use ⌃ control + D to delete forward.

Is there a Delete key on Android keyboard?

Does any android keyboard has the delete button? My Samsung Galaxy Tab A (2016) (Android) (model SM-T580) Android 8.1 has a “delete” key (right above back space) and also cursor keys that move the cursor w/o deleting anything.

How do I get the delete button on my phone?

Tap the menu button on the top right side of the app and then Settings. Now, go to General settings. Next, go to Archive & Delete Actions. You'll be able to select whether you want to show the archive button, delete button, or both.


2 Answers

It looks like the terminal control you are using must be consuming the KEYCODE_DEL instead of letting it propagate to the window and it must be sending a different char to the remote end instead of \b. So when your edit text is focused your dispatchKeyEvent is handling the press - but you don't see it when the terminal has focus. Have you confirmed that the even handler is firing via debugger when the terminal has focus? You didn't say which library you are using for the terminal, but I'd look at that and see if you can set a key handler or something.

like image 183
harmanjd Avatar answered Oct 26 '22 01:10

harmanjd


I don't have any experience with Android, and I'll also admit I've never tried to implement a delete/backspace key bind. However, if I were trying to do this, and I didn't know a good standard implementation I can think of a workaround that would probably function just fine. Make a key bind to delete with an associated action listener. Make the action listener getText() out of your text field and store it as a String. Substring that string to include everything but the last character. Then use setText() for the text field with the new string. Kind of a manual way of doing it, but it would definitely work.

like image 2
sage88 Avatar answered Oct 26 '22 01:10

sage88