Is it possible to listen for key and mouse events without having a gui selected by the user? I want to make a program that runs in the background without a gui and responds to user interaction such as pressing ctrl-t
.
-
See if this post helps http://stackoverflow.com/questions/6174119/global-keylistener-using-jna – Bala R Jun 21 '11 at 14:34
2 Answers
The window manger will track the focus and direct input according to its configuration. If your window manager decides to send input to a different program because it is enforcing a change of focus, you are out of luck.
While the program lacks focus, if it were able to obtain the mouse and keyboard events, it would be a major security hole. Basically any small unnoticeable background program could spy on the entire system, possibly even stealing passwords in the process.
Your only hope is to find an option in your window manager which will allow you to minimize the program without changing focus. Of course, whatever you type will only go the minimized program (and not affect the rest of the system).
Another technique (by no means guaranteed) is for the program to detect minimization events, and to request focus within a minimization event handler. Note that doing so would probably (If I were writing a window manager) unminimize the window.

- 69,361
- 7
- 100
- 138
-
1well i dont see the big deal about it being a security thread because almost all other computer languages have this abilty. – Stas Jaro Jun 21 '11 at 14:48
-
1All computer languages have the ability to receive focus; however, they can only request focus when they don't have it. They can't just willy-nilly decide to eavesdrop on some other program's input unless the security model allows them to do so (or is broken). Even then, it's not really a feature of the language, it is a feature of the operating system's windowing interface. – Edwin Buck Jun 21 '11 at 17:09
-
1Edwin, tons of legitimate useful applications listen for key presses in the background. This is possible. I'm looking for the win32 API hook to make this happen and I'll post back here when I find it. – Michael McHenry Feb 19 '13 at 18:23
-
@MichaelMcHenry If there are tons of legitimate useful applications that need to listen to another application's input in the background (and are not wilful security breaches), please take the time to post a list. – Edwin Buck Feb 20 '13 at 03:18
-
@EdwinBuck For one easy example, screenshot programs / screencasting programs, you minimize it and they continue to record the window. – Ali Sep 14 '13 at 21:24
-
I did use the win32 API to make this happen and it worked great. It was a bit of trial and error until I got it right. I don't have the source in front of me to say what exactly it was that I did, but the client was happy with the result. – Michael McHenry Apr 14 '14 at 22:22
You probably would like to look at jnativehook. Jnativehook allows you to listen for keyboard events using JNA without an existing focused GUI.
https://github.com/chrislgarry/Apollo-11 https://github.com/kwhat/jnativehook/blob/2.2/doc/ConsumingEvents.md
Example usage:
try {
GlobalScreen.registerNativeHook()
} catch (NativeHookException ex) {
System.out.println(ex.message)
}
GlobalScreen.addNativeKeyListener(
new NativeKeyListener() {
@Override
public void nativeKeyTyped(NativeKeyEvent nativeEvent) {
System.out.println("Pressed " + nativeEvent.keyChar)
}
}
)

- 101
- 1
- 11