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
Solved

delphi SetWindowsHookEx inside thread

Posted on 2013-12-03
5
1,078 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
  • 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 26

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 313
Virtuailstring tree compare node issue 14 125
Delphi application Soap connection 5 118
shape, triangle, dbctrlgrid 3 17
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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