Board index » delphi » Capturing keys without application having focus

Capturing keys without application having focus

Hi,

Just wondering if anyone knew how to capture keys pressed when the
application doesn't have focus. For example :

* In PGPDisk, pressing a hotkey (e.g. Ctrl+F12) unmounts all PGP
disks, no matter whether or not PGPDisk currently has focus
(obviously, it has to be loaded though)

* You can set a hotkey combination in the windows explorer for
starting an application. Maybe this is a special case, but even if
explorer doesn't have focus, pressing the hotkey combination starts
the program

Just wondering if anyone knew how to do the same thing in Delphi?
Maybe respond to a windows message? Maybe there's some sort of order
of receiving keys and I need to shift the application to the top of
that list?

Thanks,

Dave Hughes.
EMail : hughe...@cs.man.ac.uk

 

Re:Capturing keys without application having focus


Here is the code of a unit of a Dll that can intercept key :
(you have just to create a main program that call the Start and the Stop
procedures, don't forget to export thes procedures)

unit UHookDll;

interface

uses Windows, Messages, SysUtils;

procedure Start;
procedure Stop;

function GetMsgProc(code: Integer;wParam: wParam;lParam: lParam):
lResult;stdcall;

var
  GetMsgHook: hHook;

implementation

procedure Start(List: TStrList);
begin
  GetMsgHook:=SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,hInstance,0);
end;

procedure Stop;
begin
  UnHookWindowsHookEx(GetMsgHook);
end;

function GetMsgProc(code: Integer;wParam: wParam;lParam: lParam):
lResult;stdcall;
var ptMsg: pMsg;
    Buf: PChar;
    PressCar: String;
begin
  if code<0 then
    Result:=CallNextHookEx(GetMsgHook,code,wParam,lParam)
  else
  begin
    ptmsg:=pMsg(lParam);
    if ptmsg^.message=WM_KEYDOWN then
    begin
      Buf:=StrAlloc(255);
      GetKeyNameText(ptmsg^.lParam,Buf,255);
      PressCar:=Trim(Buf);

// What you will here !!!

    end;
    Result:=0;
  end;
end;

end.

The PressCar contains the name of the key. If it is the letter, it is normal
(a,b,c,d ...) but for the control
key, the PressCar contains Ctrl, Shift, Esc ...
Dave Hughes <hughe...@cs.man.ac.uk> a crit dans le message :
37918414.102371...@news.free-online.net...

Quote
> Just wondering if anyone knew how to capture keys pressed when the
> application doesn't have focus.

Re:Capturing keys without application having focus


In article <37918414.102371...@news.free-online.net>, Dave Hughes
<hughe...@cs.man.ac.uk> writes
Quote
>Hi,

>Just wondering if anyone knew how to capture keys pressed when the
>application doesn't have focus. For example :

Dave,

There is a paper on the Borland/Inprise web site which demonstrates how
to implement system-wide mouse and keyboard hooks. It contains a demo
application and DLL (with full source) which implements a basic macro
recorder.

You can find it at:

http://www.borland.com/delphi/papers/winAPI/part2.html

HTH
--
Steve Turner
Leeds, England
(Remove NOMORESPAM from return address to reply via e-mail)

Re:Capturing keys without application having focus


Hi,

Quote
Dave Hughes <hughe...@cs.man.ac.uk> wrote:
> Hi,

> Just wondering if anyone knew how to capture keys pressed when the
> application doesn't have focus. For example :

> * In PGPDisk, pressing a hotkey (e.g. Ctrl+F12) unmounts all PGP
> disks, no matter whether or not PGPDisk currently has focus
> (obviously, it has to be loaded though) [...]

If its only about hotkeys as in your examples, I would not use keyboard
hooks but rather the API function RegisterHotkey (and UnRegisterHotkey,
respectively). You give it a hotkey, a window handle and an ID number, and
windows sends an WM_HOTKEY message to the specified window whenever the
hotkey is pressed.

HTH, Thorsten

Re:Capturing keys without application having focus


Hi everyone,

Thanks very much for all your help - I've eventually settled on the
RegisterHotKeys solution which works like a dream (especially when
combined with the various Shortcut Key functions for converting hotkeys
into strings and the other bits normally only used for menus - e.g. the
hotkey editor component).

Apologies if I'm breaking any usenet rules for posting a thank you!

Thanks again,

Dave.

--
Dave Hughes (d...@waveform.free-online.co.uk)
"What do you want?"...
"Yes, but what do you REALLY want?"...
"And then what do you want?" - Morden, B5

Other Threads