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;
}
Related
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));
I want to print something in the position where the mouse cursor is, so I use POINT cursorPos; GetCursorPos(&cursorPos);
to get the position of mouse cursor.
Then I set the console cursor to the position, and print the mouse coordinates. However the result is not correct.
Here's the code:
#include<iostream>
#include<Windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
void gotoxy(int column, int line){
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE),
coord
);
}
int main(){
while (1){
POINT cursorPos;
GetCursorPos(&cursorPos);
system("pause");
gotoxy(cursorPos.x, cursorPos.y);
cout << cursorPos.x << " " << cursorPos.y;
}
}
Thank U~
Use GetConsoleScreenBufferInfo to find the cursor position in console window. See this example
Tracking mouse position in a console program may not be useful. If you really need the position of mouse pointer, you have to convert from desktop coordinates to to console window coordinates.
Get the console window's handle GetConsoleWindow()
Use ScreenToClient to convert mouse pointer position from screen to client. Map the coordinates to CONSOLE_SCREEN_BUFFER_INFO::srWindow
COORD getxy()
{
POINT pt;
GetCursorPos(&pt);
HWND hwnd = GetConsoleWindow();
RECT rc;
GetClientRect(hwnd, &rc);
ScreenToClient(hwnd, &pt);
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO inf;
GetConsoleScreenBufferInfo(hout, &inf);
COORD coord = { 0, 0 };
coord.X = MulDiv(pt.x, inf.srWindow.Right, rc.right);
coord.Y = MulDiv(pt.y, inf.srWindow.Bottom, rc.bottom);
return coord;
}
int main()
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
while (1)
{
system("pause");
COORD coord = getxy();
SetConsoleCursorPosition(hout, coord);
cout << "(" << coord.X << "," << coord.Y << ")";
}
}
Here is my code which blinks 'Welcome' after user enter his name.
'Welcome' does not blink when user is writing his name. As user hits enter then caret goes into the while loop. Then caret position is set back to the coordinates of 'Welcome' & cout prints 'Welcome' with 5 colors again & again so it seems that 'Welcome' is blinking.
But I want that 'Welcome' blinks continuously as the program starts.
So more likely this question also ask - can we have two caret/cursor at the same time ?
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int main(int argc, char** argv)
{
int x,y,i;char name[10];
textcolor(10);
x=wherex();y=wherey(); //corrdinates of caret will be stored in x & y.
cout<<"\t\t\t\tWelcome\n";
textcolor(15);
cout<<"\nEnter your name\n";
gets(name);
while(1)
{
for(i=10;i<15;i++)
{
textcolor(i);
gotoxy(x,y); //Transferring caret to the coordinates stored in x & y.
cout<<"\t\t\t\tWelcome";
Sleep(300);
}
}
return 0;
}
I wrote a small code for this question , it's not 100% correct answer. I am just posting this answer just for giving little bit idea to newbie.
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int x,y,b,l,n=0;
char c;
void blink()
{
{
int m;
for(m=10;m<15;m++)
{
textcolor(m);
gotoxy(x,y);
cout<<"\t\t\t\tWelcome";
Sleep(60);
}
}
}
int main(int argc, char** argv)
{
char i;int key_stroke;
textcolor(10);
x=wherex();y=wherey();
cout<<"\t\t\t\tWelcome\n";
textcolor(15);
cout<<"\nEnter your name\n";
l=wherex();b=wherey();
z:
{
while (1)
{
if(!(_kbhit()))
{
blink();
goto z;
}
else
{
i=_getch();
if(i==13)
{
gotoxy(l+n,b+1);
return 0;
}
textcolor(10);
gotoxy(l+n,b);
cout<<i;n=n+1;
}
}
}
return 0;
}
No we cant have two caret/cursor at the same time. User inputs name first.
It begins to blink right after the user has pressed the enter key
by first displaying the text in a given color and time delay.
Then after it sets the color to black and overwrites the text wth black color.
Windows code:
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y);
void setcolor(WORD color);
void clrscr();
int main(int argc, char** argv){
int x,y,i;char name[10];
setcolor(10);
cout<<"Welcome\n";
setcolor(15);
cout<<"\nEnter your name ";
gets(name);
i=0;
x=22;
y=12;
while(1) {
// counter for text color
i++; if (i>15) i=1;
// print colored text
setcolor(i);
gotoxy(x,y);
cout<<"Welcome "<<name;
Sleep(100);
// Print black text to simulate blink
setcolor(0);
gotoxy(x,y);
cout<<" ";
Sleep(100);
}
setcolor(7);
gotoxy(1,24);
return 0;
}
void setcolor(WORD color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
return;
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
Instead of such Codes you can use BLINK in textcolor() function. the only problem is that you cannot control its speed. thats it. otherwise its easy to use and you can also set text color.
Eg.
textcolor ( RED + BLINK ) ;
cprintf ( " /t/t WELCOME " ) ;
thats it. I didn't had time to read your full question and program. Also I am just a newbie. So I hope this helps you and others.
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;
}
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;
}