java 如何使机器人类型成为`:`?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5736129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make Robot type a `:`?
提问by Eric Bautista
I want to type :
using Java Robot. However, I'm getting an IllegalArgumentException
. My code is:
我想:
使用 Java Robot打字。但是,我得到了一个IllegalArgumentException
. 我的代码是:
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_COLON);
robot.keyRelease(KeyEvent.VK_COLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
The exception is:
例外是:
java.lang.IllegalArgumentException: Invalid key code.].
I also tried with:
我也试过:
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
How can I solve this problem?
我怎么解决这个问题?
回答by GuruKulki
try with this code :
尝试使用此代码:
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
As with the keyboard you enter : when you press shift + ;. the same you need to simulate.
与您输入的键盘一样:当您按下 shift + ; 时。与您需要模拟的相同。
Try running this code just to try out which works fine with above answer:
尝试运行此代码只是为了尝试使用上述答案可以正常工作:
public class Test {
public static void main(String[] args) {
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
} catch (AWTException e) {
// TODO Auto-generated catch bloc
e.printStackTrace();
}
}
}
回答by Tim Bender
Unfortunately, Java Robot
class relies on a platform specific implementation of an undocumented interface called java.awt.peer.RobotPeer
. The platform specific implementation decides what key press events are legal or illegal.
不幸的是,JavaRobot
类依赖于一个名为java.awt.peer.RobotPeer
. 平台特定的实现决定了哪些按键事件是合法的或非法的。
On my windows XP box, this works fine:
在我的 Windows XP 机器上,这很好用:
public static void main(final String[] args) throws InterruptedException, IOException {
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
} catch (final AWTException e) {
// TODO Auto-generated catch bloc
e.printStackTrace();
}
}
On a different platform you may want to try:
在不同的平台上,您可能想尝试:
public static void main(final String[] args) throws InterruptedException, IOException {
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_COLON);
robot.keyRelease(KeyEvent.VK_COLON);
} catch (final AWTException e) {
// TODO Auto-generated catch bloc
e.printStackTrace();
}
}
回答by Misose
try this code ;), maybe it helps (using ascii code alt+5+8=:):
试试这个代码;),也许它有帮助(使用 ascii 代码 alt+5+8=:):
robot9.delay(20);
robot9.keyPress(KeyEvent.VK_ALT);
robot9.delay(20);
robot9.keyPress(KeyEvent.VK_NUMPAD5);
robot9.keyRelease(KeyEvent.VK_NUMPAD5);
robot9.delay(20);
robot9.keyPress(KeyEvent.VK_NUMPAD8);
robot9.keyRelease(KeyEvent.VK_NUMPAD8);
robot9.delay(20);
robot9.keyRelease(KeyEvent.VK_ALT);
robot9.delay(20);
回答by Thomas Weller
This also seems to be language dependent. On a German keyboard, using the combination of VK_SHIFT
and VK_PERIOD
worked.
这似乎也取决于语言。在德语键盘,使用的结合VK_SHIFT
和 VK_PERIOD
合作。
回答by Omar N
Someone build a KeyboardKeys class and published it here in SO. it′s at https://stackoverflow.com/a/20979488/7069565. In a nutshell, he types every character as an Alt + Number combination.
有人构建了一个 KeyboardKeys 类并将其发布在 SO 中。它位于https://stackoverflow.com/a/20979488/7069565。简而言之,他将每个字符键入为 Alt + Number 组合。
回答by Saclyr Barlonium
Semicolon is an "upercase leter", that is, you only get it with the combination of Keys
分号是一个“大写字母”,也就是说,你只有用Keys的组合才能得到它
Shift+Coma
Shift+昏迷
Try this:
试试这个:
robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_COMMA);
//Since you have the Shift pressed it will generate a semi colon.
robot.keyRelease(KeyEvent.VK_COMMA);
robot.keyRelease(KeyEvent.VK_SHIFT);
I hope I have helped.
我希望我有所帮助。
Have a nice day. :)
祝你今天过得愉快。:)
回答by Nirav Dabhi
Try This Code
试试这个代码
case KeyEvent.VK_SEMICOLON:
if((event.getModifiers() & KeyEvent.KEY_PRESSED)!=0)
System.out.println(":");
else
System.out.print(";");
break;
回答by None
I don't know about Java Robots, but if you're using shift, shouldn't you then type semicolon, because shift + semicolon = colon. So it's probably an illegal argument because colon isn't a key, semicolon is.
我不了解 Java Robots,但是如果您使用 shift,那么您不应该输入分号,因为 shift + 分号 = 冒号。所以这可能是一个非法参数,因为冒号不是键,分号才是。