You are not logged in.

#1 2006-08-08 00:25:47

toxic
Member
Registered: 2006-06-05
Posts: 117

Trapping keystroke events

Hi..

I'd like to type a small program which listens to keystroke events and executes a small script in response to it.

I'm not sure where to start, and I found it really hard to find anything useful on the net.

Say for instance that I want to capture the buttoncombination
  Fn+F7 on my laptop, and execute some code in response to keypress.

I'm not interested in any existing program for this as I would just like to learn how to do it.

I realise that xev can give me some information on keypress. But that'll mean I have to write a loop which runs xev thousands of times a minute ? (sorry, haven't ready a whole lot about xev yet, just had my HD replaced the other day so I can't check it until the next day). Anyway, my point is that I would like to avoid polling.
This probably requires a java/c solution, and either is fine. (I doubt bash has mechanisms for  this). Any pointers to documentation or similiar will be appreciated.

Thanks.

Offline

#2 2006-08-08 03:21:58

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: Trapping keystroke events

I think you're looking for xbindkeys...it's in the repos.

Offline

#3 2006-08-08 17:08:10

toxic
Member
Registered: 2006-06-05
Posts: 117

Re: Trapping keystroke events

Seems like a fine program. Second best to typing one myself smile

Offline

#4 2006-08-08 21:00:47

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: Trapping keystroke events

toxic wrote:

I'm not interested in any existing program for this as I would just like to learn how to do it.

DOH!  :oops: I need to learn how to read!

Offline

#5 2006-08-08 22:55:35

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Trapping keystroke events

random crap:

Display *dpy = XOpenDisplay(NULL);
XEvent xev;
XGrabKey(dpy, AnyKey, AnyModifier, RootWindow(dpy), GrabModeAsync, True, GrabModeAsync);
XNextEvent(dpy,&xev);
switch(xev.type)
{
case KeyRelease:
case KeyPress:
    KeySym sym = XKeycodeToKeysym(xev.xany.display, xev.xkey.keycode, 0);
    if(sym != NoSymbol) printf("key=%s",XKeysymToString(sym));
    break;
default:
    break;
}

Now.. I wrote this from memory so I'm probably off alot.  You'll also need an XGrabServer call in there to actually grab keyboard input for your app...

You can check just about every WM out there or keygrabbing code.

Offline

#6 2006-08-10 17:19:41

toxic
Member
Registered: 2006-06-05
Posts: 117

Re: Trapping keystroke events

Thanks for the code. I Haven't gotten around to test it yet as my project list is running long smile I'll leave some feedback when I get the chance.

Offline

Board footer

Powered by FluxBB