#include "keyboardmousesimulator.h"
KeyboardMouseSimulator::KeyboardMouseSimulator()
{
display = XOpenDisplay(NULL);
if(display == NULL)
{
std::cout << "Error XOpenDisplay !!!" << std::endl;
}
}
KeyboardMouseSimulator::~KeyboardMouseSimulator()
{
XCloseDisplay(display);
}
void KeyboardMouseSimulator::mouseMove(int x, int y)
{
Window win;
if( !(win = DefaultRootWindow( display )) )
{
std::cout << "Error DefaultRootWindow !!!" << std::endl;
}
XWarpPointer(display, None, win, 0, 0, 0, 0, x, y);
XFlush(display);
}
void KeyboardMouseSimulator::mouseClick(int button)
{
XEvent event;
if(display == NULL) return;
memset(&event, 0x00, sizeof(event));
event.type = ButtonPress;
event.xbutton.button = button;
event.xbutton.same_screen = True;
XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
event.xbutton.subwindow = event.xbutton.window;
while(event.xbutton.subwindow)
{
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
}
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
{
std::cout << "XSendEvent error" << std::endl;
}
XFlush(display);
//usleep(100000);
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
{
std::cout << "XSendEvent error" << std::endl;
}
XFlush(display);
}
#ifndef KEYBOARDMOUSESIMULATOR_H
#define KEYBOARDMOUSESIMULATOR_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
class KeyboardMouseSimulator
{
public:
KeyboardMouseSimulator();
~KeyboardMouseSimulator();
void mouseMove(int x, int y);
void mouseClick(int button);
private:
Display *display;
};
#endif // KEYBOARDMOUSESIMULATOR_H