gpt4 book ai didi

java - 如何使用参数为 "KeyEvent e"的方法?

转载 作者:行者123 更新时间:2023-11-30 06:48:37 27 4
gpt4 key购买 nike

我已经查看了我之前的问题的一堆答案(已得到解答),但我一直看到的是这样的方法:

public void keyPressed(KeyEvent e)

但没有人显示这些方法的使用地点,所以我从来不知道如何处理该参数。

示例:

public void keyPressed(KeyEvent e, Robot r) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
r.keyPress(KeyEvent.VK_R);
r.mousePress(InputEvent.BUTTON1_MASK);
try { Thread.sleep(100); } catch (Exception s) {}
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
}

public static void autoCliker() throws AWTException, InterruptedException
{
Robot r = new Robot();
while(KeyPressed(not sure what to do here, r)//this is what my question is about
{
Thread.sleep(10);
r.keyPress(KeyEvent.VK_R);
r.mousePress(InputEvent.BUTTON1_MASK);
try { Thread.sleep(100); } catch (Exception e) {}
r.mouseRelease(InputEvent.BUTTON1_MASK);

}
}

更多的是关于如何在方法内的参数中使用事件而不是 KeyEvent,我只是使用我的一个程序作为示例。

最佳答案

当您的类实现 KeyListener 时,会显示此方法以及其他方法。

public class Test implements KeyListener {

此方法感测键盘上的按键按下情况。如果你想检测像w这样的某个键。这样做:

if(e.getKeyCode.equals(KeyEvent.VK_W);

希望这有帮助。

关于java - 如何使用参数为 "KeyEvent e"的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43245991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com