C++ How Do I Simulate a MouseClick On a BackGround Process? - c++

As The Title Says I want to make a code or a app that runs in the background which clicks repeatedly so far I was only able to make a click code which is
void Click() {
INPUT iNPUTh = { 0 };
iNPUTh.type = INPUT_MOUSE;
iNPUTh.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &iNPUTh, sizeof(iNPUTh));
ZeroMemory(&iNPUTh, sizeof(iNPUTh));
iNPUTh.type = INPUT_MOUSE;
iNPUTh.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &iNPUTh, sizeof(iNPUTh));
}
int main() {
HWND hhProcess;
hhProcess = FindWindow(NULL, L"ARK: Survival Evolved");
if (hhProcess) {
AllocConsole();
cout << "Process Found!" << endl;
}
while (true) {
if (GetAsyncKeyState(VK_Numpad0) {
Click()
}
}
}
But my problem is that once I start to run it, it clicks on the surface what I want to happen is that it clicks on a background process

I have found myself an answer :) I tried testing some codes myself and tried to use Post Message
PostMessage(hhProcess, WM_LBUTTONDOWN, 0, 0);
Sleep(100);
PostMessage(hhProcess, WM_LBUTTONUP, 0, 0);
everytime i press numpad 6 while the window is not active it punches i thank you guys for helping me find ways even tho sometimes i dont understand it :)
Now My Problem is How do i make it click on a certain coords x,y in the background

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.

SendInput to Ctrl+C text Doesn't Work When Launched from Hotkey

Currently using this code to Copy selected text in currently open Window in Windows 10. This code is working fine if I run it on its own by when my target program (Notepad) has focus. The selected text in notepad is copied into data variable OK.
wchar_t title[MAX_PATH];
HWND target_window = GetForegroundWindow();
GetWindowText(target_window, title, MAX_PATH);
std::wcout << "Target window is '" << title << "'" << std::endl;
// Send Control + C
int key_count = 4;
INPUT* input = new INPUT[key_count];
for (int i = 0; i < key_count; i++)
{
input[i].ki.dwFlags = 0;
input[i].type = INPUT_KEYBOARD;
}
input[0].ki.wVk = VK_CONTROL;
input[0].ki.wScan = MapVirtualKey(VK_CONTROL, MAPVK_VK_TO_VSC);
input[1].ki.wVk = 0x43; // Virtual key code for 'c'
input[1].ki.wScan = MapVirtualKey(0x43, MAPVK_VK_TO_VSC);
input[2].ki.dwFlags = KEYEVENTF_KEYUP;
input[2].ki.wVk = input[0].ki.wVk;
input[2].ki.wScan = input[0].ki.wScan;
input[3].ki.dwFlags = KEYEVENTF_KEYUP;
input[3].ki.wVk = input[1].ki.wVk;
input[3].ki.wScan = input[1].ki.wScan;
if (!SendInput(key_count, (LPINPUT)input, sizeof(INPUT)))
{
// TODO: error handling
}
else
{
// not ideal but not sure of another way to wait for SendInput to complete
Sleep(100);
if (OpenClipboard(NULL))
{
HGLOBAL hglb = GetClipboardData(CF_UNICODETEXT);
LPWSTR lpwstr = (LPWSTR)(GlobalLock(hglb));
std::wstring data(lpwstr);
GlobalUnlock(hglb);
CloseClipboard();
// do something with selected text in data
}
else
{
// TODO: error handling
}
}
However, if I launch the exact same code via Hotkey, it doesn't work:
if (RegisterHotKey(
NULL,
1,
MOD_CONTROL | MOD_ALT | MOD_NOREPEAT,
VK_OEM_2)) // back slash question mark key
{
std::cout << "Hotkey 'Ctrl+Alt+/' registered, using MOD_NOREPEAT flag\n";
}
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
if (msg.message == WM_HOTKEY)
{
std::cout << "WM_HOTKEY received\n";
// Call function to COPY TEXT here
if (RegisterHotKey(
NULL,
1,
MOD_CONTROL | MOD_ALT | MOD_NOREPEAT,
VK_OEM_2)) // back slash question mark key
{
std::cout << "Hotkey 'Ctrl+Alt+/' registered, using MOD_NOREPEAT flag\n";
}
}
}
Now, in both cases, GetWindowText() is showing the title of the program I want to copy text from.
In addition, I wrote a simple test utility to check Ctrl+C is being passed to Window, which it is. It seems like Ctrl+C is being passed, but the copy is not occurring.
Is it possible that Alt is still down because of the hotkey and you are actually sending Ctrl+Alt+C? SendInput inserts the input directly into the global input queue.
You could try setting a timer in response to the hotkey and call GetAsyncKeyState in the timer handler until all modifier keys are up before generating input.
A better alternative would be to use UI Automation instead of a hack like this.

ShowCursor(FALSE) does not hide cursor on console application

