简体   繁体   中英

Listen to keystrokes if window is not on top

Alright so I have a JFrame that I have a KeyListener added to, and I set the focusable equal to true. There is no problem with the KeyListener but it only listens to keys if the window is on top. What I'm trying to do is listen to the key strokes if the window is not on top. Basically, I'm trying to make a program that executes the code when the user presses F10 and stops executing the code when the user presses F10 again. The window shouldn't have to be on top for this program to work, meaning that if I press F10 and the JFrame is not on top, it will still execute. Any help would be greatly appreciated! Thank you.

No, Key Bindings/KeyListeners/any pure Java application will not work as the application must be on top for these to work.

You're looking for a keyboard hook, and Java by itself cannot do this. You'll have to use either JNI, JNA that are linked to a native app such as a C/C++ app, or easier still link Java to a scripting utility such as AutoIt (if Windows, or a similar utility if not). Note that any and all of these solutions must be platform specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM