tags:

views:

297

answers:

6

Is it possible to listen for a certain hotkey ( e.g:Ctrl-I ) and then perform a specific action ? My application is written in C , will only run on Linux , and it doesn't have a GUI . Are there any libraries that help with this kind of task ?

EDIT:as an example,amarok has global shortcuts , so for example if you map a combination of keys to an action ( let's say Ctrl-+ , Ctrl and + ) you could execute that action when you press the keys . If I would map Ctrl-+ to the volume increase action , each time I press ctrl-+ the volume would increase by a certain amount .

Thanks

+3  A: 

How global do your hotkeys need to be? Is it enough for them to be global for a X session? In that case you should be able to open an Xlib connection and listen for the events you need.

Ordinarily keyboard events in X are delivered to the window that currently has the focus, and propagated up the hierarchy until they are handled. Clearly this is not what we want. We need to process the event before any other window can get to it. We need to call XGrabKey on the root window with the keycode and modifiers of our hotkey to accomplish this.

I found a good example here http://ubuntuforums.org/showpost.php?p=5419687&postcount=1.

smoofra
Would you please detail a bit?
Vhaerun
A: 

In UNIX, your access to a commandline shell is via a terminal. This harks back to the days when folks accessed their big shared computers literally via terminals connected directly to the machines (e.g. by a serial cable).

In fact, the 'xterm' program or whatever derivative you use on your UNIX box is properly referred to as a terminal emulator - it behaves (from both your point of view and that of the operating system) much like one of those old-fashioned terminal machines.

This makes it slightly complicated to handle input in interesting ways, since there are lots of different kinds of terminals, and your UNIX system has to know about the capabilities of each kind. These capabilities were traditionally stored in a termcap file, and I think more modern systems use terminfo instead. Try

man 5 terminfo

on a Linux system for more information.

Now, the good news is that you don't need to do too much messing about with terminal capabilities, etc. to have a commandline application that does interesting things with input or windowing features. There's a library, curses, that will help. Lookup

man 3 ncurses

on your Linux system for more information. You will probably be able to find a decent tutorial on using curses online.

Incident
A: 

I don't think you understood , Cristian . Imagine the following situation : I start the application , and I let it run in the background . Meanwhile , I may be surfing a web page , writing some documents or just writing code . I want to be able to press a combination of keys and have the code respond to it .

Let's say for example , I want the application to save it's progress if I press a certain combination of keys . For that matter , I don't want to necessarily have the terminal/console in the foreground . So , think of it kinda like the global hotkeys amarok offers .

Vhaerun
Ok. Then could you please edit the question and give the amarok example? And maybe also specify that you're not interested in terminal/curses based solutions.
Cristian Ciupitu
A: 

One way to do it is to have your application listen on a certain port, or socket file, for incoming requests.

Then you can write a small client application that connects to that port or socket file and sends commands to the running application.

Then you can configure your window manager to bind certain key combinations to launch your small client app.

Chris AtLee
I considered that , but since amarok and other applications are able to do it , there must be a way for me to do it too.
Vhaerun
+2  A: 

I think smoofra is on the right track here; you're looking to register a global hotkey with X so that you can intercept keypresses and take appropriate action. Xlib is probably what you want, and XGrabKey is the function, i think.

It's not easy to learn, I'm afraid; I did locate this example that seems useful: TinyWM. I also found an example using Java/JNI (accessing the same underlying Xlib function).

zweiterlinde
+1  A: 

You should look at the source code of xbindkeys.

Xlib programming is pretty arcane, documentation is hard to find, and there are subtle portability issues. You'll be better off copying some battle-hardened code.

ddaa