C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-08-2009, 01:21 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 5
global kbd hook

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 is offline   Reply With Quote
Old 10-22-2009, 03:09 AM   #2
Registered User
 
Join Date: Oct 2009
Posts: 5
here's sample code i found so far but unfortunately it doesn't work on keys that were hooked by other apps.
Code:
#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" );
        }
    }
}
ArtemS2006 is offline   Reply With Quote
Old 10-22-2009, 06:26 AM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by ArtemS2006 View Post
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 10-22-2009, 07:31 PM   #4
Registered User
 
Join Date: Oct 2009
Posts: 5
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 prohibit screenshots).
By the way, this all is not my idea, i'm just doing the job.
ArtemS2006 is offline   Reply With Quote
Old 10-22-2009, 09:53 PM   #5
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
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.
Kennedy is offline   Reply With Quote
Old 10-23-2009, 03:24 AM   #6
Registered User
 
Join Date: Oct 2009
Posts: 5
Quote:
Originally Posted by Kennedy View Post
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 prohibit screenshots
ArtemS2006 is offline   Reply With Quote
Old 10-23-2009, 08:27 AM   #7
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
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?
Kennedy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Global Keyboard Hook darknite135 C++ Programming 4 02-01-2008 07:36 PM
blocking or passing keys with global hook pmouse Windows Programming 4 08-29-2007 02:54 PM
Problems with global hook! HELP! LMZ Windows Programming 3 08-22-2006 08:09 AM
Global hook only works on one window? CornedBee Windows Programming 5 01-19-2004 12:25 PM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22