Solved

RegisterHotKeys in Delphi App - have I done it right?

Posted on 2009-04-11
7
1,138 Views
Last Modified: 2012-05-06
I have an application for which I want to implement user definable HotKeys for a number of procedures.  I've used the RegisterHotKeys methods in a procedure that can be called with true to register them, false to unregister.

The HotKey sequences are stored in the applications registry and applied to the HotKeys through the use of an integer array.

I want to be a good neighbor to other applications that may be running, so I wanted my HotKeys active only when my app has focus.  So I call the HotKeys(true) in an application.OnActivate event and turn them off in an Application.OnDeactivate event.

Now, if the user chooses to assign something to. for instance, Control-S,  everything seems to be working properly.  When my app has focus, Control-S does what it is supposed to do, but if WordPad, for instance, is focused, Control-S does the proper file save operation.

So what I've done seems to work, But I made all this up myself and I know from sad experience that just because I can make something work doesn't mean I've done it right.  Is what I've done the best way to do this?  Is there a more proper way?  Am I being a good neighbor?

In the main form class methods section:
 
    procedure AppOnActivate(Sender: TObject);
    procedure AppDeActivate(Sender: TObject);
In public:
   procedure HotKey(onoff: boolean);
in private
    kid4 : Integer;
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
 
 
In OnCreate procedure for Main form
  Application.OnActivate:=AppOnActivate;
  Application.OnDeactivate:=AppDeActivate;
 
Definitions of procedures:
 
procedure T_form.WMHotKey(var Msg: TWMHotKey);
begin
  if Msg.HotKey = kid4 then ToggleInOut;
end;
 
procedure T_form.HotKeys(onoff: Boolean);
begin
if onoff then
 begin
     kid4 := GlobalAddAtom('hk3');
     BK[3]:=RegisterHotKey(Handle, kid4, HK[3,0]+HK[3,1],HK[3,2]);
  end else
  begin
    UnRegisterHotKey(Handle, kid4);
    GlobalDeleteAtom(kid4);
  end;
end;
 
procedure T_form.AppOnActivate(Sender: TObject);
begin
  HotKeys(true);
  Invalidate;
end;
 
procedure T_form.AppDeActivate(Sender: Tobject);
begin
  HotKeys(false);
end;

Open in new window

0
Comment
Question by:DMTrump
[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
  • 4
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
8080_Diver earned 500 total points
ID: 24123590
Man!  I CONGRATULATE you on your professionalism!  What you've done is excellent!
The only thing I can think of to improve on your user friendliness and neighborliness to other applicationswould be to store the user's hot-key settings in a database.
You are absolutely being a good neighbor and the way you are doing this is really cool!
0
 

Author Comment

by:DMTrump
ID: 24123663
Thanks for the compliment - I ought to award you the points just for capitalizing CONGRATULATE - but I will wait a little bit just to make sure someone doesn't come up with a reason to point out that we're both mistaken! <LOL>

55 years ago I was out hunting with a friend.  As we climbed a fence I handed him my 22 and while I was climbing the fence he shot himself in the foot with MY rifle!  It wasn't until I got into programming some 20 years later that I started shooting MYSELF in the foot!  So I just thought I'd check to make sure the gun wasn't loaded.

I considered using a database, but there will only be 20 or so HotKeys and I can store them all in a single string in the Registry and only have to parse the string once into the integer array.
0
 

Author Comment

by:DMTrump
ID: 24123679
8080 Diver,

Great handle - makes me guess that you also are a rather "Senior" programmer!   I used to program embedded systems built on 8080s!

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 37

Expert Comment

by:Geert Gruwez
ID: 24124334
nice, to finish it off, you could create a component from it ...
0
 

Author Comment

by:DMTrump
ID: 24124428
I believe I will do that -  Not terribly high on my todo list - but when I get it done I'll come back to this thread and post it here as a comment.  Since some of my products are programmers tools I've been creating a few things to distribute as freeware to attract attention to my commercial products.  This will be a nice one to add.  Thanks to both of you for confirmation.
0
 

Author Closing Comment

by:DMTrump
ID: 31569218
Thanks for the evaluation.   I hope I've explained it well enough to make this thread useful to others.  Someday soon I'll turn the technique into a component.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24124869
well, supplying a link to your website in your profile would help
0

Featured Post

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!

Question has a verified solution.

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

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

695 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