I know this may sound to be a duplicate question but trust me it's not.
I have referred this question, but was not of much help as I am trying with a console application and the answerer himself tells he does not know the reason why ShowCursor(FALSE) does not work for console applications.
This thread did not help me either.
Here are the things I tried:
Using ShowCursor():
while(ShowCursor(false)>=0); //did not work
I first suspected that it was because of this statement in the msdn :
When Windows starts up, it checks if you have a mouse. If so, then the cursor show count is initialized to zero; otherwise, it is initialized to negative one.
I thought maybe in the latest windows, it doesn't recognize the connected mouse or the trackpad as an installed mouse and maybe that's why it didn't work. The following code shows it is not the case:
void UsingShowCursor()
{
CURSORINFO info;
info.cbSize = sizeof(CURSORINFO);
cout << ShowCursor(FALSE);
cout << ShowCursor(FALSE);
cout << ShowCursor(FALSE);
GetCursorInfo( &info ); //info.flags is CURSOR_SHOWING
}
Because I get -1, -2, -3. That means the initial show cursor count is obviously 0 and it does identify the installed mouse.
And another thing to note is that the GetCursorInfo() also tells that the cursor is showing.
Using SetCursor()
void UsingSetCursor()
{
HCURSOR prev = SetCursor(NULL);
int i = 0;
while(i++<10)
{
cout<<i<<endl;
Sleep(1000);
}
if( SetCursor(prev) == NULL ) //check if the previos cursor was NULL
cout<<"cursor was hidden and shown after 10 secs\n";
}
Doesn't work either.
This also did not work:
SetCursor(LoadCursor(NULL, NULL));
Edit:
Using LoadImage
Did not work either.
void UsingLoadImage()
{
// Save a copy of the default cursor
HANDLE arrowHandle = LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED);
HCURSOR hcArrow = CopyCursor(arrowHandle);
HCURSOR noCursorHandle = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR,1,1,LR_SHARED); //a single pixel thick cursor so that it wont be visible
HCURSOR noCursor = CopyCursor(noCursorHandle);
SetSystemCursor(noCursor, OCR_NORMAL);
int i =0 ;
while(i++<10)
{
cout<<i<<endl;
Sleep(1000);
}
//revert to previous cursor
SetSystemCursor(hcArrow, OCR_NORMAL);
DestroyCursor(hcArrow);
}
What can be the mistake? How can we hide the mouse for a console application?
You can use LoadImage() to achieve what you want. Here is the modified working version of the function UsingLoadImage() you quoted in the question. You have to include a cursor resource file to your visual studio project. Download the cursor from here or create your own.
Resource Files->Add->Existng Item and browse to the nocursor.cur file.
void UsingLoadImage()
{
// Save a copy of the default cursor
HANDLE arrowHandle = LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED);
HCURSOR hcArrow = CopyCursor(arrowHandle);
// Set the cursor to a transparent one to emulate no cursor
HANDLE noCursorHandle = LoadImage(GetModuleHandle(NULL), L"nocursor.cur", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE); //worked
//HANDLE noCursorHandle = LoadCursorFromFile(L"nocursor.cur"); //this also worked
HCURSOR noCursor = CopyCursor(noCursorHandle);
SetSystemCursor(noCursor, OCR_NORMAL);
int i =0 ;
while(i++<10)
{
cout<<i<<endl;
Sleep(1000);
}
SetSystemCursor(hcArrow, OCR_NORMAL);
DestroyCursor(hcArrow);
}
This would replace the normal arrow cursor with the transparent one. If you want to hide all the other cursor like the text, loading, hand cursors etc you have to hide them individually. If you don't want that to be the case, then you should opt out of the console application as many commenters have pointed out.
Hope this helps.

Sending Mouse Click to DirectX Game Running as Admin

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".

Send "button pressed" message to UnityWndClass

I have 3D game and i need to press buttons in this game from another application. Spy++ says that window of the game is UnityWndClass. When i pressing buttons in game, window recives only mouse messages like WM_SETCURSOR, WM_LBUTTONDOWN etc, but when i try to send WM_LBUTTONDOWN and WM_LBUTTONUP with coordinates of the button from another window nothing happend. Why? Any ideas how to press buttons?
WM_SETCURSOR is for changing the cursor image of your own window. WM_LBUTTONDOWN and WM_LBUTTONUP are notification messages sent after mouse click. WM_LBUTTONXXX messages are probably not handled by the main Window procedure.
To fake mouse input, you can use mouse_event or SendInput. For example, this moves cursor to x=10 / y=10 and and clicks left mouse.
int x = 10 * 65536 / GetSystemMetrics(SM_CXSCREEN);
int y = 10 * 65536 / GetSystemMetrics(SM_CYSCREEN);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Or use SendInput for multiple calls (see comments)
INPUT input[3];
for (int i = 0; i < 3; i++)
{
memset(&input[i], 0, sizeof(INPUT));
input[i].type = INPUT_MOUSE;
}
input[0].mi.dx = x;
input[0].mi.dy = y;
input[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(3, input, sizeof(INPUT));
Window should be at least visible and preferably be active. You can use SetForegroundWindow. See this example to make sure window is visible and active.
Other alternatives:
Use Spy++ to get information about the button which you want to press.
For example, if this was Windows Calculator, you can spy on calculator's buttons, it shows calculator button 2 has ID set to 0x84. You can send WM_COMMAND with WPARAM set to 0x84 to simulate button press.
HWND hwnd = FindWindow(0, L"Calculator");
if (IsWindow(hwnd))
{
//this should put "234" in to calculator's edit box
SendMessage(hwnd, WM_COMMAND, 0x84, 0);
SendMessage(hwnd, WM_COMMAND, 0x85, 0);
SendMessage(hwnd, WM_COMMAND, 0x86, 0);
}