c++ win32 使用 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
那么有人可以告诉我如何模拟例如在游戏中按下左箭头键吗?
How can I simulate a keypress with DirectInput? I currently have the initialization (but I'm not sure is it good or not):
#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
So can someone show me how to simulate for example the press of the left arrow key in a game?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SendInput
SendInput 可让您模拟按键操作。您可以选择使用虚拟按键代码或扫描代码来识别按键(请参阅KEYBDINPUT.dwFlags)。显然,DirectInput 会忽略虚拟键码,但会处理扫描码。
简而言之,将 SendInput 与扫描码一起使用,DirectInput 将做出响应。
拦截
拦截工具包使用内核模式驱动程序和一些用户模式帮助函数来模拟按键操作。
存在一些安全担忧。由于它是一个闭源内核模式驱动程序,因此您需要完全信任作者。即使作者完全值得信赖,驱动程序也允许监视和创建输入,因此它打开了一个很大的安全漏洞:任何非特权软件都可以使用它来嗅探,例如,提升密码。它还可以阻止键盘输入,包括 CTRL+ALT+DEL。
github 页面上的评论表明它尚未在 Windows 8 中运行。
使用它的风险由您自行承担。
SendInput
SendInput lets you simulate key presses. You have a choice of identifying keys using either virtual key codes or scan codes (see KEYBDINPUT.dwFlags). Apparently, DirectInput ignores virtual key codes but does process scan codes.
In short, use SendInput with scan codes and DirectInput will respond.
Interception
The Interception toolkit simulates key presses with a kernel mode driver and some user mode helper functions.
There are some security worries. Since it's a closed-source kernel mode driver you need to completely trust the author. Even if the author is entirely trustworthy the driver allows input to be monitored, as well as created, so it opens a big security hole: any unprivileged software could use it to sniff, for example, elevation passwords. It can also block keyboard input including CTRL+ALT+DEL.
Comments on the github page suggest it doesn't yet work in Windows 8.
Use it at your own risk.