I want to generate key events for Special characters like £, €, µ, ½, Ö, Ä etc. I am able to generate keyevents for key which are on my keyboard like 'A,B,c, %, *, ^' etc with following code:
public static void generateKeyEvent(final int c) {
new Thread() {
public void run() {
try {
Robot robot = new Robot();
robot.keyPress(c);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
robot.keyRelease(c);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
In case of normal characters, it is working fine but in case of characters which i mentioned above the code is throwing following exception:
java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.companyname.utils.Abc$1.run(Abc.java:286)
One thing which i noticed during my search for the solution to this problem, as these special characters are not mapped on my keyboard that is why it is throwing this exception.
any idea, how can i do this?
I got answer to this problem.. basically if you want to print symbol like those then you need to "alt" key for typing that.
for example: if you need to type 'é' in notepad you have to type alt+130.
So i did the same, i generated the key event for alt then for numpad 1 then numpad3 and finally numpad0.
How are you passing the keys?
Note that Robot.keyPress expects key code, not character. Take a look at the KeyEvent constants. There is a VK_EURO_SIGN, not sure about the others.
You should be able to get an arbitrary key code by implementing a KeyListener and checking KeyEvent.getKeyCode() when the particular key (combination of keys) is pressed.
Related
I have problem with press a special letter (Turkish etc.) via java robot class. I hava a method to press keys which works as alt+keycode. I cant convert some special letters to current keycode. So how can I solve it. Thanx
For Example:
KeyStroke ks = KeyStroke.getKeyStroke('ö', 0);
System.out.println(ks.getKeyCode());
Output : 246
// So alt+0246='ö'
//but if I convert 'ş' to keycode
//Output is 351 . So alt+351= '_' and alt+0351= '_'
//What is the Correct combination for 'ş'. same for 'Ş', 'ş','Ğ', 'ğ', 'İ', 'ı', 'Ə', 'ə'
KeyPress:
public void altNumpad(int... numpadCodes) {
if (numpadCodes.length == 0) {
return;
}
robot.keyPress(VK_ALT);
for (int NUMPAD_KEY : numpadCodes) {
robot.keyPress(NUMPAD_KEY);
robot.keyRelease(NUMPAD_KEY);
}
robot.keyRelease(VK_ALT);
}
The character numbers are defiunied in the Unicode standard. The are also used in HTML, therefore you can use this table.
Anyway if you see the character in the source code depends on the fact that the editor interprets the file correctly (UTF-8 is preferred).
Second the used editor must have a font installed that contains these characters. Hence if you type alt+0351 and get and '_' this may just be a replacement character indicating that the font misses this character.
And in the end you should tell the Java compiler that the source code is UTF-8 - just to make sure (javac -encoding utf8).
I am not sure why you did
KeyStroke ks = KeyStroke.getKeyStroke('ö', 0);
Because java docs say,
public static KeyStroke getKeyStroke(Character keyChar,
int modifiers)
//Use 0 to specify no modifiers.
you need to pass a modifier other than 0 to the overload.
You should try to pass a modification like,
java.awt.event.InputEvent.ALT_DOWN_MASK
So probably should try,
KeyStroke ks = KeyStroke.getKeyStroke('ö', java.awt.event.InputEvent.ALT_DOWN_MASK);
Java doc as reference: http://docs.oracle.com/javase/7/docs/api/javax/swing/KeyStroke.html#getKeyStroke(char)
If you cannot properly get a output from that then you should consider the fact the character is UTF-8
This might help you in that regard, Java, Using Scanner to input characters as UTF-8, can't print text
I know this is a late answer but here it is how I handle this problem for Turkish QWERTY keyboard
static void writeRobotWrite(Robot robot, String keys) throws InterruptedException {
....
try {
robot.keyPress(keyCode);
robot.delay(20);
robot.keyRelease(keyCode);
robot.delay(20);
}catch (IllegalArgumentException e)
{
pressUnicode(c, robot);
}
}
}
Basically when I got undefined keyCode for Robot I call pressUnicode function which is:
static void pressUnicode(char c, Robot robot)
{
String cantRecognize = ""+c;
StringSelection selection = new StringSelection(cantRecognize);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
Simply I'm just copying and pasting the character. This is working for all undefined characters. :)
The following is code block, where i have been trying to validate jFormatedTextFeild. When a key is typed (any key) code block does seem to execute for the first key typed. But works fine for second key typed ! Please help me :(
private void jFormattedTextField_ByingPriceKeyTyped(KeyEvent evt) {
System.out.println("key typed action ");
String checking = jFormattedTextField_ByingPrice.getText();
Pattern ptrn = Pattern.compile("[A-Z,a-z,&%$##!()*^]");
Matcher match = ptrn.matcher(checking);
if(match.find()){
txtPriceMessage.setVisible(true);
//text field which contains the message does not appears
//for first key typed only it appears when second key is typed.
} else {
txtPriceMessage.setVisible(false);
}
}
Use a DocumentFilter to filter the values going to a text component in real time, that's what it's design for. Take a look at these examples, there's even a PatternFilter for using with regular expressions...
For post validation, use a InputVerifier
I'm trying to use the Robot class in Java and type some text. Unfortunately I'm having problems finding the key codes of the square brackets, this symbol | and this symbol `. I can't find them in the KeyEvent constants. I want to use them, because the text i'm typing is in cyrillic and these symbols represent characters in the alphabet. Thanks in advance.
It's in the JavaDoc for KeyEvent
KeyEvent.VK_OPEN_BRACKET
and
KeyEvent.VK_CLOSE_BRACKET
Edit
From the KeyEvent JavaDoc
This low-level event is generated by a component object (such as a
text field) when a key is pressed, released, or typed.
So on a US 101-key keyboard, the ` and ~ will produce the same keycode, although ~ will have a SHIFT modifier. Also notice that KeyEvent.VK_BACK_SLASH traps the | (pipe) keystroke too.
Try adding the following sample KeyAdapter to your project to see this in action.
new KeyAdapter()
{
public void keyPressed(final KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_BACK_QUOTE)
{
e.toString();
}
if (e.getKeyCode() == KeyEvent.VK_BACK_SLASH)
{
e.toString();
}
if (e.getKeyCode() == KeyEvent.VK_OPEN_BRACKET)
{
e.toString();
}
}
}
The general solution is to call KeyEvent.getExtendedKeyCodeForChar(int c). If the unicode codepoint c has a VK_ constant that will be returned. Otherwise a "unique integer" is returned.
I think that '`' is KeyEvent.VK_BACK_QUOTE ...
I put a KeyListener on a TextField in my swing application to try some functionalities. The goal is to react on every key typed in that TextField. The user should only type in numbers, but how it is, it is possible to enter alphabetical chars too. So additionally I have to check every time after a key is typed, if the whole thing is a number, if so, make something with that number, if not, tell the user there is an error without exit the program. So I want to do something like this:
String enteredNumPlayers = "";
JTextField textfieldNumPlayers = new JTextField();
textfieldNumPlayers.setBounds(/*some values*/);
textfieldNumPlayers.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.DARK_GRAY));
textfieldNumPlayers.setHorizontalAlignment(JTextField.CENTER);
textfieldNumPlayers.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()!=/*Code of the back key*/){
enteredNumPlayers += e.getKeyChar();
System.out.println(e);
}else{
enteredNumPlayers = enteredNumPlayers.substring(0, s1.length()-1);
}
try{
Integer.parseInt(enteredNumPlayers);
// do something with that number
}catch (NumberFormatException err){
new ErrorDialog("Not a number"); // my own method to allude user
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
Now I wanted to look whats the specific Code for the back key by simply System.out.println(e) in the keyTyped(...) method, but following get printed:
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unbekannt keyCode: 0x0,keyChar=Rücktaste,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0] on (...)
why is every key value or code = 0 or unknown? Shouldn't it be the ascii value? Using "Rücktaste" would also be ugly, since on a english working computer this value would be different, isn't it? So how can I cleanly check if the key typed is the back key?
The same thing happens with other characters, except that their keyChar is the right one.
What you are looking for is a DocumentFilter, DocumentListener or a JFormattedTextField. All three of them are a better solution then using a key listener as it also covers drag-and-drop, copy-paste or any other mechanism you can think of to put text into a textfield.
I think that having an error message to show each time the user presses anything but an integer is a bit annoying. Check this sample code that uses a regular expression to match the input and has the advantage of accepting only digits, anything else is just not inserted to the textfield (managed by overriding the insertString method of PlainDocument).
I am facing a problem in implementing Input method for Virtual Keyboard. Currently I am using robot class for sending input to any application from virtual keyboard. But for that I need to create mapping of key-code and unicode, which is not consistent on different keyboard layout, can I directly pass the UNICODE to any application using Input method without worry about mapping between keycode and unicode.
Any useful link or sample code will be useful.
It is a simple Java program which is always on top of any application and work as onscreen keyboard. Using a mouse while you press any button (key) of the keyboard, the corresponding character will be typed in the application running below. This is working perfectly for English Alphabets. I am facing problem while I am doing for unicode.
find the code snippet below
public static void simulateKeyEvent(char key){
try{
AWTKeyStroke awtKS = AWTKeyStroke.getAWTKeyStroke(key);
int key_code = awtKS.getKeyCode();
System.out.println("key = "+key+" keyCode = "+key_code);
robot.keyPress(key_code);
robot.keyRelease(key_code);
}catch(Exception e){
e.printStackTrace();
}
}
How I sovled it:
//on startup: override the SystemEventQueue
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
final OwnEventQueue newEventQueue = new OwnEventQueue();
eventQueue.push(newEventQueue);
//because dispatchEvent is protected
public class OwnEventQueue {
private final static OwnEventQueue instance;
static{
instance = new OwnEventQueue();
}
#Override
public void dispatchEvent(AWTEvent event) {
super.dispatchEvent(event);
}
public static OwnEventQueue getInstance() {
return instance;
}
}
//then onpress of keyboard button
Character character = getCharacter();
int[] events = {KeyEvent.KEY_PRESSED, KeyEvent.KEY_RELEASED, KeyEvent.KEY_TYPED};
for (int i = 0; i < events.length; i++) {
KeyEvent pressKeyEvent = new KeyEvent(focusComponent, events[i], System.currentTimeMillis(), 0, 0, character.charValue());
OwnEventQueue.getInstance().dispatchEvent(pressKeyEvent);
}
robotKeystrokeSender.keyPress(KeyEvent.VK_RIGHT);
robotKeystrokeSender.delay(10);
robotKeystrokeSender.keyRelease(KeyEvent.VK_RIGHT);
Is your virtual keyboard used as a device by your OS ?
Or, in other words, have you tried considering it as a "real" keyboard ?
According to Java hardware abstraction, were your virtual keyboard to be considered as a driver, it should simply work like a real keyboard.
EDIT : according to comment, this is not a virtual device, but a Java application, as a consequence, probleme is different.
According to Javadoc, Robot can send key strokes given as int. To create those key strokes from characters, I would recommand you create them using getKeystroke(char) before to transform them into integer values using getKeycode(). This way, you would have integer values associatesd your unicode characters, whichever they are.
EDIT 2 : once again, a modification ;-)
it seems like getKeyStroke(String) "should" handle unicode characters.