Robot.keyPress does'nt work for VK_ALT_GRAPH (Invalid key code)

a 夏天 提交于 2019-12-11 02:59:34

问题


I'm working on a remoting app.(mouse - keyboard) I just showing fastly my issue...

It's working for 'A' or some basic characters.

    case "a":
    robot.keyPress(KeyEvent.VK_A);

But I did'nt make '@' character. My way is look like this.

 case "@":
 robot.keyPress(KeyEvent.VK_ALT_GRAPH);
 robot.keyPress(KeyEvent.VK_Q);
 robot.keyRelease(KeyEvent.VK_ALT_GRAPH);

I see the exception when I use VK_ALT_GRAPH;

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)

Is that a bug? How can I press '@' char? Also like this, I cant do these ; $ , # , ? , [ , ] , {, } , ' , * ...


回答1:


I know that this is very old, but nobody gave a correct answer and after practicing I went with the solution.

That is my keyboard:

and for press @ or # you must to do it:

            bot.keyPress(VK_CONTROL);
            bot.keyPress(VK_ALT);
            bot.keyPress(VK_3);

            bot.keyRelease(VK_CONTROL);
            bot.keyRelease(VK_ALT);
            bot.keyRelease(VK_3);


来源:https://stackoverflow.com/questions/22919591/robot-keypress-doesnt-work-for-vk-alt-graph-invalid-key-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!