使用机器人在 Java 中输入字符
我知道如何让机器人像这样模拟 Y 按键:
Robot.keyPress(KeyEvent.VK_Y);
但是如何让机器人按下引号和句号?:
".
任何人都可以为我提供一些参考页或示例代码吗?
I know how to have Robot simulate a Y keypress like so:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能总是只使用 KeyEvent.VK... 变量。
例如,在我的键盘上,“%”字符位于“5”上方。要使用机器人键入“5”,代码为:
使用机器人键入“%”,代码为:
You can't always just use the KeyEvent.VK... variable.
For example on my keyboard the "%" character is above the "5". To use a Robot to type a "5", the code would be:
and use a Robot to type a "%", the code would be:
如果你想使用Robot,KeyEvent有VK_QUOTE和VK_PERIOD常量。所有这些常量和更多常量都可以通过 KeyEvent 获得API
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
似乎已被弃用。目前,对于 JavaFX,有
FXRobot
Previous
Robot
s seem to be deprecated.For the time being, for JavaFX, there's
FXRobot
“以编程方式键入这些字符”是什么意思?
您可以使用反斜杠 (
\
) 来打印双引号,但句点不需要任何特殊内容:输出:
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:Output:
您的问题不清楚,但要打印字符,您可以使用以下代码片段作为模板使用流:
System.out.println("\".");
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("\".");