【发布时间】:2016-02-13 20:09:36
【问题描述】:
我正在开发一个用于游戏的小工具,比如热键,它处理用户按键事件的组合并使用 Awt Robot 模拟几次按键。问题是调用方法 ghostWalk() 仅在第一次按下注册组合时按预期工作,第二次当我按下相同的组合并且调用相同的方法时,仅模拟“robot.delay”之后的最后一个键。请阅读更多代码注释。
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class L {
int i = 160;
public static void main(String args[]){
L l = new L();
l.k();
}
public void k(){
// Register keys combination
JIntellitype.getInstance();
JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_ALT, (int)'D');
JIntellitype.getInstance().registerHotKey(2, JIntellitype.MOD_ALT, (int)'J');
JIntellitype.getInstance().registerHotKey(3, JIntellitype.MOD_ALT, (int)'K');
JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {
public void onHotKey(int aIdentifier) {
// First indentifier Alt + D
if (aIdentifier == 1) {
System.out.println("Alt+D hotkey pressed");
// Start new thread
new Thread(new Runnable() {
@Override
public void run() {
L gh = new L();
gh.ghostWalk();
}
}).start();
}
// Second indetifier
else if(aIdentifier == 2){
i += 10;
System.out.println(i);
}
// Third
else if(aIdentifier == 3){
i -= 10;
System.out.println(i);
}
}
});
}
// Method called to simulate key press
/* So, first time when i press Alt + D in game after program runs
all work good, the keys are simulated as expected but if I press again and again
the combination only key "d" are simulated which is after "delay(i), i = 160"
If program is restarted all again is the same, only first time when i press registered
combinations the program work as expected.
Second and others times program work only if there is delay "robot = new Robot();
robot.delay(100);"
on 100 delay program work well on 40ms need to press very fast the combination
so the program work as expected. How to fix it ? to simulate key press without
delay like first time when program is run.
P.s no matter in which window(game, notepad) you press combination to simulate key press
still work good only first time.
Example of output when i press two times combinations in game
first time: qqwewwd
second time: d
*/
public void ghostWalk(){
Robot robot = null;
try {
robot = new Robot();
//robot.delay(100);
robot.keyPress(KeyEvent.VK_Q);
robot.keyRelease(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_Q);
robot.keyRelease(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_R);
robot.keyRelease(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_W);
robot.delay(i);
robot.keyPress(KeyEvent.VK_D);
robot.keyRelease(KeyEvent.VK_D);
} catch (Exception e) {
e.printStackTrace();
}
}
}
编辑:我不明白在第一次调用 ghostWalk 方法后做了哪些更改,我应该使用延迟,以便我的程序按预期工作。
Edit2: 在我的情况下,当 setAutoWaitForIdle 设置为 true 并且 delay = 5(开始模拟按键之前的延迟)时,代码开始工作,如果介于两者之间
keypress 是一个延迟,就像在我的情况下 delay(i), i = 160 那么启动延迟应该是 60~。 (如果启动延迟为 40~ 那么并不总是很好,需要非常快地按下注册键组合 idk 为什么,如果启动延迟小于 40-60 在我的情况下仅模拟最后一次按键“D”,这是延迟之后( i).)
如果在第一次注册的热键事件中删除 robot.setAutoWaitForIdle(true) 和 robot.delay(60),该工具将按预期工作,在第二次、第三次等次 - 不是。在第一个 JIntellitype 事件之后,仅此代码起作用。
public void ghostWalk(){
Robot robot = null;
try {
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.delay(60);
robot.keyPress(KeyEvent.VK_Q);
robot.keyRelease(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_Q);
robot.keyRelease(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_R);
robot.keyRelease(KeyEvent.VK_R);;
robot.delay(i);
robot.keyPress(KeyEvent.VK_D);
robot.keyRelease(KeyEvent.VK_D);
} catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】:
-
你是说如果你增加延迟,你的程序按预期工作,但你不想延迟?
-
是的..例如在蒸汽中或在其他软件中,当您按 alt 时按预期工作不会改变焦点,然后我按 Alt D 没有延迟,没有错,一切都很好。首先,我认为这可能是一些游戏限制,但第一次完美运行 + 我有一个不是我编写的工具,它可以像我想在我的工具中做的那样与游戏一起工作。
标签: java awtrobot jintellitype