使用机器人在 Java 中输入字符

发布于 2024-11-19 11:24:01 字数 176 浏览 3 评论 0原文

我知道如何让机器人像这样模拟 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二智少女 2024-11-26 11:24:01

您不能总是只使用 KeyEvent.VK... 变量。

例如,在我的键盘上,“%”字符位于“5”上方。要使用机器人键入“5”,代码为:

robot.keyPress(KeyEvent.VK_5); 
robot.keyRelease(KeyEvent.VK_5);

使用机器人键入“%”,代码为:

robot.keyPress(KeyEvent.VK_SHIFT); 
robot.keyPress(KeyEvent.VK_5); 
robot.keyRelease(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_SHIFT);

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:

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);
要走干脆点 2024-11-26 11:24:01

如果你想使用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

江南月 2024-11-26 11:24:01

以前的 Robot 似乎已被弃用。

目前,对于 JavaFX,有 FXRobot

FXRobot robot = FXRobotFactory.createRobot(scene);
robot.keyPress(KeyCode.QUOTE);
robot.keyPress(KeyCode.PERIOD);

Previous Robots seem to be deprecated.

For the time being, for JavaFX, there's FXRobot

FXRobot robot = FXRobotFactory.createRobot(scene);
robot.keyPress(KeyCode.QUOTE);
robot.keyPress(KeyCode.PERIOD);
夜深人未静 2024-11-26 11:24:01

“以编程方式键入这些字符”是什么意思?

您可以使用反斜杠 (\) 来打印双引号,但句点不需要任何特殊内容:

System.out.println("This is a quote symbol: \" and this is a period: .");

输出:

This is a quote symbol: " and this is a period: .

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: .
柏林苍穹下 2024-11-26 11:24:01

您的问题不清楚,但要打印字符,您可以使用以下代码片段作为模板使用流:

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("\".");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文