Override delete key on Android? - java

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:
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.

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.

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.

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

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

Related

How to check in java if user is clicking save button unnecessarily and there is nothing to save?

In my piece of code there is one input box so data will be fetched in that box on drop down selection. And if user do not want to insert/update/delete that data and click on save button unnecessarily then i have to show an error message that: "There is nothing to save..You pressed the save button unnecessary."
Currently , i am checking if the data in input box is equals to the database value then show that error but its not working.
if (inputVal1.equals(dbVal1.getValue()) && inputVal2.equals(dbVal2.getValue())) {
addPageError(T_NOTHING_TO_SAVE);
}
Please suggest how to handle this validation in java.
Store the data in a separate variable and simply compare it to the current values in the input box. If you do this, you could even detect if the values were changed, but then changed back to the original value. Apart from this you could also even deactivate the button and compare in an edit-event of the input box if the content has really changed and enable the button accordingly.

Eclipse 4 RCP Disable Part Switch

In my (pure) E4 App I want to force the User to enter something in a Text Field before he can move on.
Currently, if nothing has been entered into the Text Field and the focusLost Event is triggered, I reset focus to the Text Field.
Via a ModifyListener I check, if the entered String equals "" and if yes, a fake tooltip is displayed, telling the User to enter something into the Text Field.
The Problem is, if I have two Parts on a PartStack and the Text Field is on my first Part, the User is still able to trigger a Part switch and work on the other Part without having to enter something into the Text Field on the first Part first.
How is it possible to prohibit the User from switching between these Parts, as long as nothing is entered in the Text Field on the first Part?
I donĀ“t want to hide Part 2, the App should still look the same, the user should just not be able to do anything until something has been entered into the Text Field.
If you don't want to hide Part 2, then disable the widgets in that part instead. And display a message that asks the user to provide the necessary input back in Part 1. If the user does, hide that message and enable your widgets.
For example:
if (!isInputSatisfactory())
{
displayMessage("Invalid input");
myButton.setEnabled(false);
}
else
myButton.setEnabled(true);

Processing/Java - Key pressed/Key released

I'm building an application on Processing using NyARToolkit, but my question is not directly about NyArToolkit but about a key released() method.
The thing is, I show a card and then I can do a few things if I press different keys. I press "X" and it shows one thing, I press "Y" and it shows another thing. The problem is, it shows the info from the last key pressed all the time.
If I change my AR card, it will immediatly show the info from the last key pressed. I would like to do something to release the key, something like: just show while i'm pressing the key, or have an "ESC" to stop showing everything.
I've been reading about the keyreleased() method but I didn't figured it yet out to put it to work.
By the way my method is like this:
if(key == "c") then
else if(key =="d") then...
How about a boolean keeping track wether you've pushed something
private boolean buttonIsPressed = false;
and inside your keyPressed
buttonIsPressed = true;
and inside your keyReleased
buttonIsPressed = false;
This way you can keep track wether it's pressed or not, using a String you can also keep track which button is being held by initializing it in your keyPressed and making it null in your keyReleased.
If I missunderstood your question let me know.
EDIT:
Reading your edit, you're making quite a big mistake there, make sure to use .equals to compare strings so if(key.equals("c"))
Again, your question is not quite clear for me, so if I'm wrong, excuse me.

libGDX scene2d TextFieldListener does not receive DELETE key on Android

I cannot get the android "delete" key to register in my TextField (scene2d ui element in libgdx) listener. Here is my code to define the text field:
nameTextfield = new TextField("", skin);
nameTextfield.setMessageText("Some Text");
uiStage.addActor(nameTextfield);
I tried this listener just to decode the keycode for the DELETE key:
nameTextfield.setTextFieldListener(new TextFieldListener() {
public void keyTyped (TextField textField, char key) {
textField.setText(String.valueOf(Integer.valueOf(key)));
}
});
Although it gives code for almost for all buttons, it doesn't even react on DELETE button.
I tested this on a Nexus 7.
From the TextField.java source it looks like the "DELETE" (and "BACKSPACE", and "TAB" and a couple other keys ) are handled specially by the TextField. These keys are never forwarded to any listener.
The built-in handler should do "the right thing" (trimming characters off the string contents).
Is delete not behaving correctly for your case in some way that led you to try to decode it?
Well the DELETE button should be implemented differently.
I suggest trying to verify if the pressed key is the DELETE button. If it is, you just do textField.getText(), trim the last letter off it, and set the new text with setText.
I'm sure there's a much more elegant way to do this, but it's the only workaround I can think of. After all, DELETE isn't really a char which you can throw inside setText. Is it? :/
LATER EDIT:
Print the key variable inside your listener, put a breakpoint there, and see what value is assigned to it.
Then also print (or check the javadoc) KeyEvent.KEYCODE_DEL (documentation here) to see what value this one takes.
the best way to solve this is the following:
You will need to listen to inputs from another listner, for example the same screen.
MainMenuScreen implements Screen, InputProcessor
then you will need to create a multiplexer to let inputs been listen from both the stage and the listner.
multiplexer = new InputMultiplexer();
then add the two listners:
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Now you will have to simply delete the field from here:
#Override
public boolean keyDown(int keycode) {
Gdx.app.log("Debug:", "keydown : "+keycode);
//DO SMOTHING LIKE
// if(keycode==...) deleteTextField();
return true;
}
Let me know if you have questions about this solution.
Worked great for me.
This issue has been solved by the latest nightly version of libgdx, the issue is known and discussed in the following link:
nexus button

ActionEvent textfield checking

I have cashform with atttributes pin,sendername,receivername,senderphone,amount and another form accountfrom with attributes pin,sendername receivername,senderphone,amount,bankname,account number..
both form have send Command
Now, I want to check whether the textfields are empty when the user clicks send button...
I tried it in this way
if ( ae.getCommand() == send && ae.getSource()==cashpayform){
cashcheck();
}
if ( ae.getCommand() == send && ae.getSource()==accpayform){
acccheck();
}
but its not working can anyone help me
thanx
When a command triggers an event the source of the event is the Command not the button so you can't physically make a distinction between a command triggered from a button press and a command triggered from a menu.
I suggest you use two different commands if you need to make a distinction between the source of the commands, they can have the same name and even ID if you make pointer comparisons.
Don't compare strings using ==, instead use..
send.equals(ae.getCommand())

Categories

Resources