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

Talk about things C/C++, some related to AutoHotkey
Sickae
Posts: 4
Joined: 10 Oct 2017, 09:36

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

10 Oct 2017, 10:06

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
	...
}
...
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

10 Oct 2017, 13:29

You probably shouldn't use the unicode flag in a game. :beer:
Sickae
Posts: 4
Joined: 10 Oct 2017, 09:36

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

10 Oct 2017, 14:12

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));
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

10 Oct 2017, 14:39

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().
Sickae
Posts: 4
Joined: 10 Oct 2017, 09:36

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

10 Oct 2017, 14:53

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.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

10 Oct 2017, 15:05

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.
Sickae
Posts: 4
Joined: 10 Oct 2017, 09:36

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

10 Oct 2017, 17:24

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!
User avatar
Kintaro-OEx
Posts: 12
Joined: 17 Apr 2018, 15:48

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

17 Apr 2018, 15:51

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
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

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

17 Apr 2018, 16:53

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));
User avatar
Kintaro-OEx
Posts: 12
Joined: 17 Apr 2018, 15:48

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

19 Apr 2018, 09:19

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;
	
Guest

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

18 May 2018, 05:04

@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)
leonidapower
Posts: 1
Joined: 11 Jun 2018, 13:53

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

11 Jun 2018, 13:58

Did you manage to make it work? I am also interested in a solution to this problem!
Arikok

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

23 Apr 2020, 13:43

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);
Mehmet

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

14 Dec 2020, 06:45

Run in Administrator Mode, It Will Work In Sending Keys To The Game In The Program

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 9 guests