Solved

Detect hotkey from anywhere

Posted on 2004-09-29
10
727 Views
Last Modified: 2012-05-05
Hi,

How would I be able to detect if a certain key combination has been pressed from anywhere in Windows?  I'm building an app that does a screen dump (and adds a fair bit of info to the image at the same time), but I want to do it when I press a hot key - say Alt-F12 or something.

Stu
0
Comment
Question by:SJohnson
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 4

Assisted Solution

by:Evarest
Evarest earned 75 total points
ID: 12185816
It's not that difficult to create a global hotkey, and I can give you the code for that, but why do difficult if life can be that much easier?

Just download the following component:

http://downloads.delphi32.com/2726/5308/hotkeymg.zip

And see for yourself that it works by reviewing the included EXE demo app...

By examining the easy to use demo, you can learn how to set the Hotkey. Essentially, it comes down to:

  HotKeyManager1.ClearHotKeys;
  HotKeyManager1.AddHotKey(TextToShortCut('Alt+F12'));

in your case :-)

Good luck!
Evarest
0
 
LVL 6

Expert Comment

by:gwalkeriq
ID: 12185896
Well, assuming you mean that you don't expect Alt-F12 to launch your screen-dump app itself, the magic API call you are looking for is SetWindowsHookEx

HHOOK SetWindowsHookEx(

    int idHook,      // type of hook to install
    HOOKPROC lpfn,      // address of hook procedure
    HINSTANCE hMod,      // handle of application instance
    DWORD dwThreadId       // identity of thread to install hook for
   )

Here is a nice example that hooks to the printscreen function
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21056779.html

This is one that explains capturing keystrokes globally from your delphi app
http://delphi.about.com/gi/dynamic/offsite.htm?site=http://www.q3.nu/trucomania/truco.cgi%3F408%26ing

It' is pretty easy to find more info if you include SetWindowsHookEX and WH_KEYBOARD in your search criteria.
0
 
LVL 5

Assisted Solution

by:Hypoviax
Hypoviax earned 75 total points
ID: 12187040
There is a simple but effective hotkey control component allowing a range of hotkeys for your application. You can get the JEDI JVCL component library from:

http://sourceforge.net/project/showfiles.php?group_id=45786

When you install the package the component is located under the JvWin32 tab - the component will be called JvApplicationHotKey.

It is very simple to use, simply select a hotkey combination and then insert your code in the OnHotKey event - the event that occurs when the user presses that key combination.

Hope this helps,

Hypoviax
0
 
LVL 17

Accepted Solution

by:
geobul earned 150 total points
ID: 12187580
Hi,

Add THotkey on a form, set its Hotkey to 'Alt + F12' and try the following.

Regards, Geo

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  ComCtrls,
  menus; // <- manually added also

type
  TForm1 = class(TForm)
    HotKey1: THotKey;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    procedure HotkeyProc(var Msg: TWmHotkey); message WM_HOTKEY;
  public
    { Public declarations }
    procedure RegisterShortCut(HKey: TShortCut);
    procedure UnRegisterShortCut;
  end;

var
  Form1: TForm1;
  Hotkey: integer;

implementation

{$R *.DFM}

procedure TForm1.RegisterShortCut(HKey: TShortCut);
var
  fuModifiers : Cardinal;
  Key: Word;
  Shift: TShiftState;
begin
  fuModifiers := 0;

  ShortCutToKey(HKey, Key, Shift);

  if  ssCtrl in Shift then
    fuModifiers := MOD_CONTROL;
  if ssShift in Shift then
    fuModifiers := fuModifiers or MOD_SHIFT;  
  if ssAlt in Shift then
    fuModifiers := fuModifiers or MOD_ALT;  

  RegisterHotKey(Handle,HotKey,fuModifiers, Key);
end;

procedure TForm1.UnRegisterShortCut;
begin
  UnRegisterHotkey(Handle, HotKey);
  GlobalDeleteAtom(HotKey);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  HotKey := GlobalAddAtom(PChar('GeobulShortcut'));
  RegisterShortCut(Hotkey1.HotKey);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnRegisterShortCut;
end;

procedure TForm1.HotkeyProc(var Msg: TWmHotkey);  
begin
  if MSG.HotKey = Hotkey then begin
    showmessage('HotKey pressed');  
    //do something
  end;
end;

end.
0
 
LVL 1

Author Comment

by:SJohnson
ID: 12243695
Thanks guys!  Sorry for taking so long to reply.  I've been so busy!
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:SJohnson
ID: 12243813
Thanks heaps, geobul.  Your code worked perfectly.  It's amazing how simple things can be when you know how :)

Cheers mate!

Stu.
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12243961
Thanks and best regards,

Hypoviax
0
 
LVL 17

Expert Comment

by:geobul
ID: 12246187
The pleasure was mine, Stu. I prefer code under control instead of third-party components where possible :-)
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12246561
I agree with you too on that aspect geobul, code under control is often better than components since you actually learn something too. As a note, my suggestion is an open source library so Stu, if you want to learn how that hotkey component works all you have to do is examine the source!

Regards,

Hypoviax
0
 
LVL 4

Expert Comment

by:Evarest
ID: 12247367
"I agree with you too on that aspect geobul",

I partly agree with that statement. Code is good, of course, and it'll learn you a lot. However, as many Delphi components are open source, you can learn from them anyway. Also, many components are quite good at what they do (at least if you choose from the better packages). As not everyone can be good at every aspect of delphi coding, it's often better to see for yourself in the source code of components which have been written by people who know that aspect better than you do...

To summarize: code where possible, but even better is to use the Object Oriented aspect of Delphi and rely (partly) on 3rd party components.

For myself: I have a core library of (very) good open source components which i often use in my projects. This will save me a lot of time both in writing these components and debugging. If necessary I partly rewrite the components I use (if possible wrt. the license agreement), and reuse them in my projects.

Just my idea of OO programming :-)

Cheers!
Evarest
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now