Advertisement
HEX0x29A

KeyboardLowLevelHook

Oct 29th, 2017
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.86 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, Classes, Controls, Forms, StdCtrls;
  7.  
  8. type
  9.   TForm1 = class(TForm)
  10.     ListBox1: TListBox;
  11.     procedure FormCreate(Sender: TObject);
  12.     procedure FormDestroy(Sender: TObject);
  13.   end;
  14.  
  15. var
  16.   Form1: TForm1;
  17.  
  18. implementation
  19.  
  20. {$R *.dfm}
  21.  
  22. const
  23.   WH_KEYBOARD_LL = 13;
  24.  
  25. type
  26.   tagKBDLLHOOKSTRUCT = packed record
  27.     vkCode: DWORD;
  28.     scanCode: DWORD;
  29.     flags: DWORD;
  30.     time: DWORD;
  31.     dwExtraInfo: Integer;
  32.   end;
  33.  
  34.   KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  35.  
  36.   PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
  37.  
  38. var
  39.   hkHook: HHook;
  40.  
  41. function LowLevelKeyboardProc(Code, wParam, lParam: Integer): LRESULT; stdcall;
  42. var
  43.   KeyState: TKeyboardState;
  44.   NewChar: array[0..1] of Char;
  45.   Hook: PKBDLLHOOKSTRUCT;
  46. begin
  47.   try
  48.     Hook := Pointer(lParam);
  49.     case Code of
  50.       HC_ACTION:
  51.         if wParam = WM_KEYDOWN then
  52.         begin
  53.           FillChar(NewChar, 2, #0);
  54.           GetKeyboardState(KeyState);
  55.           ToAscii(Hook^.vkCode, Hook^.scanCode, KeyState, NewChar, 0);
  56.           case NewChar[0] of
  57.             'a'..'z', 'A'..'Z':
  58.               Form1.ListBox1.Items.Add('Латинская буква: ' + NewChar[0]);
  59.             'а'..'я', 'А'..'Я', 'ё', 'Ё':
  60.               Form1.ListBox1.Items.Add('Русская буква: ' + NewChar[0]);
  61.             '0'..'9':
  62.               Form1.ListBox1.Items.Add('Цифра: ' + NewChar[0]);
  63.           else
  64.             Form1.ListBox1.Items.Add('Другое: ' + NewChar[0]);
  65.           end;
  66.         end;
  67.     end;
  68.   finally
  69.     Result := CallNextHookEx(hkHook, Code, wParam, lParam);
  70.   end;
  71. end;
  72.  
  73. procedure TForm1.FormCreate(Sender: TObject);
  74. begin
  75.   hkHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardProc, hInstance, 0);
  76. end;
  77.  
  78. procedure TForm1.FormDestroy(Sender: TObject);
  79. begin
  80.   UnHookWindowsHookEx(hkHook);
  81. end;
  82.  
  83. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement