Модератор: Модераторы
#include <string>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
using namespace std;
/// ----------------- GLOBAL VARIABLE -------------------------------- ///
Display *dpy;
int screen;
bool CTRL_L = false;
/// -------------------------------------------------------------------///
//////////////////////////////////////////////////////////////
// Function to create a keyboard event
//////////////////////////////////////////////////////////////
XKeyEvent createKeyEvent(Display *display, Window &win,
Window &winRoot, bool press,
KeyCode keyCode, int modifiers)
{
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = True;
event.keycode = keyCode;
event.state = modifiers;
if(press)
event.type = KeyPress;
else
event.type = KeyRelease;
return event;
}
//////////////////////////////////////////
/// PRESS KEY
//////////////////////////////////////////
void PressKey(XEvent *eventp, vector<string> keys, vector<string> keysSend)
{
Display *display = XOpenDisplay(NULL);
XKeyEvent *e = (XKeyEvent *) eventp;
if(display == NULL)
{
fprintf(stderr, "Errore nell'apertura del Display !!!\n");
exit(EXIT_FAILURE);
}
// Get the root window for the current display.
Window winRoot = XDefaultRootWindow(display);
// Find the window which has the current keyboard focus.
Window winFocus;
int revert;
XGetInputFocus(display, &winFocus, &revert);
for(int i=0 ; i < (int)keys.size()-1; i++)
{
if(e->keycode == XKeysymToKeycode(dpy,XStringToKeysym(keys[i].c_str())))
{
e->keycode = XKeysymToKeycode(dpy,XStringToKeysym(keysSend[i].c_str()));
break;
}
}
// Send a fake key press event to the window.
XKeyEvent event = createKeyEvent(display, winFocus, winRoot, true, e->keycode, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, e->keycode, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Done.
XCloseDisplay(display);
}
/// <summery>
/// Mouse click.
/// </summery>
static void mouseClick(int button)
{
Display *display = XOpenDisplay(NULL);
XEvent event;
if(display == NULL)
{
fprintf(stderr, "Errore nell'apertura del Display !!!\n");
exit(EXIT_FAILURE);
}
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) printf("Errore nell'invio dell'evento !!!\n");
XFlush(display);
usleep(100000);
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) printf("Errore nell'invio dell'evento !!!\n");
XFlush(display);
XCloseDisplay(display);
}
/// <summery>
/// Do Key Press.
/// </summery>
static bool MouseButtonClick (XEvent *eventp, vector<string> &symbols)
{
XKeyEvent *e = (XKeyEvent *) eventp;
if(e->keycode == XKeysymToKeycode(dpy,XStringToKeysym(symbols[0].c_str())))
{
mouseClick(1);
return true;
}
if(e->keycode == XKeysymToKeycode(dpy,XStringToKeysym(symbols[1].c_str())))
{
mouseClick(3);
return true;
}
/////////// CTRL_l + C EXIT
// ---------------------------------------------------------------------
if((e->keycode == XKeysymToKeycode(dpy,XStringToKeysym("C")))&&
(e->keycode == XKeysymToKeycode(dpy,XStringToKeysym("c")))&&
(CTRL_L))
{
exit(0);
}
if(e->keycode == XKeysymToKeycode(dpy,XStringToKeysym("Control_L")))
{
CTRL_L = true;
}
else
{
CTRL_L = false;
}
// ----------------------------------------------------------------------
return false;
}
///////////////////////////////////
// MAIN FUNCTION
///////////////////////////////////
int main (int , char**)
{http://forum.sources.ru/index.php?showtopic=263626
string strTemp;
vector<string> symbolMouseKey;
vector<string> symbolKey;
vector<string> symbolKeySend;
int item=0;
ifstream fin ("key.txt");
while ( !fin.eof() )
{
fin >> strTemp;
if(item<2)
symbolMouseKey.push_back( strTemp );
else
{
symbolKey.push_back(strTemp);
strTemp.clear();
fin >> strTemp;
symbolKeySend.push_back(strTemp);
}
item++;
strTemp.clear();
}
dpy = XOpenDisplay (NULL);
if (!dpy) {
fprintf (stderr, "Unable to open display.\n");
exit (1);
}
screen = DefaultScreen (dpy);
XGrabKeyboard(dpy, DefaultRootWindow(dpy), False, GrabModeAsync, GrabModeAsync, CurrentTime);
/* Terminate after twenty events, so the user won't have problems with ungrabbing. */
for (;;) {
XEvent event;
XNextEvent (dpy, &event);
switch (event.type) {
case KeyPress:
if(!MouseButtonClick (&event, symbolMouseKey))
{
PressKey(&event, symbolKey, symbolKeySend);
}
break;
default:
break;
}
//XFilterEvent(event, event.window);
}
XCloseDisplay (dpy);
return 0;
}
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 24