Make key codes same - java

I need to make it so using Robot.keyPress(keycode) will press the correct key that was pressed on a different computer/keyboard.
Right now, pressing '/' will give me a key code that would press (using robot) ';' on a different computer. I'm using getKeyCode() from KeyEvent to get the keycode.
Literally the only code I'm using is Robot.keyPress() and KeyEvent.getKeyCode().
So how can I make the robot press the right key?

Related

java.awt.Robot kepress not working for JPasswordField when using KeyEvent.VK_COMPOSE key and character combinations

java.awt.Robot kepress not working for JPasswordField when using KeyEvent.VK_COMPOSE key and character combinations
I have a swing application with virtual keyboard and is running in Linux. When I click on a textfield, I open virtual keyboard and user can click keys and is able to input.
In Linux , we need to support some compose key characters.
Eg : Pressing Compose Key and then pressing key 's' twice gives ß
What I do in my virtual keyboard is when user presses Compose key, Using java.awt.Robot.kepress I do the keypress of KeyEvent.VK_COMPOSE. And then when a is pressed I do robot.kepress(83) annd when a is pressed again I do the same thing.
Below is what I do overall.
Robot robot = new Robot();
// On pressing compose Key
robot.keyPress(KeyEvent.VK_COMPOSE);
robot.keyRelease(KeyEvent.VK_COMPOSE);
int c = 83; // Key s
// On First Press
robot.keyPress(c);
robot.keyRelease(c);
//On Second Press
robot.keyPress(c);
robot.keyRelease(c);
The above is working for JTextfield. When I press compose key & then press s twice, text field shows ß. So it's working fine
But the same is not working in JPasswordField. On pressing compose and s once, nothing gets printed in field as expected. But on second press, password field prints the dot. But when I check the value, it's s not ß
What's the reason for it? Is there any limitation in JPasswordField or Robot?

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

Java KeyPress arrow keys triggers numpad keys

When using Robot.keyPress for the KeyEvent VK_UP, the numpad key is triggered rather than the normal arrow key.
I am trying to write a keyboard emulator that can be used for games etc. Testing with TrackMania I notice that it does not trigger the normal key, but the key on the numpad.
How can I trigger the normal keys using keyPress?
Checking the API, it is supposed to be the non-numpad arrow key. Which it isnt.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_UP);
This will generate the numpad up key.
If this cannot be done in Java, is there any language that allows it?

What are `VK_META` and `META_MASK` in the Java Robot class?

What are VK_META and META_MASK in the Java Robot class? Any example that explains what they are or what they do would be very helpful.
Also, VK_META gives me an invalid keycode error on my Windows 7 PC.
VK_META and META_MASK are defined in KeyEvent and InputEvent classes. They both define the META key as a standalone key pressed and as a modifier used pressing another key respectively.
The META key is a key used in old keyboards and now can be emulated using the Windows Key.
Robot class allows to simulate key strokes (pressed, released, typed) and mouse movement, "acting" as a user using the keyboard and mouse. When you use the robot class, you define the sequence of "actions" to simulate, mouse movements and keyboard usage. If you want to simulate the META key pressed or used as a modifier, need to pass the keyCode and these two constants define the keyCode for META typed and modifier.
You can have problems using this key depending some factors, such your keyboard layout, keyboard configuration, operating system...
If you have problem, try avoid using this key except you need to simulate the Windows (PC) or Command (Mac) key.

keycode for ctrl

For multiple selection in a Jtree,I am using multiple selection mode in it.It works.But I want to know when i am making multiple selection exactly in this tree to do this i wrote a very simple keycontroller class that implements KeyListener, but i wanna check whether CTRL is pressed or not to do so i am using this code but it seems to be not working :
kc.getKeyCode() == KeyEvent.CTRL_DOWN_MASK ;
what is the keyCode for ctrl ? Or am i doing something wrong ?
As CTRL is a key mask, there is no character for the CTRL key alone.
However, according to KeyEvent documentation, there is always a vaild key code that is sent when either a key is pressed or released. in that case, it should be KeyEvent.CHAR_UNDEFINED and getModifiersEx() should return true for the CTRL key. Notice that, for it to work, you have to register a KeyListener (specially handle for both keyPressed() and keyReleased()).
The key code for Ctrl is KeyCode.VK_CONTROL. In order to find if Ctrl is held you can do this:
if ((event.getModifiers() & ActionEvent.CTRL_MASK) ==ActionEvent.CTRL_MASK) {
System.out.println("CTRL KEY PRESSED");
}
Which is using the java.awt.event.ActionEventinstead of the java.awt.event.KeyEvent. So the code for Ctrl in ActionEvent is CTRL_MASK.
Hope this helps.

Categories

Resources