Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1222
  • Last Modified:

delphi SetWindowsHookEx inside thread

Hi, im trying to hook the keyboard inside a thread.
The code works fine on the VLC form but inside the thread the hook wont work

the code is below

 type
  tagKBDLLHOOKSTRUCT = packed record // contém as informações do evento
    vkCode: DWord; // virtual key (1.254) vk_back,vk_return,etc
    scanCode: DWord;
    flags: DWord;
    time: DWord;
    dwExtraInfo: PDWord;
  end;

  TKBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;

type
  Thookbarcode = class(TThread)
  private
    apppath : string;
  protected
    procedure Execute; override;
  public
    hookhandle: HHook;
    constructor create(meubarcode : Tbarcodereader; apppath : string);
  end;

function LowLevelKeyboardHook(Code: integer; wParam: wParam; lParam: lParam)
    : longint;  stdcall;

var
  codigo: string;
  intervalo : cardinal;
  threadbarcode : Tbarcodereader;


implementation

constructor Thookbarcode.create(meubarcode: Tbarcodereader; apppath : string);
begin
  inherited create(true);
  threadbarcode := meubarcode;
  self.apppath := apppath;
  resume;

end;

function LowLevelKeyboardHook(Code: Integer; wParam: wParam; lParam: lParam)
  : longint; stdcall;
var
  p: PKBDLLHOOKSTRUCT;
begin
  p := PKBDLLHOOKSTRUCT(lParam);
  if (p.vkCode = VK_RETURN) then
  begin
  result := 1;
  exit;
  end;
  if wParam = $0100 then
  begin
    if (p.vkCode >= 48) and (p.vkCode <= 57) then
    begin
      if gettickcount - intervalo < 50 then
        codigo := codigo + Char(p.vkCode)
      else
        codigo := Char(p.vkCode);
      if length(codigo) > 7 then
      begin
        threadbarcode.add(codigo);
        codigo := '';
      end;
      Result := 1;
      intervalo := gettickcount;
      exit;
    end;
  end;
  Result := CallNextHookEx(0, Code, wParam, lParam);
end;

procedure Thookbarcode.Execute;
begin
hookhandle := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, Hinstance, 0);
 while not terminated do
 begin
   sleep(1);
 end;
 UnHookWindowsHookEx(hookhandle);
end;

Open in new window

0
Slinkkkk
Asked:
Slinkkkk
  • 2
  • 2
3 Solutions
 
ThommyCommented:
0
 
MerijnBSr. Software EngineerCommented:
Do you use TThread? It's possible the hook needs a message loop to work correctly.
What else is in this thread?
0
 
ThommyCommented:
Probably that's the point!!!

If you call a hook in the context of a thread, the thread that installed the hook must have a message loop.

For how to implement a message loop for a thread, have a look at Unit HookThread
0
 
MerijnBSr. Software EngineerCommented:
Alternatively you can add such a method to your TThread descendant:

procedure _TThread.MessageLoop;
var Msg: TMsg;
begin
 // If GetMessage retrieves the WM_QUIT, the return value is FALSE and the message loop is broken.

 while GetMessage(Msg, 0, 0, 0) do
 begin
  TranslateMessage(Msg);
  DispatchMessage(Msg)
 end;

 inherited Terminate();
end;

Open in new window


and call MessageLoop() in your execute method:
procedure _TThread.Execute;
begin
 // do init stuff

 MessageLoop();

 // do deinit stuff
end;

Open in new window


You'll have to keep in mind that your thread now isn't a sync function anymore, hence my question what else you are doing in your thread.
0
 
Sinisa VukCommented:
Why do you need threading? It can be done without thread. All hooking stuff should be placed in external dll. Hooking procedure must take callback procedure - which is working like event procedure.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now