如何模拟/释放键盘键? [英] How do I simulate/release keyboard keys?

查看:167
本文介绍了如何模拟/释放键盘键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果需要满足特定条件,我需要一种模拟键盘按键的方法,我还需要知道当前按下的是模拟按键还是真实按键.这需要在主应用程序之外进行.

I need a way to simulate keyboard keys if a certain condition is met, I also need to know if it's the simulated key that's currently pressed or the real key. This needs to work outside the main application.

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

This is how I would need it to work:

    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.

任何帮助将不胜感激. (这段代码是为了使游戏中的某些事情自动化,这就是为什么它需要在主应用程序之外工作的原因)

Any help would be greatly appreciated. (This code is to automate certain things inside of a game which is why it needs to work outside the main application)

我有一些适当的设置,允许用户设置自己的键,这只是我所需要的一个小例子,它只是我一直在模拟的键盘模拟部分,并确定是否仍然按下了真实键是否.

I have certain things in place to allow the user to set their own key, this was just a small example of what I need, it's just the keyboard simulating part i'm stuck with and determining if the real key is still pressed or not.

推荐答案

很抱歉,等待很长时间...通过

So sorry for the long wait... Simulating keyboard input via Window Messages turned out to be far much more complicated compared to simulating mouse input in the same way.

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

Anyway, I am finally done with it so here's a complete InputHelper class for simulating both mouse and keyboard input virtually via the mouse and keyboard input stream or via Window Messages.

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

Download at GitHub: https://github.com/Visual-Vincent/InputHelper/releases
(source code is too long to be pasted in the answer)

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子类的一些信息:

  • 用于处理和模拟物理键盘输入(即,GetAsyncKeyState()检测到的输入)的方法.
  • Methods for handling and simulating physical keyboard input (i.e. input detected by GetAsyncKeyState()).
  • 用于处理和模拟物理鼠标输入的方法.
  • Methods for handling and simulating physical mouse input.
  • 用于处理和模拟虚拟键盘和鼠标输入(即,GetAsyncKeyState()未检测到的输入)的方法.
  • Methods for handling and simulating virtual keyboard and mouse input (i.e. input that is not detected by GetAsyncKeyState()).

希望这会有所帮助!

这篇关于如何模拟/释放键盘键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