Sending Mouse Click to DirectX Game Running as Admin - c++

I have been building a gaming-related program that needs to send simulated input to the game (which is the top window on the screen and runs in fullscreen mode). After some struggling, I finally got mouse movements (cursor drag) and keyboard input working, but for some reason, the game will not respond to simulated mouse clicks.
I have tried the following:
#if TRUE // SendInput works for keyboard simulation and mouse drag, but not clicks:
INPUT mouse = {0};
mouse.type = INPUT_MOUSE;
mouse.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &mouse, sizeof(INPUT));
Sleep(100);
ZeroMemory(&mouse, sizeof INPUT);
mouse.type = INPUT_MOUSE;
mouse.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &mouse, sizeof(INPUT));
#else // tried this, but it did not work:
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(75);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
#endif
The first dilemma I faced was sending the messages in general, but I realized this issue was caused by game process privileges > sending application privileges. After this, I encountered another problem with keypresses only registering on certain game windows/screens, but after some searching, I was able to use scancodes to overcome this. For example:
void SendSpacePress(bool bHardwareLevel){
INPUT space = {0};
space.type = INPUT_KEYBOARD;
space.ki.time = 0;
space.ki.dwExtraInfo = 0;
if(!bHardwareLevel){
space.ki.wVk = VK_SPACE;
}else{
space.ki.wScan = 0x39; // physical keyboard scan code
}
space.ki.dwFlags = bHardwareLevel ? KEYEVENTF_SCANCODE : 0;
SendInput(1, &space, sizeof(INPUT));
Sleep(rand()%25 + 25);
space.ki.dwFlags = bHardwareLevel ? KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP : KEYEVENTF_KEYUP;
SendInput(1, &space, sizeof(INPUT));
}
I have also been able to do mouse movement using INPUT like below:
void PanMouse(){
INPUT mouse = {0};
mouse.type = INPUT_MOUSE;
mouse.mi.time = 0;
mouse.mi.mouseData = 0;
mouse.mi.dwExtraInfo = 0;
mouse.mi.dwFlags = MOUSEEVENTF_MOVE;
mouse.mi.dx = rand()%10 -5;
mouse.mi.dy = rand()%10 -5;
SendInput(1, &mouse, sizeof(INPUT));
}
Now the big problem is the game refuses to register my simulated mouse clicks. I would preferably like a way to send the mouse inputs without having to dive into hooking. (The game has multiple client-side anti-cheat mechanisms, so I would venture to guess that any kind of foreign process hooking or DLL injection would trigger the protection.) From what I have read, I might need to write a custom driver for a simulated hardware mouse so the input comes at the kernel level. Not a preferable option, but if need be, sobeit. (And if need be, can anyone provide helpful information for this? I have never messed around with writing drivers, but there's a first time for everything I suppose.)
TL;DR: What do I need to do to get simulated mouse clicks to register in a game that seems to ignore non-hardware input? (I.e., how can I trick the game into thinking mouse clicks are legitimate similarly to using KEYEVENTF_SCANCODE when simulating keyboard input?)

Instead of sleeping between inputs, you need to inject a pair of mouse inputs together with the time member appropriately set to indicate "how long".
Here's something close to what you want. This will click the current cursor position with the left mouse simulated for 100ms.
INPUT inputs[2] = {};
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.time = 0;
inputs[0].mi.dx = 0;
inputs[0].mi.dy = 0;
inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
inputs[1].type = INPUT_MOUSE;
inputs[1].mi.time = 100;
inputs[1].mi.dx = 0;
inputs[1].mi.dy = 0;
inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(2, inputs, sizeof(INPUT));
The mouse event won't happen immediately. It gets "queued" to happen shortly afterwards.
You should use the same technique for your keyboard events. Don't call "Sleep".

Related

Why PostMessage with WM_LBUTTONDBLCLK does not work?

I run this code:
LPPOINT pp = new POINT;
GetCursorPos(pp);
while(1){
PostMessage(GetDesktopWindow(), WM_LBUTTONDBLCLK, 0, MAKELPARAM(pp->x, pp->y));
Sleep(1000);
}
It does not click on the point indicated by the cursor, but opens and closes the Start menu
.Please tell me what's wrong
Sending WM_LBUTTONDBLCLK to an arbitrary window handle or to the desktop will not simulate a mouse click.
You can use SendInput, however, which can simulate a mouse click given screen coordinates (not a window handle, or window or client coordinates). This code will simulate a left-button click at the current cursor position:
INPUT in[2]; // 0 = left dn, 1 = left up
ZeroMemory(in, sizeof(INPUT) * 2);
in[0].type = INPUT_MOUSE;
in[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
in[1].type = INPUT_MOUSE;
in[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(2, in, sizeof(INPUT));
Note that you can also use mouse_event, but SendInput is preferred according to the official documentation on MSDN.

Simple program considered malware

I'm working on an alarm clock application. I've been testing some things with the following code:
FMOD_SYSTEM* sound;
FMOD_SOUND* son;
FMOD_System_Create(&sound);
FMOD_System_Init(sound, 32, FMOD_INIT_NORMAL, NULL);
FMOD_System_CreateSound(sound, "alarm.mp3", FMOD_CREATESAMPLE, 0, &son);
SYSTEMTIME time;
GetSystemTime(&time);
while (!(time.wHour==16 && time.wDayOfWeek==4)) {
Sleep(10000);
GetSystemTime(&time);
}
FMOD_System_PlaySound(sound, son, NULL, 0, NULL);
for (int i = 0; i < 100; i++) {
INPUT ip = { 0 };
ip.type = INPUT_KEYBOARD;
ip.ki.wVk = VK_VOLUME_UP;
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
The idea would be to pu it in the Startup folder so that it launches every time the user logs in, then waits until the specified hour. Then, it would play the sound file using the FMOD library and increase master volume. I know that my way of increasing volume is unorthodox, but I'm just testing for the moment. It works well if executed.
The problem is that my antivirus considers it malware. I've tried it under Avast and Bitdefender with the same results. It's a rather simple code that doesn't even manage files, how can it be considered a threat?
Thanks
Edit: Forgot to say that I've tested it with and without the master volume managing loop. Therefore, there should be a problem anywhere else...

Waking up Windows when in AwayMode

I am using the Windows AwayMode to turn off the monitor and audio instead of going into sleep mode. This is working fine. How do I "wake up" the system when an event I want occurs? I can detect the event, but I don't know how to get the monitor back on and the system to appear awake again.
I have tried GetCursorPos() and SetCursorPos() to try move the cursor but that did not work.
I also tried the CreateWaitableTimer() and SetWaitableTimer() but that didn't work either. I set the fResume option to TRUE.
I also tried to turn off AwayMode with the PowerSetRequest() handle and setting it NULL. That also did not work.
I also have tried the SetThreadExecutionState() call with no luck. There is an AwayMode defined here as well. I tried to set that and clear it, but the monitor doesn't come back on.
I found a way to do it using SendInput() with a mouse movement. I also had to use the SetThreadExecutionState() to let the system know that a user is present otherwise it will go back to AwayMode in 2 seconds. Here is the code I used.
// Get the current position to ensure we put it back at the end
POINT pt;
GetCursorPos(&pt);
// Make a mouse movement
// Go to upper left corner (0,0)
INPUT input;
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = 0;
input.mi.dy = 0;
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
SendInput(1, &input, sizeof(input));
Sleep(5); // Just in case this is needed
// Go to lower right corner (65535,65535)
input.mi.dx = 65535;
input.mi.dy = 65535;
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
SendInput(1, &input, sizeof(input));
Sleep(5); // Just in case this is needed
// Restore to original
SetCursorPos(pt.x, pt.y);
// Now let the system know a user is present
DWORD state = SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);

C Virtual Keyboard Input selectively working

I am working a on project that needs virtual input which is being coded in Microsoft Visual Studios using the windows.h header. To do this I am using the keybd_event() method and the VkKeyScan() method:
keybd_event(VkKeyScan('w'), 0, 0, 0);
keybd_event(VkKeyScan('w'), 0, KEYEVENTF_KEYUP, 0);
However, the virtual input is only being recognized by some programs, such as Notepad, Command Prompt, browsers, and other applications that use text fields. The purpose of virtual input for my project is to use a client to control "VisualGameBoyAdvance" which takes keyboard input that is then translated into a command such as 'w' = start, 'z' = a button, etc.
Why is the virtual input being read when using applications in text fields, but not as commands? Are there alternative methods of the windows.h header file or are there better methods in the header?
Update: I have been trying to use VkKeyScanEx() as an alternative method for taking virtual input as a shot-in-the-dark effort. How to I specify my input locale identifier? I have been trying to use UTF-8 and en_AU.UTF-8, with no luck. Is there a chart that translates how to specify this?
I've also tried using
keybd_event(GetKeyState('w'), 0, 0, 0);
keybd_event(GetKeyState('w'), 0, KEYEVENTF_KEYUP, 0);
Which did not work at all and tried
while(1){
/*Sleep(1000);
keybd_event(GetKeyState('w'), 0, 0, 0);
keybd_event(GetKeyState('w'), 0, KEYEVENTF_KEYUP, 0);*/
// Pause for 5 seconds.
Sleep(500);
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press the "W" key
ip.ki.wVk = 0x57; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "A" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
// Exit normally
}
The problem was that the game was running though DirectX, which prevented virtual keyboard strokes. By rending the game right from the sys, it avoided directx all together and worked fine :)

How to send shortcut to another application in c++ in MS Windows?

My question is how to send shortcut from c++ to another application. Let's say for example that the notepad is open, and I want send to them shortcut like CTRL+P, or more complexive shortcut like CTRL+SHIFT+HOME, or ALT+F4, I find many good tutorials that explained how to send one key like 'A' or 'ALT', but I did not find how to send it together.
You can use SendInput() or keybd_event() to send keys to the currently focused window. SendInput() might be the better choice as it would not intersperse the keyboard events with those form the actual keyboard.
// send CTRL+SHIFT+HOME
INPUT input[6];
memset(input, 0, sizeof(INPUT)*6);
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CONTROL;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = VK_SHIFT;
input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = VK_HOME;
input[3].type = INPUT_KEYBOARD;
input[3].ki.wVk = VK_HOME;
input[3].ki.dwFlags = KEYEVENTF_KEYUP;
input[4].type = INPUT_KEYBOARD;
input[4].ki.wVk = VK_SHIFT;
input[4].ki.dwFlags = KEYEVENTF_KEYUP;
input[5].type = INPUT_KEYBOARD;
input[5].ki.wVk = VK_CONTROL;
input[5].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(6, input, sizeof(INPUT));
You can send a number of WM_KEYDOWNs followed by a number of WM_KEYUPs so you could try sending DOWN ctrl, shift, home, UP home, shift, ctrl - that should do the trick.
This kind of "key combinations" is usually called Keyborad Accelerators in Microsoft Windows. The MSDN intro page