views:

439

answers:

3

How can I bind a key combination to my vb.net application? I know it has SOMETHING to do with the registry, but I have no earthly idea what or how to go about doing this. I want the user to be able to hit those keys when the app is open and have it execute my function, but not while the app is closed.

Thanks for the help!

A: 

It depends on what you are trying to do:

If you have a gui application and you want to handle key events then you can do that in a keydown eventhandler

If you want to do more low-level stuff and have an application that will intercept all key strokes (regardless of whether or not the application has focus/is visible) then you need to use pinvoke to hit the win32 apis. I suggest you read the following: link text

Please let us know what you are trying to do so we can provide better feedback.

Jason Irwin
Intercepting all keystrokes is what I would like to do, yes. What is pinvoke?
Cyclone
Sounds like you want to install a keyboard hook. The best bet is to do a search for something like "vb .net keyboard hook".
Jason Williams
@Cyclone. Pinvoke allows managed code to invoke unmanaged functions residing in dlls. if you want to intercept ALL keystrokes then you will have to do it at an OS - therefore you will need to use pinvoke to call the underlying windows 32 dlls responsible for input.A good introduction to pinvoke can be found here: http://msdn.microsoft.com/en-us/library/aa446536.aspx
Jason Irwin
A: 

If you are using a dialog, then you can put '&' into the text for some controls (buttons, checkboxes, radio buttons, etc) and this will cause Alt plus the next character in the text to be used as an accelerator/shortcut. i.e. "&Open" would activate the Open button if you press Alt+O. "Op&en" would activate if for Alt+e.

Beyond that, as Jason Irwin said, you need to add an event handler to your Form for KeyDown or KeyPress events, and then check if the keypress is the key combination you are interested in. This will only work if the user activates your form (clicks in it to give it the input focus. If they put it behind another window, it will not react to the key presses)

If you don't want to show a form, or want to react to keypresses when you're not the input-focus application, then it gets a bit more complicated (you either need to use a hidden form or a keyboard hook)

edit

OK, it looks like you want a keyboard hook. This looks like a good article.

Jason Williams
Yeah, I want the user to be able to open it without giving the form focus. How can I use a hidden form for this or keyboard hook?
Cyclone
See my edit, above.
Jason Williams
A: 

Using Google, I found this Keyhook example.

I've worked with keyhooks before, in Delphi WIN32, so I am a bit familiar with them. (Just not in C#.) If you add one to a DLL, all Hell might break loose since most virus scanners will recognise this as malware behaviour. (Especially if you use them in the wrong way, causing them to be injected in each and every process that's running on your system.) A keyhook will allow key combinations to be captured from other processes, though.

For a solution without programming requirements: Drop a shortcut for the application on your desktop. Edit it, assign a shortcut, close it. Press shortcut to test...

Workshop Alex
Ugh, the last thing i want is for a virus scanner to think I am distributing malware lol.....i am half tempted to give up now with this!
Cyclone
Just try the shortcut instead. ;-)
Workshop Alex