0

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

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

找不到检测用户何时释放密钥的方法

以及如何根据按键自动检测按键和发送键?

谢谢你的帮助

4

2 回答 2

1

首先,为了获得类似的工作,您需要KeyPreview = True为表单设置。接下来,您可能想要使用 KeyDown 事件。捕获任何键(Keys.F1),只要键按下,您就会收到 keydown 事件以发送垃圾邮件 F1。

我们今天有一个关于键盘陷阱的特别节目。 也看到这个。除此之外,您将不得不自己尝试其中的一些。你就是这样学习的。当您遇到困难时,请发布您的代码。

您还可以在此处搜索标记为 VB NET/6 的 60,059 个问题以查找示例。

于 2013-10-14T20:10:46.377 回答
0

我找到了方法

在这里我将发布代码,所以如果有人在寻找它,这就是答案

Imports System.Windows.Forms
Imports MouseKeyboardActivityMonitor
Imports MouseKeyboardActivityMonitor.WinApi
Imports System.Runtime.InteropServices.DllImportAttribute
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System

Public Class Form1
Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As Keys) As Integer

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As NativeMethods.INPUT, ByVal cbSize As Integer) As Integer
End Function
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 Sub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, ByVal key As Keys)
    Dim input As NativeMethods.INPUT
    Dim ki As NativeMethods.KEYBDINPUT
    input.type = NativeMethods.InputType.Keyboard
    input.u.ki = ki
    input.u.ki.wVk = Convert.ToInt16(key)
    input.u.ki.wScan = 0
    input.u.ki.time = 0
    input.u.ki.dwFlags = flags
    input.u.ki.dwExtraInfo = IntPtr.Zero
    Dim cbSize As Integer = Marshal.SizeOf(GetType(NativeMethods.INPUT))
    Dim result As Integer = NativeMethods.SendInput(1, input, cbSize)
    If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
End Sub

Private WithEvents myKeyboardHookManager As KeyboardHookListener
Dim hookEnabled As Boolean
Public Sub New()
    InitializeComponent()
    myKeyboardHookManager = New KeyboardHookListener(New GlobalHooker())
    myKeyboardHookManager.
于 2013-10-15T00:04:55.190 回答