c++ win32 Simulate Keypress with DirectInput
Asked Answered
W

1

5

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?

Wernher answered 6/9, 2011 at 13:9 Comment(7)
Do I understand this right: you want to simulate keyboard and mouse inputs for a game that is not yours?Craquelure
Yeah right... like a bind. I mean for example if you press Y it should do the same thing when I press Shift+Q on the keyboard. KeyPress() and keybd_event() doesn't work, that is why I need to use DirectInput.Wernher
DirectInput is deprecated and so is keybd_event(). Have you tried using SendInput()? Although I can imagine that the problem is not only sending input but also receiving it, when another program has the focus.Craquelure
Oh sorry under the "KeyPress()" in my previous comment I wanted to say SendInput(). The input is sent, for example it can write to the char of the game, but in the main screen nothing happen.Wernher
The answer to the question as asked is "You cannot generate input with DirectInput. DirectInput is for receiving input, not injecting it."Physiotherapy
This is an old thread but you might find it interesting to read the discussion we had on this one.Blackleg
@RaymondChen: Still most of these questions lead to hooking something, and if you want to send input to a program coded with DirectInput, you need to understand directinput in order to hook the respective functions / methods. In that regard, you write a hook function with DirectInput, so youre using DirectInput to generate Input ;)Multiplicity
D
9

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.

Draco answered 3/3, 2013 at 13:46 Comment(1)
that's a nice way, however I found out that sending scancodes with SendInput instead of virtual keys Direct Input can detect the "keypresses" too. It just isn't documented well enough anywhere, before I give the 100+ rep I would kindly ask you to update your answer as there are no other answers.Alemanni

© 2022 - 2024 — McMap. All rights reserved.