詳細步驟
例行說明
- 上一個版本已經把鍵盤監聽的框架搭好了,就差調用綁定了
具體步驟
callback()
=>switchKey()
=>select()
沒了
代碼分析
callback()
監聽鍵盤事件,把vkCode
&wparam
傳遞給switchKey()
進行按鈕綁定以及控制
@Override
public LRESULT callback(int code, WPARAM wParam, KBDLLHOOKSTRUCT event) {
if(code >= 0) {
int key = event.vkCode;
int type = Integer.parseInt(wParam.toString());
switchKey(key, type);
return new LRESULT(1); //KIA all keys
}
return User32.INSTANCE.CallNextHookEx(hhk, code, wParam, null);
}
switchKey()
通過不同的key
定位不同的按鈕,並對不同的按鈕進行select
處理 (Press & Release)
public void select(JToggleButton jtb, int type) {
if(type == KeyboardPiano.BUTTON_UP) {
jtb.setSelected(false);
} else if(type == KeyboardPiano.BUTTON_DOWN) {
jtb.setSelected(true);
}
}
-
整體處理細節,請見 V1.3.1 KeyboardHook.java
-
按鈕綁定示意圖(博主一手把鍵盤中部的鍵全按下了,img1 顯示的是綁定後的結果)
-
img1
項目又朝着完整度的方向邁了一步 ^ ^
BUG 記錄
- 按鍵衝突 BUG(衝突區域見 img2)
- NumLock 對數字鍵盤的開啓 & 關閉,數字鍵區的按鈕失靈
- Alt 按鍵後導致有些按鍵無法按下/擡起
- 由於鉤子函數攔截了按鍵信息並屏蔽了系統按鍵,導致程序不退出,系統無法正常按鍵
這裏之所以沒有對 BUG 進行詳細描述,除了之前擼代碼的時候沒去注意到,同時也是因爲這些 BUGs 的糾正是在很後的版本處理的,再加上 BUG 不影響後續開發,所以這裏做了模糊處理
- img2
Options
Option1
-
以 Shift_Left 爲例
兩種鍵按下的wparam-flags
,與之對應兩種鍵擡起的wparam-flags
(其他按鍵相似,但略有不同)vkCode = 160 Pressed1 Pressed2 Released1 Released2 wparam 256 260 257 261 flags 0 32 128 160
再次強調,
wparam
標識鍵的 Pressed & Released
flags
則是對vkCode
相同的情況下進一步的分類
如 Enter & NumpadEnter 的vkCode
相同,但flags
不同,以此作爲區分的關鍵