<p>Is it possible to listen for a certain hotkey (e.g:<kbd>Ctrl</kbd><kbd>-</kbd><kbd>I</kbd>) 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?</p>

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

<p>Thanks</p>

[{'answer_id': 74988, 'author': "Lawrence D'Anna", 'author_id': 10168, 'author_profile': 'https://Stackoverflow.com/users/10168', 'pm_score': 3, 'selected': True, 'text': '<p>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.</p>\n\n<p>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 <a href="http://tronche.com/gui/x/xlib/input/XGrabKey.html" rel="nofollow noreferrer">XGrabKey</a> on the root window with the keycode and  modifiers of our hotkey to accomplish this.</p>\n\n<p>I found a good example <a href="http://ubuntuforums.org/showpost.php?p=5419687&amp;postcount=1" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 74989, 'author': 'Incident', 'author_id': 11613, 'author_profile': 'https://Stackoverflow.com/users/11613', 'pm_score': 0, 'selected': False, 'text': "<p>In UNIX, your access to a commandline shell is via a <strong>terminal</strong>. 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).</p>\n\n<p>In fact, the 'xterm' program or whatever derivative you use on your UNIX box is properly referred to as a <strong>terminal emulator</strong> - it behaves (from both your point of view and that of the operating system) much like one of those old-fashioned terminal machines.</p>\n\n<p>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 <strong>termcap</strong> file, and I think more modern systems use <strong>terminfo</strong> instead. Try</p>\n\n<pre><code>man 5 terminfo\n</code></pre>\n\n<p>on a Linux system for more information.</p>\n\n<p>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, <strong>curses</strong>, that will help. Lookup </p>\n\n<pre><code>man 3 ncurses\n</code></pre>\n\n<p>on your Linux system for more information. You will probably be able to find a decent tutorial on using curses online.</p>\n"}, {'answer_id': 75025, 'author': 'Chris AtLee', 'author_id': 4558, 'author_profile': 'https://Stackoverflow.com/users/4558', 'pm_score': 1, 'selected': False, 'text': '<p>One way to do it is to have your application listen on a certain port, or socket file, for incoming requests.</p>\n\n<p>Then you can write a small client application that connects to that port or socket file and sends commands to the running application.</p>\n\n<p>Then you can configure your window manager to bind certain key combinations to launch your small client app.</p>\n'}, {'answer_id': 75226, 'author': 'zweiterlinde', 'author_id': 6592, 'author_profile': 'https://Stackoverflow.com/users/6592', 'pm_score': 2, 'selected': False, 'text': '<p>I think <a href="https://stackoverflow.com/questions/74790/is-there-a-way-for-my-binary-to-react-to-some-global-hotkeys-in-linux#74988">smoofra</a> 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.  <a href="http://tronche.com/gui/x/xlib/" rel="nofollow noreferrer">Xlib</a> is probably what you want, and <a href="http://tronche.com/gui/x/xlib/input/XGrabKey.html" rel="nofollow noreferrer">XGrabKey</a> is the function, i think.</p>\n\n<p>It\'s not easy to learn, I\'m afraid; I did locate this example that seems useful: <a href="http://incise.org/tinywm.html" rel="nofollow noreferrer">TinyWM</a>.  I also found an <a href="http://ubuntuforums.org/showthread.php?p=5787060" rel="nofollow noreferrer">example</a> using Java/JNI (accessing the same underlying Xlib function).</p>\n'}, {'answer_id': 75485, 'author': 'ddaa', 'author_id': 11549, 'author_profile': 'https://Stackoverflow.com/users/11549', 'pm_score': 2, 'selected': False, 'text': '<p>You should look at the source code of <a href="http://hocwp.free.fr/xbindkeys/xbindkeys.html" rel="nofollow noreferrer">xbindkeys</a>.</p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/74790', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11234/']