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

编程入门 行业动态 更新时间:2024-10-24 12:34:22
本文介绍了如何以编程方式生成按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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.

更多推荐

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

本文发布于:2023-11-07 11:22:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按键   事件   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!