问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/7320424/c-win32-simulate-keypress-with-directinput