How to hook keyboard in Delphi

It's very simple. Just drop TKeyboardHook component onto your form and use its events to get complete information about all keyboard events. TKeyboardHook offers OnKeyDown/OnKeyUp, OnChars for letter/digit keys and OnKeyboardHook for access to raw hook data.

procedure TFormMain.KeyboardHookKeyboardHook(Sender: TObject;
  VirtualKeyCode, ScanCode, Flags, Time: Integer; ExtraInfo: Pointer);
begin
  MemoOnKeyboardHook.Lines.Add(
    IntToHex(VirtualKeyCode,8)+' '+
    IntToHex(ScanCode,8)+' '+
    IntToHex(Flags,8)+' '+
    IntToHex(Time,8)+' '+
    IntToHex(Integer(ExtraInfo),8));
end;

procedure TFormMain.KeyboardHookKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  MemoOnKeyboardEvents.Lines.Add('OnKeyDown '+ShortCutToText(ShortCut(Key,Shift)));
end;

procedure TFormMain.KeyboardHookKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  MemoOnKeyboardEvents.Lines.Add('OnKeyUp '+ShortCutToText(ShortCut(Key,Shift)));
end;

procedure TFormMain.KeyboardHookChars(Sender: TObject; Chars: String);
begin
  with MemoEnteredText do Text:=Text+Chars;
end;

TKeyboardHook component works system-wide and does not require external DLL.

See also
Keyboard Hook
Mouse Hook
Components for Delphi / C++ Builder
Howtos for Programmers