keyboard hook - Xwindows

This is a discussion on keyboard hook - Xwindows ; Hello, I'm trying to get a keyboard hook in X. My aim is to get the time when some key was last pressed (I don't care what key). How can I do it properly? I found two ways: --snip-- Window ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: keyboard hook

  1. keyboard hook

    Hello,
    I'm trying to get a keyboard hook in X. My aim is to
    get the time when some key was last pressed (I don't care what key).
    How can I do it properly?

    I found two ways:
    --snip--
    Window root,root2,windowIn,dummywin;
    int rx,ry,wx,wy,screen;
    unsigned int modifs;
    int err;
    screen=0;
    root = XRootWindow(dd,screen);
    XSelectInput(dd,root,KeyPressMask);

    int rx2,ry2;
    while(1)
    {
    char keys[33];;
    XQueryKeymap(dd, keys);
    int i;
    for (i=0; i<32; i++) {
    printf("%d ", keys[i]);
    }
    printf("\n");
    }
    --snip--

    The "for" loop prints the keyboard state and works fine, but it requires probing
    the keyboard all the time.

    Another tried method was to read from /dev/input/... (keyboard).
    But that one requires root privileges.


    I want to write a program like xwrits so I have to know if user is doing something or not.
    So any method that reveals that the user is active and he's typing/working would be helpful.

    Regards
    Michael

  2. Re: keyboard hook


    "news" wrote in message
    news:et9stv$cd0$1@mx1.internetia.pl...
    > Hello,
    > I'm trying to get a keyboard hook in X. My aim is to
    > get the time when some key was last pressed (I don't care what key). How
    > can I do it properly?
    >
    > I found two ways:
    > --snip--
    > Window root,root2,windowIn,dummywin; int rx,ry,wx,wy,screen;
    > unsigned int modifs;
    > int err;
    > screen=0;
    > root = XRootWindow(dd,screen);
    > XSelectInput(dd,root,KeyPressMask);
    >
    > int rx2,ry2;
    > while(1) {
    > char keys[33];;
    > XQueryKeymap(dd, keys);
    > int i;
    > for (i=0; i<32; i++) {
    > printf("%d ", keys[i]);
    > }
    > printf("\n");
    > } --snip--
    >
    > The "for" loop prints the keyboard state and works fine, but it requires
    > probing
    > the keyboard all the time.
    >
    > Another tried method was to read from /dev/input/... (keyboard).
    > But that one requires root privileges.
    >
    >
    > I want to write a program like xwrits so I have to know if user is doing
    > something or not.
    > So any method that reveals that the user is active and he's typing/working
    > would be helpful.
    >


    XSelectInput(...)

    loop:
    XtAppNextEvent(app, &event);
    if ( event.type == KeyPress ) {
    time = event.xkey.time;
    }
    XtDispatchEvent( &event );
    endloop

    Be sure to #include the appropriate header(s), and properly declare all
    variables.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing



  3. Re: keyboard hook

    >>
    >> I want to write a program like xwrits so I have to know if user is doing
    >> something or not.
    >> So any method that reveals that the user is active and he's typing/working
    >> would be helpful.
    >>

    >
    > XSelectInput(...)
    >
    > loop:
    > XtAppNextEvent(app, &event);
    > if ( event.type == KeyPress ) {
    > time = event.xkey.time;
    > }
    > XtDispatchEvent( &event );
    > endloop
    >
    > Be sure to #include the appropriate header(s), and properly declare all
    > variables.


    Fred,
    Thank you, but I believe I have tried a similar solution and it
    reported only keypresses from my application, while I need to
    know about keypresses from ALL applications. My program is to
    run in the background and it needs to know that the user is
    typing something somewhere.

    Regards
    Michael

  4. Re: keyboard hook

    OK, I have found a solution:

    #include

    .....

    while(1)
    {

    Time idleTime = 0; /* millisecs since last input event */

    static XScreenSaverInfo* mitInfo = 0;
    if (!mitInfo) mitInfo = XScreenSaverAllocInfo ();
    XScreenSaverQueryInfo (dd, DefaultRootWindow (dd), mitInfo);
    idleTime = mitInfo->idle;

    printf("%d\n", idleTime/1000);
    }

    Regards
    Michael

  5. Re: keyboard hook

    > I need to know about keypresses from ALL applications. My program is to
    > run in the background and it needs to know that the user is typing
    > something somewhere.


    If you merely need to know IF the user is typing, not WHAT the user is
    typing, have a look at any of the "screenblank" programs. They detect
    keyboard and mouse activity and only activate after X minutes of
    inactivity. (Maybe some hack involving /dev/console ?)
    -WBE

  6. Re: keyboard hook

    On Mar 15, 11:23 pm, news wrote:
    > OK, I have found a solution:
    >
    > #include
    > ...


    You could also use Record extension, it's very easy to use and gives
    more information, e.g. in which window key was pressed.

    --
    Regards
    Mariusz


+ Reply to Thread