使用机器人在 Java 中输入字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6641962/
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
Use a robot to type characters in Java
提问by dfdsfsdfsddsgsrew
I know how to have Robot simulate a Y keypress like so:
我知道如何让机器人模拟 Y 按键,如下所示:
Robot.keyPress(KeyEvent.VK_Y);
But how do I get Robot to press a quote and period?:
但是我如何让机器人按下报价和句号?:
".
Can anyone provide me some reference page or sample code?
谁能给我一些参考页面或示例代码?
回答by Chris Gregg
What do you mean by "programmatically type these characters?"
“以编程方式输入这些字符”是什么意思?
You can use a backslash (\
) to print a double-quote, but you don't need anything special for the period:
您可以使用反斜杠 ( \
) 打印双引号,但您不需要任何特殊的句点:
System.out.println("This is a quote symbol: \" and this is a period: .");
Output:
输出:
This is a quote symbol: " and this is a period: .
回答by user510210
Your question is not clear, but to print the characters you can use a stream using the following snippet as a template:
您的问题不清楚,但要打印字符,您可以使用以下代码段作为模板使用流:
System.out.println("\".");
System.out.println("\".");
回答by Hovercraft Full Of Eels
If you wanted to use Robot, KeyEvent has VK_QUOTE and VK_PERIOD constants. All of these constants and more are available through the KeyEvent API
如果你想使用 Robot,KeyEvent 有 VK_QUOTE 和 VK_PERIOD 常量。所有这些常量以及更多内容均可通过KeyEvent API 获得
回答by camickr
You can't always just use the KeyEvent.VK... variable.
您不能总是只使用 KeyEvent.VK... 变量。
For example on my keyboard the "%" character is above the "5". To use a Robot to type a "5", the code would be:
例如,在我的键盘上,“%”字符位于“5”上方。要使用机器人输入“5”,代码为:
robot.keyPress(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_5);
and use a Robot to type a "%", the code would be:
并使用机器人输入“%”,代码为:
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_SHIFT);
回答by Brad Turek
Previous Robot
s seem to be deprecated.
以前的Robot
s 似乎已被弃用。
For the time being, for JavaFX, there's FXRobot
目前,对于 JavaFX,有 FXRobot
FXRobot robot = FXRobotFactory.createRobot(scene);
robot.keyPress(KeyCode.QUOTE);
robot.keyPress(KeyCode.PERIOD);