title: Why does my keyboard hook receive the same key-up and key-down events multiple times?
body: <p>In <a href="https://stackoverflow.com/questions/9833544/windows-keyboardhook-reporting-everything-twice">my previous question</a>, I reported that a keyboard hook was reporting everything twice when scanning a barcode.</p>
  
  <p>I put that down to key down &amp; key events and received good advice. </p>
  
  <p>Having looked at it more closely I find that each digit is actually being report FOUR times!</p>
  
  <p>Here's a crude "debug by print". Can anyone suggest what I might be doing wrong? Do you need more info? I <em>could</em> just ignore every second input, but ... yeuck!  I would rather understand what is happening.</p>
  
  <p>Here's what I got for a single digit 2  </p>
  
  <pre><code>---------
  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
  </code></pre>
  
  <hr>
  
  <p>Update: here's my code</p>
  
  <pre><code>function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt): LongInt; stdcall;
  begin
     if Code &lt; 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) &lt;&gt; $80000000)  (* not key up *)
     or ((LongParam and $40000000) &lt;&gt; $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;
  </code></pre>
  
  <p>At this point I have a bar code digit.   But those memo lines were added before here. </p>