Thread: global kbd hook

  1. #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?

  2. #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" );
            }
        }
    }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #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.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

  6. #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

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

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