我有这个代码可以全局监听键盘消息:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <X11/Xlib.h>
#include <time.h>
using namespace std;
char keymap[32];
char prevKeymap[32];
bool hasTyped(Display* display)
{
XQueryKeymap(display, keymap);
for(int i=0; i<32; i++)
{
if (prevKeymap[i]!=keymap[i])
{
XQueryKeymap(display, prevKeymap);
return true;
}
}
return false;
}
int main(int , char *[])
{
//setBrightness(defaultBrightness);
Display *display;
Window root, fromroot, tmpwin;
int x, y,prev_x,prev_y, tmp;
uint tmp2;
if ((display = XOpenDisplay(NULL)) == NULL)
{
fprintf(stderr, "xpointerswap: fatal: cannot open local X display\n");
exit(100);
}
root = DefaultRootWindow(display);
XQueryPointer(display, root, &fromroot, &tmpwin, &prev_x, &prev_y, &tmp, &tmp, &tmp2);
XQueryKeymap(display, prevKeymap);
while(1)
{
XQueryPointer(display, root, &fromroot, &tmpwin, &x, &y, &tmp, &tmp, &tmp2);
if(x!=prev_x || y!=prev_y || hasTyped(display))
{
prev_x = x;
prev_y = y;
cout<< "(" << x << "," << y << ")\n";
}
}
return EXIT_SUCCESS;
}
它会侦听键盘消息,但是当按下某个键时,它会键入鼠标指针的位置(我希望它代替打印键码)。如何更改它以便打印键码(当按下某个键时)?