16

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(https://i.stack.imgur.com/xNyrZ.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.

Community
  • 1
  • 1
Paul
  • 5,756
  • 6
  • 48
  • 78
  • ooi, have you tried the 'backspace' key? – Blundell Jan 18 '13 at 11:04
  • how do you mean? the backspace/delete key is what I am pressing. I mean backspace when i say delete. – Paul Jan 18 '13 at 11:10
  • Ah right sorry! Was thinking of a full size keyboard configuration where delete and backspace are separate – Blundell Jan 18 '13 at 14:35
  • ah right, no problem. still have no idea what to do, bad times :D – Paul Jan 18 '13 at 14:51
  • Check this thread out (specifically the second response): http://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event/11377462#comment19862035_11377462 – Brandon Jan 21 '13 at 02:14
  • will have a look thanks – Paul Jan 21 '13 at 09:44
  • Looks like you library do not support the delete character. and What does the class that throw the exception do? – Daniel De León Jan 23 '13 at 20:28
  • I only have 2 classes, the exception says that 127 threw an error, which is the delete character. The classes that are complaining are in the library. I know it supports delete because they provide an example. This example connects to a telnet server instead of over serial. If I connect to a telnet server the delete works fine, but if I don't then the little boxes occur, so it looks like connecting to a valid telnet server helps the terminal handle backspace – Paul Jan 24 '13 at 09:47

4 Answers4

4

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.

harmanjd
  • 1,874
  • 19
  • 21
  • I will try what you say thanks, the library I am using is https://github.com/jackpal/Android-Terminal-Emulator This is separate from the usb-to-serial library I use but there is no problem with that library. – Paul Jan 24 '13 at 09:46
  • this is the class that throws the exception I posted above https://github.com/jackpal/Android-Terminal-Emulator/blob/master/libraries/emulatorview/src/jackpal/androidterm/emulatorview/EmulatorView.java – Paul Jan 24 '13 at 09:56
  • A quick glance at the code, it looks like onKeyDown and onKeyUp both return true, which prevents the event from propagating up. You might be able to do something with onKeyDown in your activity, but I'm very fuzzy on my memory of the key event handling flow, so you'll have to research that. Looks like the TermKeyListener is mapping KEYCODE_BACK to "\177" (ascii delete no backspace which is what "\b" is. Doesn't look like there is an easy way to change it. But perhaps you could extend and override? – harmanjd Jan 25 '13 at 23:33
  • I solved this thanks, can just run these commands. // ESC [ D (VT100 cursor left) byte[] cmdLeft = { (byte) 27, (byte) '[', (byte) 'D' }; // ESC [ P (VT100 erase char at cursor) byte[] cmdErase = { (byte) 27, (byte) '[', (byte) 'P' }; – Paul Jan 28 '13 at 09:40
2

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.

sage88
  • 4,104
  • 4
  • 31
  • 41
  • hey that's a good way to store what is in the text field if I start deleting the contents by accident in the process, I'm not sure I'd be getting rid of the last character though. If I hit delete and the terminal is highlighted then i just want the editText data to stay the same,what I want is to send one delete character over serial. The editText already gets one character deleted when I highlight it and backspace. – Paul Jan 21 '13 at 09:50
1

I recommend capturing the full string and send it all at once, when the user presses Send, like a chat program.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
  • I would like that but I don't know how to do it. The library is set up to send a character to `write` class every time it is pressed. The rest of my code does that, the editText, sends one string. I'll look into it. – Paul Jan 24 '13 at 09:48
0

The solution was to move the method that wrote to the screen to another class, then everything worked fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul
  • 5,756
  • 6
  • 48
  • 78