I started working on a small bot project as a fun way to dive into the win32 api (among other things). In particular, SendInput is only behaving as expected in some instances.
As a proof of concept I set up a loop to send the 's' key just to make sure things are working. If I set the window I want to activate as Notepad, text appears just fine. But if I set the window to Ikaruga, the window pops up but the menu doesn't change (game uses WASD for menu navigation, so it should just continually go down).
I have read in multiple places that games have some (not very reliable) options for blocking external input. To check if that was the case, I found a project where someone built a key sender. The particular project I used is here enter link description here . His code sent 's' keys to Ikaruga as expected.
He included his source code. I've spent a fair bit of time reading over it. While a lot of the functions he is using have been superseded, the overall code flow is similar.
Hopefully, someone can provide me some guidance on what I'm doing wrong.
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#define WINVER 0x0500
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void AppActivate(LPCTSTR windowTitle){
// Get first window on desktop
HWND firstwindow = FindWindowEx(NULL, NULL, NULL, NULL);
HWND window = firstwindow;
TCHAR windowtext[MAX_PATH];
// Guard against search word matching current console title
TCHAR consoletitle[MAX_PATH];
GetConsoleTitle(consoletitle, MAX_PATH);
while (1){
// Check window title for a match
GetWindowText(window, windowtext, MAX_PATH);
if (strstr(windowtext, windowTitle) != NULL && strcmp(windowtext, consoletitle) != 0){
break;
}
// Get next window
window = FindWindowEx(NULL, window, NULL, NULL);
if (window == NULL || window == firstwindow){
fprintf(stderr, "Window not found\n");
}
}
fprintf(stderr, "Window found: %s\n", windowtext);
// Bring specified window into focus
AllowSetForegroundWindow(true);
ShowWindow(window, SW_RESTORE);
SetForegroundWindow(window);
SetFocus(window);
}
int main(int argc, char* argv[]){
AppActivate("Ikaruga");
Sleep(2000);
// Create a generic keyboard event structure
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wVk = 0; // scan code for 's'
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
while (1){
// Press the "S" key
ip.ki.wScan = 0x1F; // 0x1F 's'
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "S" key
ip.ki.wScan = 0x1F;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
char msgbuf[200];
sprintf(msgbuf, "Pressing S\n");
OutputDebugString(msgbuf);
Sleep(2000);
}
return 0;
}
Edit: I forgot to mention that I've tried setting the code on both wVK/wScan and 's' vs 0x53.