Connecting Tech Pros Worldwide Help | Site Map

Multiple hotkeys [c#]

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#1: Nov 1 '08
In a windows form application, I register a hotkey to be used throughout the application. However, I would also like a second hotkey to be registered and used, but this isn't too clear as to how to accomplish.

When the form loads, I do this (bearing in mind I have imported the DLLs previously)
Expand|Select|Wrap|Line Numbers
  1. RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 2, (int)'A');
  2.  
Then I use the WndProc method to catch the hotkey. Like so,
Expand|Select|Wrap|Line Numbers
  1. protected override void WndProc(ref Message m)
  2.         {
  3.             if (m.Msg == 0x0312)
  4.                 CopyToClip();
  5.             base.WndProc(ref m);
  6.         }
This then runs the method CopyToClip().

Any ideas on how I can add another hotkey?
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: Nov 2 '08

re: Multiple hotkeys [c#]


Let me ask you this...do you need the full Hotkey functionality? Or are you already going to be focused on the form?

If you are focused on the form, you can handle the KeyDown event, and test for several keys.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,752
#3: Nov 2 '08

re: Multiple hotkeys [c#]


Does something like this work?

Expand|Select|Wrap|Line Numbers
  1. if ((m.Msg == 0x0312) || (m.Msg == 0x314) || (m.Msg == 0x0327))
  2. {
  3.      // Do your thang
  4. }
  5. base.WndProc(ref m);
  6.  
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#4: Nov 2 '08

re: Multiple hotkeys [c#]


Quote:

Originally Posted by insertAlias

Let me ask you this...do you need the full Hotkey functionality? Or are you already going to be focused on the form?

If you are focused on the form, you can handle the KeyDown event, and test for several keys.

No, the form doesn't have focus.

Quote:

Originally Posted by tlhintoq

Does something like this work?

Expand|Select|Wrap|Line Numbers
  1. if ((m.Msg == 0x0312) || (m.Msg == 0x314) || (m.Msg == 0x0327))
  2. {
  3.      // Do your thang
  4. }
  5. base.WndProc(ref m);
  6.  

I wouldn't want the hotkeys to do the same thing, but I could seperate that into if/elses. The problem is: how do I know that CTRL + G would be 0x0323* ?

*Guess.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,752
#5: Nov 2 '08

re: Multiple hotkeys [c#]


Quote:

Originally Posted by Markus

No, the form doesn't have focus.



I wouldn't want the hotkeys to do the same thing, but I could seperate that into if/elses. The problem is: how do I know that CTRL + G would be 0x0323* ?

*Guess.

Maybe the best way is to look for the Windows message of key down (0x100) then check what key(s) are pressed.
Helpful article
Reply


Similar .NET Framework bytes