如何模拟/释放键盘按键?

时间:2017-07-18 00:50:49

标签: vb.net keyboard keystore simulate

如果满足某个条件,我需要一种模拟键盘键的方法,我还需要知道它是当前按下的模拟键还是真实键。这需要在主应用程序之外工作。

这就是我需要它的工作方式:

    Dim UserDefinedKey As Keys = Keys.H
    Do
        If GetAsyncKeyState(UserDefinedKey) Then
            Thread.Sleep(30)
            'release the set key
            Thread.Sleep(30)
            'press/hold the set key once, continue the loop if the real key is still been held.
        End If
    Loop While GetAsyncKeyState(UserDefinedKey) '/ loop while real key is being held
    'Real key is no longer held, release the simulated key press.

非常感谢任何帮助。 (此代码用于自动化游戏中的某些内容,这就是它需要在主应用程序之外工作的原因)

我有一些适当的东西允许用户设置自己的密钥,这只是我需要的一个小例子,它只是键盘模拟部分我坚持并确定是否真正的钥匙是否仍然按下。

2 个答案:

答案 0 :(得分:1)

很抱歉等待漫长的等待...与模拟鼠标相比,通过Window Messages模拟键盘输入结果是 更加复杂 以同样的方式输入

无论如何,我终于完成了它,所以这里有一个 完整的 InputHelper类,用于通过鼠标虚拟地模拟鼠标和键盘输入键盘输入流或通过窗口消息

在GitHub下载: https://github.com/Visual-Vincent/InputHelper/releases
(源代码太长,无法在答案中粘贴)

Dim UserDefinedKey As Keys = Keys.H

'Using a regular While-loop is better since you won't need your If-statement then.
While InputHelper.Keyboard.IsKeyDown(UserDefinedKey)
    Dim ActiveWindow As IntPtr = InputHelper.WindowMessages.GetActiveWindow()
    Thread.Sleep(30)
    InputHelper.WindowMessages.SendKey(ActiveWindow, UserDefinedKey, False) 'False = Key up.
    Thread.Sleep(30)
    InputHelper.WindowMessages.SendKey(ActiveWindow, UserDefinedKey, True) 'True = Key down.
End While

关于InputHelper子类的一些信息:

InputHelper.Keyboard

  • 处理和模拟物理键盘输入的方法(即GetAsyncKeyState()输入检测到)。

InputHelper.Mouse

  • 处理和模拟物理鼠标输入的方法。

InputHelper.WindowMessages

  • 处理和模拟虚拟键盘和鼠标输入的方法(即GetAsyncKeyState()输入未检测到的 )。

希望这有帮助!

答案 1 :(得分:0)

您需要Windows API挂钩(初学者没有)或第MouseKeyHook

等第三方库
相关问题