Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create and handle a system wide shortcut or hotkey Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Jun-03
Category
Win API
Language
Delphi 2.x
Views
84
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jim McKeeth 

How to create and handle a system wide shortcut or hotkey (one that is handled 
beyond the application)

Answer:
1   
2   {**********************************************************
3   
4     Copyright © by Jim McKeeth
5     Licensed under LGPL
6     ( http://www.gnu.org/licenses/licenses.html#LGPL )
7   
8   
9     Demo of creating a system wide hotkey
10    or shortcut
11  
12   This was written in Delphi 7,
13   but should work in most other versions
14   (but obviously not Kylix)
15  
16    You need a form with
17    1) a THotKey named HotKey1
18    2) a TCheckBox named CheckBox1
19  
20    To demo
21    1) Change the hotkey in the value
22    2) Check the box
23    3) Minimize the application
24    4) Press the hot key
25    5) Be impressed
26  
27  **********************************************************}
28  
29  unit SystemHotKeyUnit1;
30  
31  interface
32  
33  uses
34    Windows, Messages, SysUtils, Classes, Graphics,
35    Controls, Forms, StdCtrls, ComCtrls, Dialogs,
36    // Menus need to be added for calls in the code
37    Menus;
38  
39  type
40    TForm1 = class(TForm)
41      HotKey1: THotKey;
42      CheckBox1: TCheckBox;
43      procedure FormCreate(Sender: TObject);
44      procedure CheckBox1Click(Sender: TObject);
45      procedure FormDestroy(Sender: TObject);
46    private
47      { Private declarations }
48    protected
49      // Handle the global hot key messages when they are sent to the window
50      procedure HotyKeyMsg(var msg: TMessage); message WM_HOTKEY;
51    public
52      { Public declarations }
53    end;
54  
55  var
56    Form1: TForm1;
57  
58  implementation
59  
60  {$R *.dfm}
61  
62  var
63    myAtom: integer;
64  
65  function ShiftState2Modifier(const Shift: TShiftState): Word;
66  begin
67    Result := 0;
68    if ssShift in Shift then
69      Result := Result or MOD_SHIFT;
70    if ssAlt in Shift then
71      Result := Result or MOD_ALT;
72    if ssCtrl in Shift then
73      Result := Result or MOD_CONTROL;
74  end;
75  
76  function GetShortCutKey(ShortCut: TShortCut): Word;
77  var
78    shift: TShiftState;
79  begin
80    ShortCutToKey(ShortCut, Result, shift); // call in Menus!
81  end;
82  
83  function GetShortCutModifier(ShortCut: TShortCut): Word;
84  var
85    key: Word;
86    shift: TShiftState;
87  begin
88    ShortCutToKey(ShortCut, key, shift); // call in Menus!
89    Result := ShiftState2Modifier(shift);
90  end;
91  
92  function RegisterHotShortCut(const h: THandle; const Atom: integer; const ShortCut:
93    TShortCut): Boolean;
94  var
95    key: Word;
96    Shift: TShiftState;
97  begin
98    UnregisterHotKey(h, Atom); // call in Windows
99    ShortCutToKey(ShortCut, key, shift);
100   Result := RegisterHotKey(h, Atom, ShiftState2Modifier(Shift), key);
101 end;
102 
103 procedure TForm1.FormCreate(Sender: TObject);
104 begin
105   // you need to type cast it as a pChar if you are using a string
106   myAtom := GlobalAddAtom(pchar('HotKeyDemo'));
107 end;
108 
109 procedure TForm1.FormDestroy(Sender: TObject);
110 begin
111   UnregisterHotKey(Handle, myAtom);
112   GlobalDeleteAtom(myAtom);
113 end;
114 
115 procedure TForm1.CheckBox1Click(Sender: TObject);
116 begin
117   if CheckBox1.Checked then
118     RegisterHotShortCut(Handle, myAtom, HotKey1.HotKey)
119   else
120     UnregisterHotKey(Handle, myAtom);
121 end;
122 
123 procedure TForm1.HotyKeyMsg(var msg: TMessage);
124 begin
125   if (msg.LParamLo = GetShortCutModifier(HotKey1.HotKey))
126     and (msg.LParamHi = GetShortCutKey(HotKey1.HotKey)) then
127   begin
128     Application.BringToFront;
129     Showmessage('Hey, now that is a system wide hot key!')
130   end;
131 end;
132 
133 end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC