Solved

delphi SetWindowsHookEx inside thread

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 121
Magic Software info 18 131
tidtcpserver connection lost handle 2 86
DBGrid or StringGrid ? 6 89
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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