c ++ win32使用DirectInput模拟Keypress

时间:2011-09-06 13:09:53

标签: c++ c winapi directinput

如何使用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 

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

1 个答案:

答案 0 :(得分:6)

<强> SendInput

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

简而言之,使用SendInput和扫描码,DirectInput将响应。

<强>拦截

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

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

the github page的评论表明它在Windows 8中尚未发挥作用。

使用它需要您自担风险。