Reputation: 193
Am working on a robot engine which will do/key press of all the keys present in the keyboard to a notepad. i.e by using
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
So on as the possibility is present in KeyEvent which is ascii.
Now if I change the Keyboard layout say to Italy, if I type/press square BracketLeft & BracketRight its should print or type è & *, respectively. These are unicode characters, its not helping. As per the API of KeyEvent it supports ASCII. So Unicode cannot be called.
KeyStroke ks = KeyStroke.getKeyStroke(chars[t], 0);
//Where chars is the array storing unicode characters
System.out.println(ks.getKeyCode());
Above one tried using ALT+ks.getKeyCode(), but this too in vain. This I tried as per the suggestion given in here stackoverflow
1) Now I am thinking is it possible to read as rows & columns the keys from Keyboard in java & I can start playing/working with it ? Is yes, how can I do it ? Which API ?
Or 2) Is there any other way to perform the keyPress Event for Unicode Characters using Java (Robot)?
or 3) any other medium to pefrom keyPress into a notepad for different Keyboard layouts.
All this I am trying on Java 1.6 & Java 1.8.
Looking for your valuable suggestion.
char []c = {'è','é','+','*','ù','§','ò','ç','à','°','.',':','-','_'};
performKeyStroke(c);`
private void performKeyStroke(char []chars){
int charLeng = null == chars ?0:chars.length;
for(int t=0;t<charLeng;t++){
System.out.println("trying for --->"+chars[t]);
KeyStroke ks = KeyStroke.getKeyStroke(chars[t], 0);
System.out.println(ks.getKeyCode());
pressCombinationKeys(String.valueOf(ks.getKeyCode()));
}//End of for loop
}//End of performKeyStroke
private void pressCombinationKeys(String nums){
this.robot.keyPress( KeyEvent.VK_ALT );
// -- have to apply some logic to know what sequence
this.keyPressRelease( KeyEvent.VK_0 );
int numLength = null == nums?0:nums.length();
try{
for(int u=0;u<numLength;u++){
this.keyPressRelease(Integer.valueOf(nums.charAt(u)));
System.out.print(nums.charAt(u));
}//End of for loop
}catch(Exception e){
e.printStackTrace();
}
this.robot.keyRelease( KeyEvent.VK_ALT );
}//End of pressCombinationKeys
Thanks in Advance This is something similar which I asked here
Upvotes: 1
Views: 1864
Reputation: 34638
This question is rather tricky. The problem with the Java robot is that it is an implementation that works with the native underlying toolkit, and the sort of input it expects changes from implementation to implementation.
Testing this a little bit on my machines (a Mac and a Linux Mint), it seems that the Java robot expects the keycode of the US layout, and sends the correct character provided that your keyboard layout is set to the proper language when it runs.
That is, if in the Italian layout, the è key is where the [ key is in the US layout, then you should:
VK_OPEN_BRACKET
keycodeIf there is focus in an editable field, and the robot presses and releases that particular keycode, you should see the letter è
.
For the Italian ò, which is located on the ; on a US layout, send the keycode VK_SEMICOLON
.
This worked for me on my Mac and on my Linux Mint. But here is the issue: it worked on my Linux only if my main English keyboard variant was "English(US)", not "English(Dvorak)" for example. Which is why I warned you about different implementations on different platforms.
Since I have not tested it on Windows, I cannot say whether or not this will work. But you give it a try: switch the layout manually before running the robot, and make it send key codes that are correct for the English/US keyboard.
By the way, only keycodes which are on the primary layout can be used! That is, if you want to send an italian ç, you are supposed to send four keyboard events:
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
Upvotes: 2
Reputation: 239
Through reflection you should be able to get all the constants. This is an untested example:
Field[] fields = KeyEvent.class.getDeclaredFields();
for (Field field : fields) {
if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers)) {
int keyValue = field.getInt(null);
}
}
Now, the keyValue should contain the value of the representing the actual key.
Upvotes: 1