How to check for key being held down on startup in Java

后端 未结 6 530
时光说笑
时光说笑 2020-12-11 04:11

I\'m trying to write a resolution selection dialog that pops up when a program first starts up. To prevent boring the user, I want to implement the fairly standard feature t

6条回答
  •  囚心锁ツ
    2020-12-11 04:37

    The original question seems to be not answered. The proposed method determines the locking key state like CapsLock, ScrollLock, etc. So it would not work for Alt pressed state.

    Consider the following code:

    com.sun.jna.platform.KeyboardUtils.isPressed(java.awt.event.KeyEvent.VK_ALT);

    The only problem is that this class is an internal Sun's JDK class and not likely to be available in any other JVM. Depend on your project it may or may not be acceptable.

    Internally it calls into User32.DLL on Windows:

    User32.INSTANCE.GetAsyncKeyState(...)

提交回复
热议问题