0
我已經實現了keyBoardHook,但是在shift鍵關閉時卻無法檢測到按下的字符。使用jna檢測Shift鍵向下鍵
我已經嘗試使用Windows的GetAsyncKeyState函數來檢測何時按下shift鍵。但是這並沒有處理shift + 2 = @。它覆蓋了shift鍵並且輸出了鍵碼2.
我可以獲得每一個鍵,但是Shift + 2都作爲單獨的鍵被檢測到(儘管[SHIFT + 2]給出了我鍵盤上的@)。 IE:程序輸出SHIFT和2,但不是它們產生的內容:@。
問題:
我怎樣才能檢測到移位鍵關閉時產生的字符。
我寫的代碼到目前爲止。
public class Keyhook {
private static volatile boolean quit;
private static HHOOK hhk;
private static LowLevelKeyboardProc keyboardHook;
public static void main(String[] args) {
final User32 lib = User32.INSTANCE;
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
keyboardHook = new LowLevelKeyboardProc() {
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
if (nCode >= 0) {
switch (wParam.intValue()) {
case User32.WM_KEYUP:
break;
case User32.WM_KEYDOWN:
if(lib.GetAsyncKeyState(160) == 1){
System.out.println(info.vkCode);
}
break;
case User32.WM_SYSKEYUP:
break;
case User32.WM_SYSKEYDOWN:
System.err.println("in callback, key=" + info.vkCode);
if (info.vkCode == 81) {
quit = true;
}
}
}
return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
}
};
hhk = lib.SetWindowsHookEx(User32.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
//noinspection ConstantConditions
new Thread() {
public void run() {
while (!quit) {
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
System.err.println("unhook and exit");
lib.UnhookWindowsHookEx(hhk);
System.exit(0);
}
}.start();
// This bit never returns from GetMessage
int result;
MSG msg = new MSG();
while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
if (result == -1) {
System.err.println("error in get message");
break;
} else {
System.err.println("got message");
lib.TranslateMessage(msg);
lib.DispatchMessage(msg);
}
}
lib.UnhookWindowsHookEx(hhk);
}
}
看看這個答案http://stackoverflow.com/q/6237250/233906它的解決方案http://melloware.com/products/jintellitype/index.html – Cerber