Solved

delphi SetWindowsHookEx inside thread

Posted on 2013-12-03
5
1,127 Views
Last Modified: 2013-12-05
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
Comment
Question by:Slinkkkk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 39694826
0
 
LVL 19

Assisted Solution

by:MerijnB
MerijnB earned 334 total points
ID: 39694989
Do you use TThread? It's possible the hook needs a message loop to work correctly.
What else is in this thread?
0
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 166 total points
ID: 39695095
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
 
LVL 19

Accepted Solution

by:
MerijnB earned 334 total points
ID: 39695169
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
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 39695275
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question