[2.0] key shortcuts while form is inactive
here's what i would like to know how to do (2005 express edition):
let's suppose that my form, or application, is not active, minimized, or hidden, in the notification area. i want to be able to press a key, f10 for example, and then have it do a command. that command is not important right now, but let's just suppose that it's to show a message box or something simple.
thanks to all who help.
Re: [2.0] key shortcuts while form is inactive
If the application is in focus you could use a keyevent handler. For e.g. lets say an application that is displaying advertisements and when the user clicks ESC it stops, this could be used. e.g.
if (e.KeyValue == 27) // User presses ESC, Key numeration code is 27.
{
MessageBox.Show("You just pressed the Escape key!");
}
The downside to this is that the application must be in focus. To "listen" for a key when the application is not in focus, you need some sort of key hook and simply read when ESC / F1 is pressed. There's a nice example of this in the Code Project site, probably tomorrow when i have more time I would post a link for you.
Jennifer.
Re: [2.0] key shortcuts while form is inactive
and this will work if the form is inactive, or minimized?
Re: [2.0] key shortcuts while form is inactive
The key hook will work if the form is minimized. I faintly remember that I saw a nice example of it on code project. When i get time later I will do some searching. But in the meantime here's 2 links could be of interest to you:
http://search.microsoft.com/results....=keyboard+hook
http://search.microsoft.com/results....-AU&FORM=QBME1
Jenni
Re: [2.0] key shortcuts while form is inactive
http://www.codeproject.com/csharp/globalhook.asp
http://weblogs.asp.net/jdanforth/arc...21/454219.aspx
http://blogs.msdn.com/toub/archive/2...03/589423.aspx
Just to share, using keyboard hooks is not a good idea unless you have a valid reason for it. Also, try to use shortcuts which are otherwise not system specific.
For example, your application pop ups a message box on pressing CTRL+G. If your application is inactive and you have Notepad open (active I mean), then pressing CTRL+G could be problematic because it is a shortcut in Notepad also.
Hope it helps you.