Java Robot with Azerty vrs Qwerty

2020-03-30 04:30发布

问题:

I hope this isn't a duplicate, but I've scoured the forums and have yet to find any answers.

I am having issues with my java application using the Robot class to type text. The text is provided and the application types it out. When the user has a QWERTY keyboard everything works fine. When the user has an AZERTY keyboard the values come out incorrectly.

For example feeding "1234567890" into the software types "&é"'(§è!çà"

I'm hoping some people have an idea how to support multiple keyboard layouts. Thank you in advance.

Here is a snippet

String TRANSLATION_SHIFT = "~!@#$%^&*()_+{}|:\"<>?";
String TRANSLATION_NON_SHIFT = "`1234567890-=[]\\;',./";
Robot robot = new Robot();
String text = "1234567890";
int key;
for (char c: text.toCharArray()){
  switch(c){
    ....
    case '0':
      key = KeyEvent.VK_0;
      break;
case '1':
  key = KeyEvent.VK_1;
  break;
...(etc etc)...
case '9':
  key = KeyEvent.VK_9;
  break;
    ...
  }
  robot.keyPress(key);
  robot.keyRelease(key);
}

There is also code in there to

回答1:

I think you will need to be aware of the Keyboard layout, I am not sure if there is some built-in function to specify the locale to the Robot class, but you can find an wrapper implementation for Robot class that support different keyboard layouts see this library