使用 Java Robot 模拟按键类型
我编写了一个方法来模拟 KeyEvent 中的按键操作,如下所示:
private Robot robot(){
if(robot==null){
try {
return new Robot();
} catch (AWTException e) {
throw new RuntimeException("Failed to create instance of Robot");
}
}else{
return robot;
}
}
public void sendKeyEvent(KeyEvent evt) throws IOException {
int type = evt.getID();
if(type == KeyEvent.KEY_PRESSED){
if(evt.isShiftDown()){
robot().keyPress(KeyEvent.VK_SHIFT);
}
robot().keyPress(evt.getKeyChar());
}else if(type == KeyEvent.KEY_RELEASED){
robot().keyRelease(evt.getKeyChar());
if(evt.isShiftDown()){
robot().keyRelease(KeyEvent.VK_SHIFT);
}
}
}
当此方法收到按“A”键事件时,它可以键入“A”。
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='A',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_UNKNOWN]]
但问题是当它收到这个KeyEvent(按'a')时,它实际上按下了“1”。
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='a',keyLocation=KEY_LOCATION_UNKNOWN]]
谁能告诉我这个方法有什么问题吗?
I write a method to simulate key press from KeyEvent, like below:
private Robot robot(){
if(robot==null){
try {
return new Robot();
} catch (AWTException e) {
throw new RuntimeException("Failed to create instance of Robot");
}
}else{
return robot;
}
}
public void sendKeyEvent(KeyEvent evt) throws IOException {
int type = evt.getID();
if(type == KeyEvent.KEY_PRESSED){
if(evt.isShiftDown()){
robot().keyPress(KeyEvent.VK_SHIFT);
}
robot().keyPress(evt.getKeyChar());
}else if(type == KeyEvent.KEY_RELEASED){
robot().keyRelease(evt.getKeyChar());
if(evt.isShiftDown()){
robot().keyRelease(KeyEvent.VK_SHIFT);
}
}
}
When this method received press 'A' key event, it could type 'A'.
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='A',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_UNKNOWN]]
But the problem is when it received this KeyEvent(press 'a'), it acturaly pressed "1".
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='a',keyLocation=KEY_LOCATION_UNKNOWN]]
Could anyone tell me what wrong with this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点棘手和令人困惑,你很困惑。
没有“大写 a”和“小写 a”按键事件。只有“A/a”事件,您可以有或没有 SHIFT 修饰符。
恰好 VK_A 到 VK_Z 与 ASCII 'A' 到 'Z' 相同,但 'a' 到 'z' 则不然。
当您重新发送从 getKeyChar() 获得的“a”(ASCII 0x61,又名 97)时,您实际上发送的是 VK_NUMPAD1,这就是您收到“1”的原因。
getKeyChar 的 JavaDoc 是这样说的:
因此,当您尝试使用“A”时,您会返回 VK_A 并且事情会按您的预期进行。但是当你简单地输入“a”时,你会得到 0x61,这不是你想要的。
据我所知,将 getKeyChar() 更改为 getKeyCode() 可以解决您的问题。
也就是说,我不会去搞乱 KEY_PRESS/KEY_RELEASED。我只是从那里拦截 KEY_TYPED 和“Robot”。
It's a bit tricky and confusing and you got confused.
There are no 'uppercase a' and 'lowercase a' key events. There are just 'A/a' events and you can have or not a SHIFT modifier.
It just happen to be that VK_A to VK_Z are identical to ASCII 'A' through 'Z' but not so for 'a' to 'z'.
When you're re-sending the 'a' (ASCII 0x61, aka 97) that you got from getKeyChar(), you're actually sending VK_NUMPAD1, which is why you get the '1'.
The JavaDoc for getKeyChar says this:
So when you try with 'A', you get back VK_A and things work as you expect. But when you simply type 'a', you get 0x61 which is not what you want.
As far as I can tell changing getKeyChar() to getKeyCode() would fix your problem.
That said I wouldn't go messing with KEY_PRESS/KEY_RELEASED. I'd simply intercept KEY_TYPED and "Robot" from there.