VB.NET Detecting Keypress While Minimized

后端 未结 3 995
梦谈多话
梦谈多话 2020-12-12 06:38

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

3条回答
  •  悲&欢浪女
    2020-12-12 07:07

    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.

提交回复
热议问题