1

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

Community
  • 1
  • 1
Kiran
  • 183
  • 5
  • 19
  • I havn't used robot. But I got the all of the key events in java. I implements the keyListeners. It catches all of the key events from keyboard http://stackoverflow.com/questions/14301775/get-key-combinations – Anonymous Jul 06 '16 at 10:17

2 Answers2

2

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:

  • Switch the layout to Italian
  • Tell the robot to send the VK_OPEN_BRACKET keycode

If 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);
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • Thank you, RealSkeptic. I tried this on Windows Laptop programmatically for these 3 keypress. Selected the notepad layout as Italy ...`this.typeLableName(KeyEvent.VK_OPEN_BRACKET); this.keyPressRelease(KeyEvent.VK_OPEN_BRACKET); Open Bracket ' this.typeLableName(KeyEvent.VK_CLOSE_BRACKET); this.shiftKeyPress(KeyEvent.VK_CLOSE_BRACKET); Close Bracket ^ this.typeLableName(KeyEvent.VK_SEMICOLON); this.shiftKeyPress(KeyEvent.VK_SEMICOLON); Semicolon é'. But when I do manually for open Bracket I get é, for close Bracket * & for SEMICOLON ç – Kiran Apr 20 '15 at 04:10
  • @Kiran, sorry, comments are not really appropriate for code and I can't understand what you wrote. Perhaps you can add this as an edit to your question, and then leave me a comment here saying to look there? Also explain what is `keyPressRelease` and `typeLabelName`. – RealSkeptic Apr 20 '15 at 05:08
  • Sorry @RealSkeptic, ok will share the code snippets in multiple comments where keyPressRelease and typeLabelName are generic methods which: ` protected void typeLableName(int keyCode){ String labelName = KeyEvent.getKeyText(keyCode);; type(labelName+" "); }` – Kiran Apr 20 '15 at 09:08
  • The other method :- `protected void keyPressRelease(int keyCode){ try{ this.robot.keyPress(keyCode); this.robot.keyRelease(keyCode); }catch(`ava.lang.IllegalArgumentException e){ logger.error("Error in typing ",e); }catch(Exception e){ logger.error("Error in typing ",e); } }` – Kiran Apr 20 '15 at 09:09
  • `protected void shiftKeyPress(int keyCode){ this.robot.keyPress(KeyEvent.VK_SHIFT); keyPressRelease(keyCode) this.robot.keyRelease(KeyEvent.VK_SHIFT); }` – Kiran Apr 20 '15 at 09:10
  • `this.typeLableName(KeyEvent.VK_OPEN_BRACKET); this.keyPressRelease(KeyEvent.VK_OPEN_BRACKET); this.keyPressRelease(KeyEvent.VK_ENTER); this.typeLableName(KeyEvent.VK_CLOSE_BRACKET); this.keyPressRelease(KeyEvent.VK_CLOSE_BRACKET); this.keyPressRelease(KeyEvent.VK_ENTER); this.typeLableName(KeyEvent.VK_OPEN_BRACKET); this.shiftKeyPress(KeyEvent.VK_OPEN_BRACKET); this.keyPressRelease(KeyEvent.VK_ENTER); this.typeLableName(KeyEvent.VK_CLOSE_BRACKET); this.shiftKeyPress(KeyEvent.VK_CLOSE_BRACKET); this.keyPressRelease(KeyEvent.VK_ENTER);` – Kiran Apr 20 '15 at 09:11
  • I tried to put the code in single comment, but since there was limitation in size . I have added multiple comments. As you said I tried doing key press & release using Robot I got output as ' for open bracket, but if I press the same Key manually I get the output as è – Kiran Apr 20 '15 at 09:15
  • @Kiran, this really shouldn't be in comments. There is a reason there is size limit and formatting limits here on StackOverflow. You can add this information as edit to your question: "EDIT: I tried doing so-and-so and this is the code and the output", and put everything there, properly formattid. I can't follow the logic like this, but I think you are trying to guess what the robot is typing by using `typeLableName`? This won't work. – RealSkeptic Apr 20 '15 at 09:32
  • typeLableName will get the KeyEvent.getKeyText(int keyCode) . Which is label name of the Key Code which is pressed/released will be typed into notepad. Inbetween will try in my local & try to understand the behaviour – Kiran Apr 20 '15 at 12:07
1

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.

Ractoc
  • 239
  • 2
  • 9
  • This is not what the OP is asking. He is asking about key values that don't have a `VK_...` constant at all, and how to create those with a java `Robot`. – RealSkeptic Apr 17 '15 at 11:39
  • Thank Ractoc & RealSkeptic, Actually 1 way its gives all the fields but not as per the Keyboard layout. Also then if do robot.keyPress for keyValue I guess it may not have the code for Unicode character..will try out . And as RealSkeptic said am trying to type Unicode characters using robot engine – Kiran Apr 17 '15 at 11:43