0

看起来我的老问题已经抛在脑后了

我很难找到一种方法来做到这一点

我希望我的应用程序在按键时做一些事情。例如,当用户按下 F1 时,只要 F1 仍然按下,它就会发送 F1 键,而当用户释放 F1 时,它不会发送 F1 键。

我找不到让我的应用程序在 F1 发布之前一直按 F1 的方法

这是代码

Imports System.Windows.Forms
Imports MouseKeyboardActivityMonitor
Imports MouseKeyboardActivityMonitor.WinApi

Public Class Form1

Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As Keys) As Integer
Public Declare Sub mouse_event Lib "User32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

Private WithEvents myKeyboardHookManager As KeyboardHookListener
Dim hookEnabled As Boolean
Public Sub New()
    InitializeComponent()
    myKeyboardHookManager = New KeyboardHookListener(New GlobalHooker())
    myKeyboardHookManager.Enabled = True
End Sub

Private Sub HookManager_KeyDown(sender As Object, e As KeyEventArgs)
    Select Case e.KeyCode
        Case Keys.F1 To Keys.F9
            SendKeys.Send("{F" + Convert.ToChar(e.KeyValue - 63) + "}")
    End Select
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If hookEnabled Then
        hookEnabled = False
        RemoveHandler myKeyboardHookManager.KeyDown, AddressOf HookManager_KeyDown
    Else
        hookEnabled = True
        AddHandler myKeyboardHookManager.KeyDown, AddressOf HookManager_KeyDown
    End If
End Sub
End Class

此代码工作正常,但只按一次。我想要的是在我释放之前一直按下

谢谢你的帮助

4

0 回答 0