简体   繁体   English

如何以编程方式将按键事件发送到Java App中的任何窗口/进程?

[英]How to programmatically send a key event to any window/process in a Java App?

使用Java应用程序,如何以编程方式将键事件(字母,数字,标点,箭头等)发送/触发到同一台计算机上的窗口/进程?

Assuming you know a position that window will be you could use java.awt.Robot 假设您知道窗口的位置,则可以使用java.awt.Robot

This types a in whatever window is covering 10,50 on the screen. 在屏幕上覆盖10,50的任何窗口中都输入a。

Robot r = new Robot();
r.mouseMove(10, 50);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.keyPress(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_A);

If you had one window you know to cover 10,50 another at 10,400 and another at 400, 400 then this would type xy and z in the different windows. 如果您有一个窗口,您知道可以覆盖10,50的另一个窗口(分别为10,400和400、400),那么这将在不同的窗口中键入xy和z。 In my testing I also needed some delays before moving to make it more reliable. 在我的测试中,在使它变得更可靠之前,我还需要一些延迟。

Robot r = new Robot();
r.mouseMove(10, 50);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_X);
r.keyRelease(KeyEvent.VK_X);
Thread.sleep(500);
r.mouseMove(10, 400);
Thread.sleep(500);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_Y);
r.keyRelease(KeyEvent.VK_Y);
Thread.sleep(500);
r.mouseMove(400, 400);
Thread.sleep(500);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.keyPress(KeyEvent.VK_Z);
r.keyRelease(KeyEvent.VK_Z);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将中断键序列发送到Java进程? - How to send interrupt key sequence to a Java Process? 如何以编程方式确定在Java中运行x11窗口的进程 - How to programmatically determine process running x11 window in Java 如何以编程方式检测是否有任何屏幕录制进程/应用程序在 Android 中运行? - How to programmatically detect if any screen recording process/app is running in Android? Java Process API-将Tab键按下事件发送到交互式Shell程序 - Java Process API - send tab key press event to interactive shell program 将箭头键输入发送到Java中的子进程 - Send Arrow key input to child process in java 如何以编程方式将输入发送到在浏览器窗口中运行的Java应用程序? - How to programatically send input to a java app running in a browser window? 如何在 Java 中发送密码进行处理 - How to Send a Password to Process in Java 如何将EOF发送到Java中的进程? - How to send EOF to a process in Java? 无法以编程方式在Webview Java android中发送单击按钮事件 - Can't programmatically send click button event in Webview Java android 如何以编程方式(C#或Java)在Windows中启动应用并在其窗口中调用click? - How to programmatically (C# or Java) launch an app in Windows and invoke click in it's window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM