如何以编程方式生成按键事件?

How can I programmatically generate keypress events?(如何以编程方式生成按键事件?)
本文介绍了如何以编程方式生成按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

java 程序应该做的是它应该在某些情况下触发键盘按下,而无需人按下键盘键.因此,任何在 windows 中运行且需要键盘输入的程序都可以在没有人实际按下键盘的情况下获得输入.

What the java program should do is it should trigger keyboard press on some condition without a person pressing a keyboard key. So any program running in windows and in focus which requires keyboard input will get the input without a person actually pressing the keyboard.

我在这里找到了这些相关问题:问题1,问题2

I found these related questions here : question 1, question 2

我想知道在 Java 中是否有任何方法可以做到这一点.

I was wondering if there is any method to do this in Java.

推荐答案

使用 机器人 类.

代码片段:

import java.awt.Robot;
import java.awt.KeyEvent;

Robot r = new Robot();
int keyCode = KeyEvent.VK_A; // the A key
r.keyPress(keyCode);
// later...
r.keyRelease(keyCode);

但是,如果您尝试在计算机上自动执行一项任务,我建议您使用 AutoHotKey.它致力于自动化常见任务,因此使用它而不是 Java 会更容易.

However, if you are trying to automate a task on your computer, I would recommend AutoHotKey. It's dedicated to automating common tasks, so it would be easier to use it instead of Java.

这篇关于如何以编程方式生成按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)