【发布时间】:2019-08-18 17:20:39
【问题描述】:
我正在尝试创建一个调用 JNA 的 KeyboardUtils 类来检查 Windows 上的关键状态的示例(类似于 Win32 的 GetAsyncKeyState())。
这是我的代码:
package com.foo;
import com.sun.jna.platform.KeyboardUtils;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
System.out.println("Watching for Left/Right/Up/Down or WASD. Press Shift+Q to quit");
while (true) {
try
{
Thread.sleep(10);
if (KeyboardUtils.isPressed(KeyEvent.VK_DOWN) || KeyboardUtils.isPressed(KeyEvent.VK_S) )
{
System.out.println("Down");
}
if (KeyboardUtils.isPressed(KeyEvent.VK_UP) || KeyboardUtils.isPressed(KeyEvent.VK_W) )
{
System.out.println("Up");
}
if (KeyboardUtils.isPressed(KeyEvent.VK_LEFT) || KeyboardUtils.isPressed(KeyEvent.VK_A) )
{
System.out.println("Left");
}
if (KeyboardUtils.isPressed(KeyEvent.VK_RIGHT) || KeyboardUtils.isPressed(KeyEvent.VK_D) )
{
System.out.println("Right");
}
if (KeyboardUtils.isPressed(KeyEvent.VK_Q) && KeyboardUtils.isPressed(KeyEvent.VK_SHIFT) )
{
break;
}
}
catch(Exception e)
{ }
}
System.exit(0);
}
}.start();
}
}
这可以正常工作并检测 WASD 键以及 Shift+Q。但是,永远不会检测到箭头键 Left/Right/Up/Down。
将代码转换为 C++ 并调用 Win32 GetAsyncKeyState() 确实可以使用箭头键。
根据网络,KeyEvent.VK_DOWN 的值与Win32 definition (40) 匹配。
知道为什么 JNA 没有正确检测到箭头键吗?
【问题讨论】:
-
我还没有看到
GetAsyncKeyState的有效用例。你想完成什么? -
@IInpectable - 我的用例是在立即模式下读取键状态,而不是使用来自 awt 或 swing 的 KeyEvents。这就是 GetAsyncKeyState 的目的(这是 Windows 上的 isPressed 调用)。
-
我明白了,
GetAsyncKeyState做了什么。我不明白,你(或任何人)为什么要使用它。