Hotkey functionality

  • Thread starter Thread starter Guest
  • Start date Start date
Don't be a fool and don't pay for 3rd party components.

There is a 'poor man' version of what you need.

protected override bool ProcessKeyPreview(ref
System.Windows.Forms.Message msg)
{
if (msg.Msg == WM_KEYDOWN)
{
if ((Int32)msg.WParam == (Int32)Keys.Enter)
return base.ProcessKeyPreview(ref msg);

if ((Int32)msg.WParam == (Int32)_keyNew)
{
NewOrder(null);
return true;
}


if ((Int32)msg.WParam == (Int32)_keyEdit)
{
Edit();
return true;
}

and so on.
2 problems

- not every key can be intercepted (for example PrintScreen)
- could not find a way to stop the keystroke to be passed to an input
control inside ProcessKeyPreview so my 'hot keys' are just F keys and
Ctrl-PrintScreen.

MH
 
D.Coy said:
I need hotkey functionality inside my Windows forms application. There are
going to be more than 20-30 commands some of which will even involve
function
keys (E.g. CTRL+F2). I came across the following article from 2002
explaining
the technique:
http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows_Messages/Hot_Key_Form/article.asp

Is there a better way of doing this in .NET 2005 rather than having to
directly make Win32 calls?

Add a mainmenu component, add menu items and assign the hotkeys to them.
Note that the menu items do not necessarily need to be visible for the
hotkeys to work.
 
Thanks everyone for the responses. I was wondering if .NET 2.0 gave an easier
way to react to hotkeys and based on the responses it doesn't appear that
there is one. The technique described in the vbAccelerator article is quite
clean to use so I think I would go with that (just need to inherit from
HotKeyForm and handle the hotkey event). Otherwise, the mainmenu technique
described by Mr. Wagner appears to be the easiest to implement.
 
Back
Top