JFrame Close to Background and Listen to Keys

前端 未结 2 1842
耶瑟儿~
耶瑟儿~ 2021-01-17 05:33

Working on a new personal project with jframe. My goal is to close the frame in an ActionListener to the background, and when specific keys are pressed (Ctrl+

2条回答
  •  鱼传尺愫
    2021-01-17 06:02

    1. Is this the best way to do it? I'm trying to keep the CPU usage as low as possible.

    As previously mentioned, use JNativeHook. It is the only cross-platform solution and it will be much faster and less intensive than a polling approach like while (1) { GetAsyncKeyState(...); Sleep(5); } The biggest performance bottleneck with JNativeHook is the OS, not the library.

    1. Will the ActionListener even work while the frame's not visible?

    It will not work unless the frame has focus, but the native library will provide other events that do fire out of focus, so you could make it work by fabricating your own ActionEvent's from the NativeInputEvent listeners. Just make sure you set the library to use the Swing event dispatcher as it does not by default!

    1. How do I listen to multiple key presses? I have an idea but it doesn't sound like it will work.

    What do you mean by "multiple key presses?" If you mean auto-repeat when a key is held down, that is handled by sending multiple Key Pressed events after the auto repeat delay is exceeded at an interval of the auto repeat rate. You many also receive multiple Key Typed events if that event produces a printable character. When the key is released, a single key release event will be dispatched. If you mean a sequence of keys or multiple keys at the same time, you will need to do your own tracking or checks in the native input listener, but it should be possible.

    Basic Modifier Example: Note that JNativeHook library has both a left and right mask for the modifier keys. I assume you want to use a combination of either the left or the right which makes this a tad more complicated.

    public void nativeKeyPressed(NativeKeyEvent e) {
        // If the keycode is L
        if (e.getKeyCode() == NativeKeyEvent.VK_L) {
            // We have a shift mask and a control mask for either the left or right key.
            if (e.getModifiers() & NativeInputEvent.SHIFT_MASK && e.getModifiers() & NativeInputEvent.CTRL_MASK) {
                // Make sure you don't have extra modifiers like the meta key.
                if (e.getModifiers() & ~(NativeInputEvent.SHIFT_MASK | NativeInputEvent.CTRL_MASK) == 0x00) {
                    ....
                }
            }
        }
    }
    

提交回复
热议问题