Simulate leftclicks only while holding the leftclick button - c++

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

Related

I'm currently making an Autoclicker and so far it's been a semi success. I need help introducing a toggle key

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.

Button isn't appearing

I am supposed to draw window that has a light bulb and have a button that turns it on ( white) and off( black). Here is the full objective:Write a program to display a window with a circle representing a light bulb and a button labelled "ON/OFF". The light bulb starts off black, i.e., off. When the button is clicked, the light bulb turns on, i.e., white. When the button is clicked again, the light goes off, and so on.
But for some reason, the circle doesn't appear in the window Here is my code:
#include "std_lib_facilities_5.h"
#include "Simple_window.h"
#include "Graph.h"
#include "Window.h"
#include "Point.h"
struct Lines_window : Graph_lib::Window {
Lines_window(Point xy, int w, int h, const string& title);
Circle light_bulb{Point{400,400},150};
private:
Button on;
bool button_pushed;
static void cb_on1(Address, Address); // callback for next_button
void on1(); // action to be done when next_button is pressed
};
Lines_window::Lines_window(Point xy, int w, int h, const string& title)
:Window{xy,w,h,title},
on{Point{x_max()-100,0}, 70, 20, "ON/OFF",cb_on1},
button_pushed{false}
{
attach(on);
}
void Lines_window::cb_on1(Address, Address pw)
// call Simple_window::next() for the window located at pw
{
reference_to<Lines_window>(pw).on1();
}
void Lines_window::on1(){
if (button_pushed)
{
light_bulb.set_fill_color(Color::white);
button_pushed = false;
redraw();
}
else
{
light_bulb.set_fill_color(Color::black);
button_pushed = true;
redraw();
}
redraw();
}
int main ()
{
try{
Lines_window win {Point{100,100},800,800,"Light Bulb"};
return gui_main();
}
catch (exception& e){ //catches range errors and run_time_errors
cerr << "error: "<< e.what() <<'\n';
return 1;
}
catch (...){ //catch any exception
cerr <<"Exception: something went wrong \n";
return 2;
}
}

Color pairs in ncurses not working properly in classes

