I want to be able to detect when the user presses F10 or Ctrl+F10 outside of my program, and upon receiving the key press, it will send text to whatever they currently have selected (e.g. a text box). How can this be accomplished in the simplest way?
-
possible duplicate of [listen for a key when the application is not focused](http://stackoverflow.com/questions/5065817/listen-for-a-key-when-the-application-is-not-focused) – CodesInChaos Mar 07 '11 at 10:22
3 Answers
Use RegisterHotkey and not a keyboard hook to get a global hotkey.
See one of my older answers on almost the same question: listen for a key when the application is not focused

- 1
- 1

- 106,488
- 23
- 218
- 262
-
Yes, this is a better solution than mine. I'd forgotten all about that function. – Cody Gray - on strike Mar 07 '11 at 10:21
The first problem here is that the F10 key is already a reserved key for Windows applications. When the user presses it, the active application selects the first menu in its menu bar. This provides a convenient way for users to activate menus without having to use their mouse. Your proposal to hijack that functionality globally is inconsiderate and inappropriate. I suggest choosing a different shortcut key, preferably one that doesn't already have a standard meaning.
Then, you will have to install a low-level keyboard hook (the only type of hook supported by .NET apps). This is the only way to capture keyboard events that occur outside of your application. Since you want to detect keypresses that occur when your application is not in the foreground, you'll need a global hook.
There's an excellent sample available here on Stephen Toub's blog. It happens to be written in C#, but converting to VB.NET is trivial. If you prefer a full base of existing code that you can simply add to your project, you can try this sample, but note that it includes a lot of additional functionality you won't need.

- 239,200
- 50
- 490
- 574
-
The application I am trying to create won't be using any menus. I basically just want to create a macro for personal use that uses the function keys as I rarely use them compared to other keys. – cs475x Mar 07 '11 at 07:59
-
@cs475x: As I understand your question, it is irrelevant whether or not *your* application has menus. If you want to capture presses of the F10 key when *other* applications are active, you'll be interfering with the user's ability to drop down the menus of *that* application without triggering *your* application's functionality. Again, my suggestion is to simply pick a different function key. There are 10 of them that are hardly used for anything (F1 is help, F10 is menus). But if it's just a personal application and you never use F10, I suppose it doesn't matter. Until your mouse breaks... – Cody Gray - on strike Mar 07 '11 at 08:03
-
-
@David: That's true, but only in Windows Explorer. Those aren't necessarily standard meanings reserved across *all* applications. You'll still have potential conflicts with particular applications that reserve meanings for F-keys. – Cody Gray - on strike Mar 07 '11 at 10:20
-
F2 is rename in Excel. F5 is refresh in all web browsers etc. etc. The function keys are heavily used by my development IDEs. My app uses CTRL+ALT+arrow key to rotate a 3D view. Some Intel graphics software registers system shortcuts with those key presses and rotates the screen! My clients blame me for this! I loath and despise global system-wide hotkeys! – David Heffernan Mar 07 '11 at 10:28
You can check that in the keypress event:
if not e.control and e.keycode = keys.F10 then
<Statements>
elseif e.keycode = keys.F10 then
<Statements>
end if

- 32,008
- 25
- 109
- 114

- 1