-
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
-
Re: keyboard hook
"news" <abuse@inetia.pl> wrote in message
news:et9stv$cd0$1@mx1.internetia.pl...[color=blue]
> 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.
>[/color]
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
-
Re: keyboard hook
>>[color=blue][color=green]
>> 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.
>>[/color]
>
> 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.[/color]
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
-
Re: keyboard hook
OK, I have found a solution:
#include <X11/extensions/scrnsaver.h>
.....
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
-
Re: keyboard hook
> I need to know about keypresses from ALL applications. My program is to[color=blue]
> run in the background and it needs to know that the user is typing
> something somewhere.[/color]
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
-
Re: keyboard hook
On Mar 15, 11:23 pm, news <a...@inetia.pl> wrote:[color=blue]
> OK, I have found a solution:
>
> #include <X11/extensions/scrnsaver.h>
> ...[/color]
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