Page 1 of 1

[C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 10 Oct 2017, 10:06
by Sickae
So I'm trying to generate a key with the SendInput method in C++. It works very well until it comes to the specific game I want to use it in. It does the job in the game's chat but not in the game itself. I am using AHK for the script I want currently, but I wish to change to a native language to get more control over it. Looked up how AHK does it and apparently it uses the same method as I do, so I'm a bit confused. Why does AHK's method works without any problem and mine doesn't? I have tried many many different methods, but I just can't make it work.
Here's the code:

Code: Select all

void sendKey(char c) {
	INPUT key;
	key.type = INPUT_KEYBOARD;
	key.ki.wVk = 0;
	key.ki.wScan = VkKeyScan(c);
	key.ki.dwFlags = KEYEVENTF_UNICODE;
	key.ki.time = 0;
	key.ki.dwExtraInfo = 0;
	SendInput(1, &key, sizeof(INPUT));
	key.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
	Sleep(100);
	SendInput(1, &key, sizeof(INPUT));
}
And here's my AHK script code that I want to implement in C++ (just a part, no sense copying the whole code):

Code: Select all

...
while BMActive = 1
{
	...
	Send yx
	...
}
...

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 10 Oct 2017, 13:29
by Helgef
You probably shouldn't use the unicode flag in a game. :beer:

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 10 Oct 2017, 14:12
by Sickae
AHK also uses unicode flag. And trust me, my first try was with scancode, but that doesn't even work in the game's chat. Changed to unicode only after I looked up the AHK source code.
Here it is:

Code: Select all

INPUT u_input[2];

	u_input[0].type = INPUT_KEYBOARD;
	u_input[0].ki.wVk = 0;
	u_input[0].ki.wScan = aChar;
	u_input[0].ki.dwFlags = KEYEVENTF_UNICODE;
	u_input[0].ki.time = 0;
	// L25: Set dwExtraInfo to ensure AutoHotkey ignores the event; otherwise it may trigger a SCxxx hotkey (where xxx is u_code).
	u_input[0].ki.dwExtraInfo = KEY_IGNORE_LEVEL(g->SendLevel);
	
	u_input[1].type = INPUT_KEYBOARD;
	u_input[1].ki.wVk = 0;
	u_input[1].ki.wScan = aChar;
	u_input[1].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
	u_input[1].ki.time = 0;
	u_input[1].ki.dwExtraInfo = KEY_IGNORE_LEVEL(g->SendLevel);

	SendInput(2, u_input, sizeof(INPUT));

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 10 Oct 2017, 14:39
by Helgef
Are you certain Send yx end up in that code :angel: . Either verify which part of the source code is relevant for the working ahk script, or look at the msdn documentation for sendinput().

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 10 Oct 2017, 14:53
by Sickae
Yeah, that's my only guess too, that Send doesn't actually use the SendInput method. I'm trying to figure out the source code but couldn't find anything else that generates keys yet. Any help with that would be greatly appreciated. I know the documentation for SendInput and as I said it works everywhere without problems, only in the game I can't get it to work. I know that much that it has something to do with directx's DirectInput but no solution yet.

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 10 Oct 2017, 15:05
by Helgef
Search for SendKeys() (source: Helgef memory :?) in the source code. You need to consider sendmode and possibly you need to understand the concept of sendinput reverting to sendevent.

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 10 Oct 2017, 17:24
by Sickae
I was searching in the source code for hours now and tried a lot more different methods. And finally I made it to work. :) As it turns out the problem was indeed with the flags, I had to set it to 0 and it works like a charm. No idea how to close the thread, so I'll just leave it be. Thank you!

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 17 Apr 2018, 15:51
by Kintaro-OEx
Sickae wrote:I was searching in the source code for hours now and tried a lot more different methods. And finally I made it to work. :) As it turns out the problem was indeed with the flags, I had to set it to 0 and it works like a charm. No idea how to close the thread, so I'll just leave it be. Thank you!
hello my friend, i got exactly the same issue and going to change some of my AHK scripts to C++. The Input works in the games chat but not actually working for using "skills" or such. you said that u found the solution, but sadly did not post it :-(. Anyone else maybe knows what exactly he changed to make it work?

ty

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 17 Apr 2018, 16:53
by swagfag
untested

Code: Select all

INPUT kb[2];

kb[0].type = INPUT_KEYBOARD;
kb[0].ki.wVk = 0x47; // send 'g' down
kb[0].ki.dwFlags = 0; 

kb[1].type = INPUT_KEYBOARD;
kb[1].ki.wVk = 0x47; // send 'g' up
kb[1].ki.dwFlags = KEYEVENTF_KEYUP; 

SendInput(2, &kb, sizeof(INPUT));

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 19 Apr 2018, 09:19
by Kintaro-OEx
There are 2 method to do this. #1 actually does not work in most games but is considered the "better" one to C++ community. Nevertheless, #2 works always and as good as #1. So go and use #2 in any case and u are fine.
#1

Code: Select all

    INPUT KEY;
    KEY.type = INPUT_KEYBOARD;
    KEY.ki.wScan = 0;
    KEY.ki.time = 0;
    KEY.ki.dwExtraInfo = 0;
    KEY.ki.wVk = 0x61;
                
    KEY.ki.dwFlags = 0;
    SendInput(1, &KEY, sizeof(INPUT));
    Sleep( (rand() % 15 + 5) );
    KEY.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &KEY, sizeof(INPUT));
    KEY.ki.dwFlags = 0;

#2

Code: Select all

	INPUT KEY_B;

	KEY_B.type = INPUT_KEYBOARD;
	KEY_B.ki.time = 0;
	KEY_B.ki.wVk = 0;
	KEY_B.ki.dwExtraInfo = 0;
	KEY_B.ki.dwFlags = KEYEVENTF_SCANCODE;
	KEY_B.ki.wScan = 0x30;

	SendInput(1, &KEY_B, sizeof(INPUT));
	Sleep((rand() % 15 + 1));
	KEY_B.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
	SendInput(1, &KEY_B, sizeof(INPUT));
	KEY_B.ki.dwFlags = KEYEVENTF_SCANCODE;
	

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 18 May 2018, 05:04
by Guest
@antrox Are you saying that the second version you posted works in Dota 2, then? I'm trying to get it working, but no luck so far. (it works in other programs, but not dota for me)

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 11 Jun 2018, 13:58
by leonidapower
Did you manage to make it work? I am also interested in a solution to this problem!

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 23 Apr 2020, 13:43
by Arikok
working solution first you need to set dwFlags 0
then you need to set dwFlags to KEYUP
this code is in java but you can understand the difference.

input.input.ki.dwFlags = new WinDef.DWORD(0);
input.input.ki.wScan = new WinDef.WORD( keyCode); // 0x41 -
User32.INSTANCE.SendInput( new WinDef.DWORD( 1 ), ( WinUser.INPUT[] ) input.toArray( 1 ), input.size() );
Thread.sleep(8);

input.input.ki.dwFlags = new WinDef.DWORD( KEYEVENTF_KEYUP );
input.input.ki.wScan = new WinDef.WORD( keyCode );
User32.INSTANCE.SendInput( new WinDef.DWORD( 1 ), ( WinUser.INPUT[] ) input.toArray( 1 ), input.size() );
Thread.sleep(16);

Re: [C++] SendInput doesn't work in a game, but AHK's SendInput (from the source code) does

Posted: 14 Dec 2020, 06:45
by Mehmet
Run in Administrator Mode, It Will Work In Sending Keys To The Game In The Program