1

I created a Java application that uses GlobalScreen to addNativeKeyListener. (jnativehook library)

When i press F5 it executes a code. Problem is that if i give focus to other applications it also executes funciton for the focused application. (for example Chrome will refreshes website)

Is there a way to suppress these functions and give my application full control over F5?

I use this to listen for F5 keyPress globaly (even if my application loses focus):

public class Listen implements NativeKeyListener {
    @Override
    public void nativeKeyReleased(NativeKeyEvent arg0) {
        if (arg0.getKeyCode() == NativeKeyEvent.VC_F5) {
            Main.executeCode();
        }
    }
}

And from my main i have to call:

try {
     GlobalScreen.registerNativeHook();
    }
     catch (NativeHookException ex){
    }
     GlobalScreen.addNativeKeyListener(new Listen());
  • You mean even if your app loses focus and you have focus on Chrome, you want that F5 goes to your app and not chrome? And right now it goes to where, both applications or only Chrome? Can you also share your code? – Zabuzard Apr 17 '20 at 15:06
  • 1
    @Zabuza exactly. It goes to both aplications right now. The global screen allows me to lose focus of my application but still listen to keypress. – FilipDolezal Apr 17 '20 at 15:08
  • 1
    @Zebuza I updated the question. you should see the code now. – FilipDolezal Apr 17 '20 at 15:19
  • If your application could gain exclusive control of the F keys, what would stop a different application (say Chrome) from gaining exclusive control of the F keys away from your application? – Gilbert Le Blanc Apr 17 '20 at 18:47
  • @GilbertLeBlanc There is an application that does exactly what i want. Its called MacroGamer. its for making macros that get executed on keypress. You macro to a key. you can execute the macro globaly. the key also doesnt do anything beside executing the macro. so if you bind macro to F5 it doesnt refresh the website. another example is if you bound the macro to 'w'. Now you wouldnt be able to write 'w' because the program gains exclusive control of the 'w' key. – FilipDolezal Apr 17 '20 at 19:18

0 Answers0