PDA

View Full Version : global kbd hook



ArtemS2006
10-08-2009, 01:21 AM
Hi! Concerning programming under linux, i am newbie. I need to make global keyboard hook when my application is working. I found that the best way is to use xkb module of xwindow, but i have no idea how to use this module|: what headers to include, what funcs to use. may be someone could provide an example of working with xkb or something related?

ArtemS2006
10-22-2009, 03:09 AM
here's sample code i found so far but unfortunately it doesn't work on keys that were hooked by other apps.


#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h> // I include this to test return values the lazy way
#include <unistd.h> // So we got the profile for 10 seconds
#include <iostream>

#define NIL (0) // A name for the void pointer

main()
{
// Open the display
Display *dpy = XOpenDisplay(NIL);
assert(dpy);

// Get some colors
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

// Create the window
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, whiteColor, whiteColor);

// Grab F2 key with code 68 (experiment by yourself)
int F2_KEY = 68;
XGrabKey( dpy, F2_KEY, AnyModifier, w, true, GrabModeAsync, GrabModeAsync );

// "Map" the window (that is, make it appear on the screen)
XMapWindow(dpy, w);

// Infinite loop ( hazard thing ;) )
for(;;) {
XEvent e;
XNextEvent(dpy, &e);
if (e.type == KeyPress )
{
printf( "F2 was pressed!!!\n" );
}
}
}

MK27
10-22-2009, 06:26 AM
here's sample code i found so far but unfortunately it doesn't work on keys that were hooked by other apps.


So basically you are looking to screw up the system and/or other applications? No one wants software like that and no one will help you create it.

ArtemS2006
10-22-2009, 07:31 PM
What made you think i'm going to make destructive app?
i've got a task of prohibiting screenshot in linux.
The task consists of 2 things:
- Show a message "screecshot cannot be done when app is launched", when user pushes PrintScreen.
- Make app's window to be unscreenshotable (i asked about it in another topic http://cboard.cprogramming.com/linux-programming/120874-prohibit-screenshots.html).
By the way, this all is not my idea, i'm just doing the job.

Kennedy
10-22-2009, 09:53 PM
Let me confuse this issue for you a bit more -- Shall we?

Which environment are you planning to do this in? How will you stop the user from executing this from the command line? Bottom line: you cannot achieve what you want unless you really protect yourself by limiting yourself to ONE specific environment. Now, which one will that be?

The problem is not as easy as "binding the keyboard up so that no one can do printscreen" as the keyboard is NOT the only way that this could be done. Furthermore, who is to say that I don't have CTRL-BREAK as the defined way to get a screen shot? What if I'm in KDE and you've written this for gnome?

Without seriously mucking around with others' machines, you cannot do this.

ArtemS2006
10-23-2009, 03:24 AM
Let me confuse this issue for you a bit more -- Shall we?

Which environment are you planning to do this in? How will you stop the user from executing this from the command line? Bottom line: you cannot achieve what you want unless you really protect yourself by limiting yourself to ONE specific environment. Now, which one will that be?

The problem is not as easy as "binding the keyboard up so that no one can do printscreen" as the keyboard is NOT the only way that this could be done. Furthermore, who is to say that I don't have CTRL-BREAK as the defined way to get a screen shot? What if I'm in KDE and you've written this for gnome?

Without seriously mucking around with others' machines, you cannot do this.

What do you mean by environment? Currently i'm planning to use XWindow layer. Message on PrintScreen button is just an additional feature, The main question is whether it is possible to create such a window that screenshoting functions will fail. I wrote in http://cboard.cprogramming.com/linux-programming/120874-prohibit-screenshots.html

Kennedy
10-23-2009, 08:27 AM
Examples of an environment are KDE, gnome, xf86, etc, etc. There are about a dozen or so of these.

So, you are not using any environment at all? Your app is to be run as the environment?