I'm making a basic terminal-app engine with ncurses. I have made a basic window class, which stores the window you are working with, and allows you to write on it. Now, I'm doing a ColorWindow class, which inherits from BasicWindow, and should allow you to write with colors. Per abstraction, I'm initializing the color pairs I need in one of the methods of the class, here is the example code:
EDIT: I'm writing here all code
#include <ncurses.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
class ColorWindow : public BasicWindow {
private:
int npairs;
bool canColor;
void initializePairs() {
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
npairs = 8;
}
public:
ColorWindow() {
if(has_colors()) {
initializePairs();
this->canColor = true;
}
else {
this->canColor = false;
}
}
ColorWindow(int he, int wi, int stx, int sty) : BasicWindow(he, wi, stx, sty) {
if(has_colors()) {
initializePairs();
this->canColor = true;
}
else {
this->canColor = false;
}
}
int addColorPair(int foreground, int background) {
if(foreground < 0 || foreground > 7
|| background < 0 || background > 7)
return -1;
init_pair(npairs, foreground, background);
npairs++;
return npairs-1;
}
bool ColorWindow::writeStringWithColor(int x, int y,const char* message, int pair) {
if(!isInLimits(x, y))
return false;
if(pair >= npairs)
return false;
if(canColor)
return false;
wattron(getContainer(), COLOR_PAIR(pair));
mvwprintw(getContainer(), x, y, message);
wattroff(getContainer(), COLOR_PAIR(7));
}
bool changeColor(int color, int r, int g, int b) {
if(!can_change_color())
return false;
if(color < 0 || color > 7 || r < 0 || r > 1000
|| g < 0 || g > 1000 || b < 0 || b > 1000)
return false;
init_color(color, r, g, b);
return true;
}
};
class BasicWindow {
private:
WINDOW* container;
int startx, starty, height, width;
public:
BasicWindow() {
this->height = 20;
this->width = 84;
this->starty = (LINES - height) / 2; /* Calculating for a center placement */
this->startx = (COLS - width) / 2;
}
BasicWindow(int he, int wi, int stx, int sty) {
this->height = he;
this->width = wi;
this->starty = sty;
this->startx = stx;
}
WINDOW* createNewContainer() {
this->container = newwin(height, width, starty, startx);
wrefresh(this->container); /* Show that box */
return getContainer();
}
WINDOW* getContainer() {
return this->container;
}
bool writeString(int x, int y, const char* message) {
if(!isInLimits(x, y))
return false;
mvwprintw(this->container, x, y, message);
}
void refreshContainer() {
refresh();
wrefresh(this->container); /* Show that box */
}
void destroyWindow() {
wborder(this->container, ' ', ' ', ' ',' ',' ',' ',' ',' ');
wrefresh(this->container);
delwin(this->container);
}
bool isInLimits(int x, int y) {
if(x < 0 || x >= this->width-1) {
return false;
}
if(y < 0 || y >= this->height-1) {
return false;
}
return true;
};
And here is the main:
#include <iostream>
#include <unistd.h>
#include <ncurses.h>
#include "windows.h"
int main() {
ColorWindow cw = ColorWindow(20, 80, 0, 0);
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE);
start_color();
WINDOW* container;
container = cw.createNewContainer();
cw.writeStringWithColor(0, 10, "Hello everyone in color!!", 2);
cw.refreshContainer();
sleep(2);
endwin();
return 0;
}
It initializes the color mode, but shows nothing, as if the pair selected was the BLACK BLACK.
Running the code (and tracing it using ncurses' debug-trace feature), there is only one call to init_pair, using a pair number which appears to come from an uninitialized value, e.g., 7978 in this part of the listing:
called {init_pair(0x193e250,7978,5,7)
return }0
and a quick scan of the sources shows that corresponds to this method:
/* PUBLIC METHODS */
int ColorWindow::addColorPair(int foreground, int background) {
if(foreground < 0 || foreground > 7
|| background < 0 || background > 7)
return -1;
init_pair(npairs, foreground, background);
npairs++;
return npairs-1;
}
and npairs is never set to zero in the constructor.
You might find valgrind useful for seeing this type of problem.

How do I draw lines on a image using mouse clicks in opencv?

So, after following the advise from the stackexchange users about mouse event, I was able to understand and implement some simple task using mouse clicks. So, the next goal was to draw a simple line using the mouse left click and mouse right click. Unfortunately, I can't see any line after I implemented my program.
int x,y;
Point p(0,0);
Point q(0,0);
Mat xal;
void drawimage()
{
a = q.x - p.x; //i am just checking out the values of a and b to see if the drawimagefunction is being called in the rightmouse click event
b = q.y - p.y;
cout<<" a is :"<<a<<endl;
cout<<"b is:"<<b<<endl;
line(xal,Point(p.x,p.y),Point(q.x,q.y),Scalar(0,0,255),2,8);
}
void onMouse( int event, int x, int y, int f, void* )
{
switch (event)
{
case EVENT_LBUTTONDOWN:
cout<<"Left button was pressed"<<x<<" "<<y<<" "<<endl;
{
p.x = x;
p.y = y;
cout<<"p is:"<<p.x<<p.y<<endl;
}
break;
case EVENT_RBUTTONDOWN:
cout<<"Right button was pressed at :"<<x <<" "<<y<<endl;
{
q.x = x;
q.y = y;
drawimage();//no line is being drawn though i can see that i get the values of a and b in the drawimage function.
}
break;
default:
break;
}
}
int main()
{
xal = imread("pic.JPG);
namedWindow("Picture",1);
setMouseCallback("Picture",onMouse,NULL);
imshow("Picture",xal);
cvwaitkey(0);
}
Add the following after your "line(..)" call in your drawLine() function:
imshow("Picture", xal);
The problem is that you are writing the line to the xal matrix, but you have not updated the image on the screen, which is what the imshow(..) call will do.
Try this one code. It is useful for you.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void drawimage()
{
line(xal,Point(p->x,p->y),Point(q->x,q->y),Scalar(0,0,255),2,8);
}
void CallBackFunc(int event, int x, int y, int flags, void *ptr )
{
if ( event == EVENT_LBUTTONDOWN )
{
Point*p = (Point*)ptr;
p->x = x;
p->y = y;
drawimage();
}
else if ( event == EVENT_RBUTTONDOWN )
{
Point*q = (Point*)ptr;
q->x = x;
q->y = y;
drawimage();
}
}
int main(int argc, char** argv)
{
// Read image from file
Point p;
Point q;
Mat xal = imread("MyPic.JPG");
//if fail to read the image
if ( xal.empty() )
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc,&p);
setMouseCallback("My Window", CallBackFunc,&q);
//show the image
imshow("My Window", xal);
// Wait until user press some key
waitKey(0);
return 0;
}

Allegro 5 drawing to buffer

I am making a program that will draw balls bouncing around the screen. I am currently working on the part that draws the balls.
My code consists of the following:
BallEngine Class - Manage all the Allegro functions/objects
BallManager Class - Manages all of the balls
Ball Class - Hold information about a ball
main.cpp - Game loop, etc.
BallEngine.h:
#pragma once
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include "BallManager.h"
ALLEGRO_DISPLAY *display;
class BallEngine
{
private:
bool fullScreen;
int fps;
bool running;
public:
BallManager BManager;
bool getFullScreen();
bool getIsRunning();
void updateFullScreen();
void setFullScreen(bool value);
void setIsRunning(bool value);
int getFPS();
//Allegro Objects
ALLEGRO_FONT *deafault_font_12;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_TIMER *timer;
//Colors
ALLEGRO_COLOR RED;
ALLEGRO_COLOR GREEN;
ALLEGRO_COLOR BLUE;
ALLEGRO_COLOR YELLOW;
ALLEGRO_COLOR PINK;
ALLEGRO_COLOR LIGHT_BLUE;
ALLEGRO_COLOR WHITE;
ALLEGRO_COLOR BLACK;
//Debug
bool showDebug;
void drawBallInfo(int x, int y, int id); //Draws information about a certain ball
BallEngine(int width, int height);
~BallEngine(void);
};
BallEngine.cpp:
#include "BallEngine.h"
BallEngine::BallEngine(int width, int height)
{
running = true;
showDebug = false;
fps = 60;
al_init();
if(!al_init())
{
printf("Failed to initalize Allegro \n");
}
al_install_keyboard();
if(!al_install_keyboard())
{
printf("Failed to initalize keyboard \n");
}
al_init_font_addon();
al_init_ttf_addon();
fullScreen = false;
updateFullScreen();
deafault_font_12 = al_load_font("arial.ttf", 12, 0);
event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_keyboard_event_source());
timer = al_create_timer(1.0/fps);
al_register_event_source(event_queue, al_get_timer_event_source(timer));
display = al_create_display(width, height);
//Define engine colors
RED = al_map_rgb(255,0,0);
GREEN = al_map_rgb(0,255,0);
BLUE = al_map_rgb(0,0,255);
YELLOW = al_map_rgb(255,255,0);
PINK = al_map_rgb(255,0,255);
LIGHT_BLUE = al_map_rgb(255,255,0);
WHITE = al_map_rgb(255,255,255);
BLACK = al_map_rgb(0,0,0);
}
BallEngine::~BallEngine(void)
{
}
bool BallEngine::getFullScreen()
{
return fullScreen;
}
bool BallEngine::getIsRunning()
{
return running;
}
void BallEngine::updateFullScreen()
{
if ( fullScreen == true )
{
al_set_new_display_flags(ALLEGRO_FULLSCREEN);
}
else
{
al_set_new_display_flags(ALLEGRO_WINDOWED);
}
}
void BallEngine::setFullScreen(bool value)
{
fullScreen = value;
}
int BallEngine::getFPS()
{
return fps;
}
void BallEngine::drawBallInfo(int x, int y, int id)
{
if(BManager.isBallExist(id))
{
al_draw_textf(deafault_font_12, RED, x, y, 0, "X: %i Y: %i Velocity: %i Angle: %i Radius: %i Color %ALLEGRO_COLOR ", BManager.getBall_X(id), BManager.getBall_Y(id), BManager.getBall_Velocity(id), BManager.getBall_Angle(id), BManager.getBall_Radius(id), BManager.getBall_Color(id));
}
else
{
printf("Failed to draw ball %i information: Ball selceted out of range \n", id);
}
}
BallManager.h:
#pragma once
#include <iostream>
#include <vector>
#include "Ball.h"
#include <allegro5\allegro.h>
class BallManager
{
private:
std::vector<Ball*> List;
public:
//Get functions
int getBall_X(int id);
int getBall_Y(int id);
int getBall_Velocity(int id);
int getBall_Angle(int id);
int getBall_Radius(int id);
ALLEGRO_COLOR getBall_Color(int id);
//Set Functions
void setBall_X(int id, int value);
void setBall_Y(int id, int value);
void setBall_Velocity(int id, int value);
void setBall_Angle(int id, int value);
void setBall_Radius(int id, int value);
void setBall_Color(int id, ALLEGRO_COLOR value);
bool isBallExist(int id); //Returns true if a ball at index id exists. Else returns false.
void CreateBall(int x, int y, int velocity, int angle, int radius, ALLEGRO_COLOR color);
void DeleteBall(int id);
void drawBall(int id);
void drawBalls();
void updateBalls(); //NOT YET DONE
BallManager(void);
~BallManager(void);
};
BallManager.cpp:
#include "BallManager.h"
BallManager::BallManager(void)
{
}
BallManager::~BallManager(void)
{
}
//Get Functions:
int BallManager::getBall_X(int id)
{
return List[id]->getPos_X();
}
int BallManager::getBall_Y(int id)
{
return List[id]->getPos_Y();
}
int BallManager::getBall_Velocity(int id)
{
return List[id]->getVelocity();
}
int BallManager::getBall_Angle(int id)
{
return List[id]->getAngle();
}
int BallManager::getBall_Radius(int id)
{
return List[id]->getRadius();
}
ALLEGRO_COLOR BallManager::getBall_Color(int id)
{
return List[id]->getColor();
}
//Set functions:
void BallManager::setBall_X(int id, int value)
{
List[id]->setPos_X(value);
}
void BallManager::setBall_Y(int id, int value)
{
List[id]->setPos_Y(value);
}
void BallManager::setBall_Velocity(int id, int value)
{
List[id]->setVelocity(value);
}
void BallManager::setBall_Angle(int id, int value)
{
List[id]->setAngle(value);
}
void BallManager::setBall_Radius(int id, int value)
{
List[id]->setRadius(value);
}
void BallManager::setBall_Color(int id, ALLEGRO_COLOR value)
{
List[id]->setColor(value);
}
void BallManager::CreateBall(int x, int y, int velocity, int angle, int radius, ALLEGRO_COLOR color)
{
Ball* ball = new Ball(x, y, velocity, angle, radius, color);
List.push_back(ball);
}
void BallManager::DeleteBall(int id)
{
if(isBallExist(id))
{
delete List[id];
List.erase(List.begin()+id);
}
else
{
printf("Failed to delete ball %i information: Ball selceted out of range \n", id);
}
}
bool BallManager::isBallExist(int id)
{
if((id+1) > List.size() || id < 0)
{
return false;
}
return true;
}
void BallManager::drawBall(int id)
{
List[id]->Draw();
}
void BallManager::drawBalls()
{
int total = List.size();
for(int index = 0; index < total; index++)
{
List[index]->Draw();
}
}
void updateBalls()
{
//TODO
}
Ball.h:
#pragma once
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h>
class Ball
{
private:
int x;
int y;
int velocity; //Positive is left side of screen, Negitive is right side of screen
int angle; // Angle derived from the positive vertical
int radius;
ALLEGRO_COLOR color;
public:
//Get Functions
int getPos_X();
int getPos_Y();
int getVelocity();
int getAngle();
int getRadius();
ALLEGRO_COLOR getColor();
//Set Functions
void setPos_X(int value);
void setPos_Y(int value);
void setVelocity(int value);
void setAngle(int value);
void setRadius(int value);
void setColor(ALLEGRO_COLOR value);
//Draws to screen
void Draw();
//Constructor
Ball(int Start_X, int Start_Y, int Start_Velocity, int Start_Angle, int Start_Radius, ALLEGRO_COLOR Start_Color);
//Desctructor
~Ball(void);
};
Ball.cpp:
#include "Ball.h"
Ball::Ball(int Start_X, int Start_Y, int Start_Velocity, int Start_Angle, int Start_Radius, ALLEGRO_COLOR Start_Color)
{
x = Start_X;
y = Start_Y;
velocity = Start_Velocity;
angle = Start_Angle;
radius = Start_Radius;
color = Start_Color;
}
Ball::~Ball(void)
{
}
//Get functions
int Ball::getPos_X()
{
return x;
}
int Ball::getPos_Y()
{
return y;
}
int Ball::getVelocity()
{
return velocity;
}
int Ball::getAngle()
{
return angle;
}
int Ball::getRadius()
{
return radius;
}
ALLEGRO_COLOR Ball::getColor()
{
return color;
}
//Set functions
void Ball::setPos_X(int value)
{
x = value;
}
void Ball::setPos_Y(int value)
{
y = value;
}
void Ball::setVelocity(int value)
{
velocity = value;
}
void Ball::setAngle(int value)
{
angle = value;
}
void Ball::setRadius(int value)
{
radius = value;
}
void Ball::setColor(ALLEGRO_COLOR value)
{
color = value;
}
void Ball::Draw()
{
al_draw_filled_circle(x, y, radius, color);
}
main.cpp:
#include <allegro5\allegro.h>
#include "BallEngine.h"
int ScreenWidth = 620;
int ScreenHeight = 480;
int main()
{
BallEngine Engine(ScreenWidth, ScreenHeight);
//Test balls
Engine.BManager.CreateBall(10, 20, 0, 0, 5, al_map_rgb(0,0,255));
Engine.BManager.CreateBall(11, 21, 1, 1, 5, al_map_rgb(0,0,255));
Engine.BManager.CreateBall(12, 22, 2, 2, 5, al_map_rgb(0,0,255));
Engine.BManager.CreateBall(13, 23, 3, 3, 5, al_map_rgb(0,0,255));
ALLEGRO_EVENT events;
int selected = 0; //Used to show which ball is selected
al_start_timer(Engine.timer);
while(Engine.getIsRunning())
{
al_wait_for_event(Engine.event_queue, &events);
if(events.type == ALLEGRO_EVENT_KEY_DOWN)
{
//Keyboard Input
switch(events.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
Engine.setIsRunning(false);
break;
case ALLEGRO_KEY_RIGHT:
Engine.showDebug = !Engine.showDebug; //Toggles the selected balls info
break;
case ALLEGRO_KEY_UP:
selected++;
break;
case ALLEGRO_KEY_DOWN:
selected--;
break;
case ALLEGRO_KEY_DELETE:
Engine.BManager.DeleteBall(selected); //Deletes a certain ball
break;
}
}
else if(events.type == ALLEGRO_EVENT_TIMER)
{
//Update
}
//Draw
Engine.BManager.drawBalls();
//Show debug
if(Engine.showDebug == true)
{
Engine.drawBallInfo(10, 10, selected);
}
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
return 0;
}
I am having trouble drawing the balls. In allegro 4, you would pass a buffer on which to draw to and then draw the buffer to the screen. With the code that I have above I am getting an error at the draw() function in the Ball class.
The error reads:
Debug Error!
R6010 -abort() has been called
I also get some information the command prompt:
Assertion failed: addon_initialized, file allegro-git\addons\primitives\primitives.c, line 79
I think I am getting an error because the draw function doesn't have anywhere to draw to because the display was created in the BallEngine Class, but how do I fix it?
Assertion failed: addon_initialized, file allegro-git\addons\primitives\primitives.c, line 79
That's precisely the problem. You haven't initialized the primitives addon.
al_init_primitives_addon()
Also, you should use forward slashes as part of paths (e.g., <allegro5/allegro.h>) because it is cross platform.