How to create a background program that monitors input (linux)

Essentially, I want to create a program that is simply ran in the background, not in a terminal, that monitors keypresses and mouse presses and takes certain actions on specific keypresses. Where to start?

Do you wish to do that in an active Xorg GUI session? If yes, the X API (and corresponding Haskell packages) will allow you to do that; otherwise you usually have to read directly from the device files (e.g. /dev/input*) which usually requires root. On Tue 29 Apr 2014 15:55:00 BST, EatsKittens wrote:
Essentially, I want to create a program that is simply ran in the background, not in a terminal, that monitors keypresses and mouse presses and takes certain actions on specific keypresses. Where to start?

Yes, from within the X server. I've used Xlib in the past but this seemed
mostly limited to window management and keypresses when a specific terminal
in which a program is running has focus. I never came across a way to
simply monitor arbitray keypresses from a background program and take
action depending on them. For instance running a system command whenever
ctrl+alt+r is pressed.
On 29 April 2014 17:13, Niklas Hambüchen
Do you wish to do that in an active Xorg GUI session?
If yes, the X API (and corresponding Haskell packages) will allow you to do that; otherwise you usually have to read directly from the device files (e.g. /dev/input*) which usually requires root.
On Tue 29 Apr 2014 15:55:00 BST, EatsKittens wrote:
Essentially, I want to create a program that is simply ran in the background, not in a terminal, that monitors keypresses and mouse presses and takes certain actions on specific keypresses. Where to start?

On Tue, Apr 29, 2014 at 11:19 AM, EatsKittens wrote: Yes, from within the X server. I've used Xlib in the past but this seemed
mostly limited to window management and keypresses when a specific terminal
in which a program is running has focus. I never came across a way to
simply monitor arbitray keypresses from a background program and take
action depending on them. For instance running a system command whenever
ctrl+alt+r is pressed. You're looking for passive key grabs,
http://hackage.haskell.org/package/X11/docs/Graphics-X11-Xlib-Misc.html#v:gr...)
for Xlib manpage).
--
brandon s allbery kf8nh sine nomine associates
allbery.b@gmail.com ballbery@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Tue, Apr 29, 2014 at 11:25 AM, Brandon Allbery
I never came across a way to simply monitor arbitray keypresses from a background program and take action depending on them. For instance running a system command whenever ctrl+alt+r is pressed.
I should mention that this mechanism you're trying to use does not exist in Xlib. It can be done via the RECORD extension. I do not recommend it; among other things, X11 does not give programs "veto control" of event delivery --- so you could in theory use RECORD to do this (with significantly increased load over using the proper mechanism since you are seeing all key events) but you cannot prevent the triggering key(s) from also being delivered to the focused window. This is the wrong mechanism entirely for such things, operates at the wrong level, and is expensive (and potentially a security issue; consider what "all key events" means; this also means that the RECORD extension is often disabled in the interest of security). -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 2014年04月29日 23:55, EatsKittens wrote:
Essentially, I want to create a program that is simply ran in the background, not in a terminal, that monitors keypresses and mouse presses and takes certain actions on specific keypresses. Where to start?
Perhaps you can get inspiration from XMonad [1]: http://code.haskell.org/xmonad/XMonad/Main.hsc s/^grabKeys ::/ Cheers, Travis ---- [1] http://xmonad.org/
participants (4)
-
Brandon Allbery
-
EatsKittens
-
Niklas Hambüchen
-
Travis Cardwell