无论 vb.net 中的活动应用程序如何,读取 keydown

发布于 2024-08-23 08:40:31 字数 324 浏览 9 评论 0原文

我之前问过一个关于 vb.net 中的 keyhooks 的问题。

我当前的问题是这样的:

我创建了一个程序,只要同时按下某组键,该程序就应该执行特定的操作。该程序必须能够在后台运行,或者在系统托盘中运行。本质上,这应该像表单上的 KeyDown 事件一样工作,只不过本例中的表单就是一切。

我不确定是否有办法直接从 .net API 中执行此操作,但如果有的话我肯定还没有找到。

I asked a question earlier about keyhooks in vb.net.

My current issue is such:

I have created a program which should perform a certain action whenever a certain group of keys is pressed at the same time. The program must be able to run in the background, or in the system tray or something. Essentially, this should work like the KeyDown event on a form, except the form in this case is everything.

I'm not certain if there is a way to do this directly from within the .net API, but if there is I certainly have not found it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2024-08-30 08:40:31

这不需要键盘挂钩,您需要注册一个热键。实施起来更容易,对系统资源的要求也低得多。这是一个示例,如果表单被最小化,它会将表单恢复到前台。请注意,您可以注册多个密钥:

Imports System.Runtime.InteropServices
Imports System.ComponentModel

Public Class Form1
  Private Const cHotKeyId As Integer = 0

  Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    '--- Register Ctrl + Shift + U as a hot key
    If Not RegisterHotKey(Me.Handle, cHotKeyId, MOD_CONTROL + MOD_SHIFT, Keys.U) Then
      Throw New Win32Exception()
    End If
    MyBase.OnLoad(e)
  End Sub

  Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
    UnregisterHotKey(Me.Handle, cHotKeyId)
    MyBase.OnFormClosing(e)
  End Sub

  Protected Overrides Sub WndProc(ByRef m As Message)
    Console.WriteLine(m.ToString())
    If (m.Msg = WM_HOTKEY AndAlso m.WParam = CType(cHotKeyId, IntPtr)) Then
      Me.Visible = True
      If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
      SetForegroundWindow(Me.Handle)
    End If
    MyBase.WndProc(m)
  End Sub

  '--- P/Invoke declarations
  Private Const WM_HOTKEY As Integer = &H312
  Private Const MOD_ALT As Integer = &H1
  Private Const MOD_CONTROL As Integer = &H2
  Private Const MOD_SHIFT As Integer = &H4
  Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Boolean
  Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
  Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean

End Class

That doesn't require a keyboard hook, you'll want to register a hot key. Much easier to implement and much less demanding of system resources. Here's an example, it restores the form to the foreground if it was minimized. Note that you can register more than one key:

Imports System.Runtime.InteropServices
Imports System.ComponentModel

Public Class Form1
  Private Const cHotKeyId As Integer = 0

  Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    '--- Register Ctrl + Shift + U as a hot key
    If Not RegisterHotKey(Me.Handle, cHotKeyId, MOD_CONTROL + MOD_SHIFT, Keys.U) Then
      Throw New Win32Exception()
    End If
    MyBase.OnLoad(e)
  End Sub

  Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
    UnregisterHotKey(Me.Handle, cHotKeyId)
    MyBase.OnFormClosing(e)
  End Sub

  Protected Overrides Sub WndProc(ByRef m As Message)
    Console.WriteLine(m.ToString())
    If (m.Msg = WM_HOTKEY AndAlso m.WParam = CType(cHotKeyId, IntPtr)) Then
      Me.Visible = True
      If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
      SetForegroundWindow(Me.Handle)
    End If
    MyBase.WndProc(m)
  End Sub

  '--- P/Invoke declarations
  Private Const WM_HOTKEY As Integer = &H312
  Private Const MOD_ALT As Integer = &H1
  Private Const MOD_CONTROL As Integer = &H2
  Private Const MOD_SHIFT As Integer = &H4
  Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Boolean
  Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
  Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文