Skip to main content
gamedev.net
🔒 Locked

SendKeys using DirectInput

Started by Jacob Roman Aug 15, 2005 at 9:23 PM 7 replies 13.3k views
Original Post
Jacob Roman
Jacob Roman
Is it possible to do something similar to SendKeys only using DirectInput? And if so, how do I do it? For those of you who don't know, SendKeys is like automatic keypresses of whatever text you want without the user touching the keyboard.
Muhammad Haggag
Muhammad Haggag
There's no way to do that. What do you want to do? If you want to simulate the user pressing the keys in your own game, then just call the code that's supposed to run when the user presses the keys.

Jacob Roman
Jacob Roman
Are you sure? I'm thinking that this could be one of the methods of Action Mapping. But in my case, I'm gonna be using it for a non game related purpose. Might come in handy for different things. Does anyone else agree or can it be possible?
Muhammad Haggag
Muhammad Haggag
Quote:
Original post by jacklin
You can use SendInput().

As far as I know, this won't work with DirectInput, because it communicates directly with the driver.

jacklin
jacklin
SendInput() injects at a lower level than DirectInput, so the input that you send using SendInput() will be picked up by your DirectInput devices.
Muhammad Haggag
Muhammad Haggag
Quote:
Original post by jacklin
SendInput() injects at a lower level than DirectInput, so the input that you send using SendInput() will be picked up by your DirectInput devices.

Thanks for the information, this is news to me [smile]

Spasojevic
Spasojevic
I have a function:
//******************************************************
static void SendString(LPCTSTR str)
{
INPUT inp[2];
memset(inp,0,sizeof(INPUT));
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.dwFlags = KEYEVENTF_UNICODE; // to avoid shift, etc.
inp[1] = inp[0];
inp[1].ki.dwFlags |= KEYEVENTF_KEYUP;

for (LPCTSTR p=str; *p; p++) {
inp[0].ki.wScan = inp[1].ki.wScan = *p;
SendInput(2, inp, sizeof(INPUT));
}
}
//********************************************************

I use Keyboard_2003 sample from DX 9 SDK (June 2005) to test it. When I send a string like "a" nothing seems to happen. Am I doing something wrong? It works with apps that don't use DInput.
Spasojevic
Spasojevic
SendInput function is the right answer. Correct way to use it with DInput is something like this:
//*********************************************************
// Sets a button to down state
static void SetInputDX(WORD input_code_set)
{
INPUT inp[1];
memset(inp,0,sizeof(INPUT));
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.wScan = input_code_set;
SendInput(1, inp, sizeof(INPUT));

}
//*********************************************************
// Sets a button to up state
static void ReleaseInputDX(WORD input_code_release)
{
INPUT inp[1];
memset(inp,0,sizeof(INPUT));
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.dwFlags |= KEYEVENTF_KEYUP;
inp[0].ki.wScan = input_code_release;
SendInput(1, inp, sizeof(INPUT));

}

For DInput to see these codes properly you should pass DInput constants like:
DIKEYBOARD_A but only first two bytes (letter "a" is 0x041e , and so on).
I'we tried this, it realy works.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.