Recent

Author Topic: [SOLVED] Keyboard hook  (Read 3519 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 636
[SOLVED] Keyboard hook
« on: May 20, 2021, 01:48:08 pm »
I use the following code to hook the keyboard.

How can I make it "eat" the keypress?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode delphi}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   Windows, LCLType, Menus, StdCtrls, ComCtrls, MyTimer, myled, MMSystem;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Label3: TLabel;
  19.     Label4: TLabel;
  20.     TrackBar1: TTrackBar;
  21.     procedure FormCreate(Sender: TObject);
  22.     procedure FormDestroy(Sender: TObject);
  23.   private
  24.     function Start: boolean;
  25.     function Stop: Boolean;
  26.     function IsStarted: Boolean;
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.   llKeyboardHook: HHOOK = 0;
  34.   AHandle : HWND;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. procedure SendKey(Wnd, VK : Cardinal);
  41. begin
  42.   ShowWindow(Wnd, SW_SHOW);
  43.   SetForegroundWindow(Wnd);
  44.  
  45.   keybd_event(VK,0,0,0);
  46.   keybd_event(VK,0,KEYEVENTF_KEYUP,0);
  47. end;
  48.  
  49. function LowLevelKeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
  50. type
  51.   PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
  52.   TKBDLLHOOKSTRUCT = packed record
  53.     vkCode: DWORD;
  54.     scanCode: DWORD;
  55.     flags: DWORD;
  56.     time: DWORD;
  57.     dwExtraInfo: DWORD;
  58.     AHandle : HWND;
  59.   end;
  60. var
  61.   pkbhs: PKBDLLHOOKSTRUCT;
  62.   bPASSON : boolean;
  63. begin
  64.   bPASSON := true;
  65.   pkbhs := PKBDLLHOOKSTRUCT(Pointer(lParam));
  66.   Form1.Label2.Caption := pkbhs^.vkCode.ToString;
  67.   Form1.Label4.Caption := pkbhs^.flags.ToString;
  68.  
  69.     if ((pkbhs^.vkCode = 109)) then  //-
  70.       begin
  71.         Form1.TrackBar1.Position := Form1.TrackBar1.Position - 1;
  72.          bPASSON := False;
  73.       end;
  74.  
  75.   if ((pkbhs^.vkCode = 107)) then   //+
  76.     begin
  77.       Form1.TrackBar1.Position := Form1.TrackBar1.Position + 1;
  78.       bPASSON := False;
  79.     end;
  80.  
  81.   Result := CallNextHookEx(llKeyboardHook, 0, wParam, lParam);
  82. end;
  83.  
  84. { TForm1 }
  85.  
  86. function TForm1.Start: boolean;
  87. const
  88.   WH_KEYBOARD_LL = 13;
  89. begin
  90.   if llKeyboardHook = 0 then
  91.     llKeyboardHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, HInstance, 0);
  92.   Result := (llKeyboardHook <> 0)
  93. end;
  94.  
  95. function TForm1.Stop: Boolean;
  96. begin
  97.   Result := False;
  98.   if (llKeyboardHook <> 0) and UnhookWindowsHookEx(llKeyboardHook) then
  99.   begin
  100.     llKeyboardHook := 0;
  101.     Result := True;
  102.   end;
  103. end;
  104.  
  105. function TForm1.IsStarted: Boolean;
  106. begin
  107.   Result := (llKeyboardHook <> 0)
  108. end;
  109.  
  110. procedure TForm1.FormCreate(Sender: TObject);
  111. begin
  112.   if not Start then
  113.     MessageDlg('Cannot start the hook!', mtError, [mbOk], 0);
  114.   Label2.Caption := '0';
  115.   Label4.Caption := '0';
  116. end;
  117.  
  118. procedure TForm1.FormDestroy(Sender: TObject);
  119. begin
  120.   if IsStarted then
  121.     Stop;
  122. end;
  123.  
  124. end.
  125.  
« Last Edit: May 20, 2021, 03:47:25 pm by pcurtis »
Windows 10
Laz 2.0.10
FPC 3.2.0

GetMem

  • Hero Member
  • *****
  • Posts: 3458
Re: Keyboard hook
« Reply #1 on: May 20, 2021, 01:54:36 pm »
Like this:
Code: Pascal  [Select][+][-]
  1. if EatMe then
  2.   Result := -1
  3. else
  4.   Result := CallNextHookEx(llKeyboardHook, 0, wParam, lParam);

pcurtis

  • Hero Member
  • *****
  • Posts: 636
Re: Keyboard hook
« Reply #2 on: May 20, 2021, 03:43:55 pm »
Thanks. Works perfectly.
Windows 10
Laz 2.0.10
FPC 3.2.0

 

TinyPortal © 2005-2018