2012-03-26 141 views
5

my previous question中,我報告說,掃描條形碼時,鍵盤鉤子報告了所有內容。爲什麼我的鍵盤掛鉤多次接收到相同的鍵盤鍵和鍵盤鍵事件?

我把它放下來,關鍵事件&,並收到了很好的建議。

仔細看了看,我發現每個數字實際上都是報告四次!

這裏是一個粗略的「打印調試」。任何人都可以建議我可能做錯了什麼?你需要更多信息嗎?我可能只是忽略每一秒的輸入,但... yeuck!我寧願明白髮生了什麼。

這就是我得到了一個數字2

--------- 
LongParam = 196609 | Word = 50 | 2 
LongParam and $80000000 = 0 
LongParam and $40000000 = 0 
--------- 
LongParam = 196609 | Word = 50 | 2 
LongParam and $80000000 = 0 
LongParam and $40000000 = 0 
--------- 
LongParam = -1073545215 | Word = 50 | 2 
LongParam and $80000000 = 2147483648 
LongParam and $40000000 = 1073741824 
--------- 
LongParam = -1073545215 | Word = 50 | 2 
LongParam and $80000000 = 2147483648 
LongParam and $40000000 = 1073741824 

更新:這裏是我的代碼

function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt): LongInt; stdcall; 
begin 
    if Code < 0 then // http://msdn.microsoft.com/enus/library/windows/desktop/ms644984%28v=vs.85%29.aspx 
    begin 
     Result := CallNextHookEx(KBHook, Code, WordParam, LongParam); 
     Exit; 
    end; 

MainForm.Memo1.Lines.Add('---------'); 
MainForm.Memo1.Lines.Add('LongParam = ' + IntToStr(LongParam) + ' | Word = ' +   IntToStr(Ord(WordParam)) + ' | ' + Char(WordParam)); 
MainForm.Memo1.Lines.Add('LongParam and $80000000 = ' + IntToStr(LongParam and $80000000)); 
MainForm.Memo1.Lines.Add('LongParam and $40000000 = ' + IntToStr(LongParam and $40000000)); 

    if ((LongParam and $80000000) <> $80000000) (* not key up *) 
    or ((LongParam and $40000000) <> $40000000) (* key was not previously down *) 
    then 
    begin 
     Result := CallNextHookEx(KBHook, Code, WordParam, LongParam); 
     Exit; 
    end; 

    if MainForm.ScanningChemical = False then 
    begin 
     Result := CallNextHookEx(KBHook, Code, WordParam, LongParam); 
     Exit; 
    end; 

在這一點上我有一個條形碼數字。但是這些備忘錄行在此之前添加了。

+1

嘗試包括代碼,您正在使用處理鍵盤鉤來幫助你。 – RRUZ 2012-03-26 02:47:46

+0

已添加1個代碼。我試圖擺脫關鍵並只處理關鍵,但我似乎收到每個 – Mawg 2012-03-26 03:18:24

回答

9

您的問題與您如何評估Code param的值有關。 關於KeyboardProc callback function狀態的文檔:

HC_NOREMOVE 的wParam和lParam參數包含關於一個按鍵消息的信息,和該鍵擊消息尚未 從消息隊列中刪除。 (應用程序稱爲的PeekMessage 功能,指定PM_NOREMOVE標誌。)

要解決該問題,只需更換該代碼

if Code < 0 then 
    begin 
     Result := CallNextHookEx(KBHook, Code, WordParam, LongParam); 
     Exit; 
    end; 

有了這個

if (Code < 0) or (Code = HC_NOREMOVE) then 
    begin 
     Result := CallNextHookEx(KBHook, Code, wparam, lparam); 
     Exit; 
    end; 
+0

+1的*兩*和答案謝謝*因此*很多。老實說,我真的很感謝 – Mawg 2012-03-26 04:31:44

+0

,我不明白這一點。這是什麼意思,擊鍵已被「刪除」?如果有人已經刪除它,它肯定不會達到我的代碼? – Mawg 2012-03-30 06:30:22