i am trying to make an autoclicker. It should repeat MOUSEEVENTF_LEFTDOWN and MOUSEEVENTF_LEFTUP when LBUTTON is held.
Here is what i have so far. it doesn't repeat it, it double clicks.
#include <iostream>
#include<Windows.h>
using namespace std;
void menu()
{
cout << "Press 'F4' to enable and 'F5' to disable autoclicker\n";
}
void clicker()
{
bool click = false;
while (true)
{
if (GetAsyncKeyState('X'))
{
click = true;
}
else if (GetAsyncKeyState('Z'))
{
click = false;
}
if (click == true)
{
while (GetKeyState(VK_LBUTTON))
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(100);
}
}
}
}
int main()
{
menu();
clicker();
return 0;
}
Any help is appreciated! thanks
You're not giving the target application enough time to detect the key press, you should be using SendInput() as it's not deprecated.
#include <chrono>
#include <thread>
INPUT LeftButton = { 0 };
LeftButton.type = INPUT_MOUSE;
LeftButton.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &LeftButton, sizeof(INPUT));
std::this_thread::sleep_for(std::chrono::milliseconds(5));
LeftButton.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &LeftButton, sizeof(INPUT));
Related
Im VERY new to coding and Im trying to create an autoclicker that clicks aslong as im holding my left mouse button.
this is what i've tried but i dont know how to make it stop clicking when i release the left mouse button.
#include <iostream>
#include <windows.h>
using namespace std;
int x=0, y=0, cps;
bool click=false;
void Menu()
{
cout << "CPS: ";
cin >> cps;
}
void Clicker()
{
while (true)
{
if(GetAsyncKeyState(VK_LBUTTON) & 0x8000)
{
click = true;
}
while(click == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0 , 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0 , 0);
Sleep(1000/cps);
}
}
}
int main()
{
Menu();
Clicker();
}
what if you get rid of the click variable entirely:
void Clicker()
{
while (true)
{
if(GetAsyncKeyState(VK_LBUTTON) & 0x8000)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0 , 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0 , 0);
Sleep(1000/cps);
}
}
}
How do I make a tiny program that holds down left click, while toggleable through a key? I can make it toggle the leftmousebutton to click constantly but I have no clue on how to make it hold down.
void Clicker2()
{
while (1)
{
if (GetAsyncKeyState('U')) // U toggles on
{
click = true;
}
if (GetAsyncKeyState('I')) //I toggles off
{
click = false;
}
if (click == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
}
}
When using toggles you want to check the return value of GeyAsyncKeyState(), the least significant bit specifically, you do this by doing a bitwise AND with the result.
I have simplified your code, and converted it to using SendInput() which is what is recommended.
int main()
{
INPUT input{ 0 };
input.type = INPUT_MOUSE;
bool bClick = false;
while (true)
{
//toggles it on and off
if (GetAsyncKeyState('U') &1)
bClick = !bClick;
if (bClick)
{
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &input, sizeof(INPUT));
}
else
{
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &input, sizeof(INPUT));
}
}
return 0;
}
At the moment I'm trying to add a toggle key and make it hold to click, so that when I toggle it and hold down left click, it starts clicking. Currently it boots up and when I center the CPS it clicks, but it doesn't stop. It'll click continuously.
#include <iostream>
#include <windows.h>
using namespace std;
int x = 0, y = 0, cps;
bool click = false;
void Menu()
{
cout << "Add CPS (click per second):" << endl;
cin >> cps;
}
void Clicker()
{
while (1)
{
if (GetAsyncKeyState(VK_LBUTTON))
{
click = true;
}
if (GetAsyncKeyState(VK_RBUTTON))
{
click = false;
}
if (click == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
Sleep(1000 / cps);
}
}
}
int main()
{
Menu();
Clicker();
}
Please check the following code to see if it helps:
void Clicker()
{
while (1)
{
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000 && !click) //Capture that auto click start condition.
{
click = true;
}
else
{
click = false;
}
while (click)
{
if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) //Capture the stop condition.
{
break;
}
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
Sleep(1000 / cps);
}
}
}
GetAsyncKeyState Return Value:
If the most significant bit is set, the key is down.
How can I detect if a click on a (x,y) coordinates point will create any action?
For example is there any bool function (in c++) or something like that which can tell me if a double-click on (200,200) coordinates point will create any action before actually making the click?
Use ReadConsoleInput for mouse event.
You can check out microsoft domentation. http://msdn.microsoft.com/en-us/library/windows/desktop/ms685035(v=vs.85).aspx
Sample code again:
#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
cout<<"click anywhere in console window to write - hello world -\n\n\n\n\n\n\n\n\n\n\n\n\n"
"Press Ctrl+C to Exit";
HANDLE hout= GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD InputRecord;
DWORD Events;
COORD coord;
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 25;
cci.bVisible = FALSE;
SetConsoleCursorInfo(hout, &cci);
SetConsoleMode(hin, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
while(1)
{
ReadConsoleInput(hin, &InputRecord, 1, &Events);
if(InputRecord.EventType == MOUSE_EVENT)
{
if(InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
coord.X = InputRecord.Event.MouseEvent.dwMousePosition.X;
coord.Y = InputRecord.Event.MouseEvent.dwMousePosition.Y;
SetConsoleCursorPosition(hout,coord);
SetConsoleTextAttribute(hout,rand() %7+9);
cout<<"Hello world" ;
}
}
FlushConsoleInputBuffer(hin);
}
return 0;
}
I'm trying to catch keypress events using XLib. But for some reasons XNextEvent not working.
I'm not receiving any errors, but it looks like my program stuck on the line of "XNextEvent" call.
Here is my code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main()
{
XEvent event;
KeySym key;
char text[255];
Display *dis;
dis = XOpenDisplay(NULL);
while (1) {
XNextEvent(dis, &event);
if (event.type==KeyPress && XLookupString(&event.xkey,text,255,&key,0) == 1) {
if (text[0]=='q') {
XCloseDisplay(dis);
return 0;
}
printf("You pressed the %c key!\n", text[0]);
}
}
return 0;
}
This is not how X11 windowing system works.
Read this carefully. The key point is :
The source of the event is the viewable window that the pointer is in.
You do not create a window, therefore your program doesn't receive keyboard events. Even if you created window, it has to have focus :
The window used by the X server to report these events depends on the window's position in the window hierarchy and whether any intervening window prohibits the generation of these events.
Working example
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main()
{
XEvent event;
Display *dis;
Window root;
Bool owner_events = False;
unsigned int modifiers = ControlMask | LockMask;
dis = XOpenDisplay(NULL);
root = XDefaultRootWindow(dis);
unsigned int keycode = XKeysymToKeycode(dis, XK_P);
XSelectInput(dis,root, KeyPressMask);
XGrabKey(dis, keycode, modifiers, root, owner_events, GrabModeAsync, GrabModeAsync);
while (1) {
Bool QuiteCycle = False;
XNextEvent(dis, &event);
if (event.type == KeyPress) {
cout << "Hot key pressed!" << endl;
XUngrabKey(dis, keycode, modifiers, root);
QuiteCycle = True;
}
if (QuiteCycle) {
break;
}
}
XCloseDisplay(dis);
return 0;
}