How to check for key being held down on startup in Java
awt, java, key, keyboard, swing
Solution
public class LockingKeyDemo {
static Toolkit kit = Toolkit.getDefaultToolkit();
public static void main(String[] args) {
System.out.println("caps lock2 = "
+ kit.getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
}
}
Problem
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 that you can turn off that dialog with a checkbox, but get it back by holding down the alt key at startup. Unfortunately, there is no obvious way to ask java whether a given key is currently being pressed. You can only register to be informed of new key presses via a KeyListener, but that doesn't help if the keypress starts before the app launches.