Recent

Author Topic: [SOLVED]Help modifying Keyhook  (Read 535 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED]Help modifying Keyhook
« on: August 11, 2020, 03:32:49 pm »
Hi All,

I have found the following app (see attachment).

Code: Pascal  [Select][+][-]
  1. unit unit1;
  2.  
  3. {$mode delphi}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Windows, MouseAndKeyInput, LCLType;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Label3: TLabel;
  19.     Label4: TLabel;
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormDestroy(Sender: TObject);
  22.   private
  23.     function Start: boolean;
  24.     function Stop: Boolean;
  25.     function IsStarted: Boolean;
  26.     { private declarations }
  27.   public
  28.     { public declarations }
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38.  
  39. var
  40.   llKeyboardHook: HHOOK = 0;
  41.   bSKIP : boolean;
  42.  
  43. { TForm1 }
  44.  
  45. function LowLevelKeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
  46. type
  47.   PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
  48.   TKBDLLHOOKSTRUCT = packed record
  49.     vkCode: DWORD;
  50.     scanCode: DWORD;
  51.     flags: DWORD;
  52.     time: DWORD;
  53.     dwExtraInfo: DWORD;
  54.   end;
  55. var
  56.   pkbhs: PKBDLLHOOKSTRUCT;
  57. begin
  58.   pkbhs := PKBDLLHOOKSTRUCT(Pointer(lParam));
  59.  
  60.   Form1.Label1.Caption := inttostr(pkbhs^.vkCode);
  61.   Form1.Label2.caption := inttostr(pkbhs^.scanCode);
  62.   Form1.Label3.Caption := inttostr(pkbhs^.dwExtraInfo);
  63.   Form1.Label4.caption := inttostr(pkbhs^.flags);
  64.  
  65.   CallNextHookEx(llKeyboardHook, nCode, wParam, lParam);
  66. end;
  67.  
  68. function TForm1.Start: boolean;
  69. const
  70.   WH_KEYBOARD_LL = 13;
  71. begin
  72.   if llKeyboardHook = 0 then
  73.     llKeyboardHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, HInstance, 0);
  74.   Result := (llKeyboardHook <> 0)
  75. end;
  76.  
  77. function TForm1.Stop: Boolean;
  78. begin
  79.   Result := False;
  80.   if (llKeyboardHook <> 0) and UnhookWindowsHookEx(llKeyboardHook) then
  81.   begin
  82.     llKeyboardHook := 0;
  83.     Result := True;
  84.   end;
  85. end;
  86.  
  87. function TForm1.IsStarted: Boolean;
  88. begin
  89.   Result := (llKeyboardHook <> 0)
  90. end;
  91.  
  92. procedure TForm1.FormDestroy(Sender: TObject);
  93. begin
  94.   if IsStarted then
  95.     Stop;
  96. end;
  97.  
  98. procedure TForm1.FormCreate(Sender: TObject);
  99. begin
  100.   if not Start then
  101.     MessageDlg('Cannot start the hook!', mtError, [mbOk], 0)
  102. end;
  103.  
  104. end.
  105.  
  106.  

How would I go about changing the pressed key into, for example, 'B'. (not by returning the keycode for B, but returning SHIFT + b.
and how can I compile in objfpc mode?

Thanks in advance.
« Last Edit: August 12, 2020, 03:13:01 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: [SOLVED]Help modifying Keyhook
« Reply #1 on: August 12, 2020, 03:13:59 pm »
Google is your friend
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018