5

如何使用 DirectInput 模拟按键?我目前有初始化(但我不确定它是否好):

#include <dinput.h>

#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

LPDIRECTINPUT8 din;    // the pointer to our DirectInput interface
LPDIRECTINPUTDEVICE8 dinkeyboard;    // the pointer to the keyboard device
BYTE keystate[256];    // the storage for the key-information

void initDInput(HINSTANCE hInstance, HWND hWnd);    // sets up and initializes DirectInput
void detect_input(void);    // gets the current input state
void cleanDInput(void);    // closes DirectInput and releases memory 

那么有人可以告诉我如何模拟例如在游戏中按下左箭头键吗?

4

1 回答 1

9

发送输入

SendInput可让您模拟按键。您可以选择使用虚拟键码或扫描码来识别键(请参阅KEYBDINPUT.dwFlags)。显然,DirectInput 会忽略虚拟键码,但会处理扫描码。

简而言之,将 SendInput 与扫描码一起使用,DirectInput 会响应。

拦截

Interception工具包使用内核模式驱动程序和一些用户模式帮助函数来模拟按键。

存在一些安全隐患。由于它是一个闭源内核模式驱动程序,您需要完全信任作者。即使作者完全值得信赖,驱动程序也允许监视和创建输入,因此它打开了一个很大的安全漏洞:任何非特权软件都可以使用它来嗅探,例如提升密码。它还可以阻止键盘输入,包括 CTRL+ALT+DEL。

github 页面上的评论表明它还不能在 Windows 8 中运行。

需要您自担风险使用它。

于 2013-03-03T13:46:34.987 回答