سؤال

Is there any solution that corresponds to the problem from the topic. If I press e.g. Windows key + Space, I get:

keyTyped: Windows

I guess it should be:

keyTyped: Windows
keyTyped: Space
keyReleased: Windows
keyReleased: Space

My code:

        <Java component>.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
            e.consume();
        }

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("keyTyped: " + KeyEvent.getKeyText(e.getKeyCode()));
            e.consume();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println("keyReleased: " + KeyEvent.getKeyText(e.getKeyCode()));
            e.consume();
        }
    });

Thanks!

هل كانت مفيدة؟

المحلول

WINDOWS + Space is a valid global hotkey for the Windows system and therefore is not notifying your program (and will never.) It detects the first stroke because WINDOWS key is not a hotkey (and only unfolds the Start menu upon release.) That's the reason you don't get the release either.

I'm not aware if Java allows for system-wide hooks, but you'll probably have to mess with JNI, which I assume you don't want to deal with.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top