Solved

Detect hotkey from anywhere

Posted on 2004-09-29
10
743 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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