8

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.

Zarkonnen
  • 22,200
  • 14
  • 65
  • 81

6 Answers6

4
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class LockingKeyDemo {
    static Toolkit kit = Toolkit.getDefaultToolkit();

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        frame.addWindowListener(new WindowAdapter() {
            public void windowActivated(WindowEvent e) {
                System.out.println("caps lock1 = "
                        + kit.getLockingKeyState(KeyEvent.VK_CAPS_LOCK));

                try {
                    Robot robot = new Robot();
                    robot.keyPress(KeyEvent.VK_CONTROL);
                    robot.keyRelease(KeyEvent.VK_CONTROL);
                } catch (Exception e2) {
                    System.out.println(e2);
                }

                System.out.println("caps lock2 = "
                        + kit.getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
            }
        });

        frame.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                System.out.println("caps lock3 = "
                        + kit.getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
anjanb
  • 12,999
  • 18
  • 77
  • 106
2

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(...)

AlexV
  • 544
  • 9
  • 19
2

Well there are two types of key press detection: event based, and polling. If you poll the keyboard for KEY_PRESSED on startup (through a loop with a sleep.thread(timeInMs) constantly checking if your key is down), then you can detect if it's already pressed on startup.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Karan
  • 1,636
  • 4
  • 19
  • 35
1
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));
}
}
anjanb
  • 12,999
  • 18
  • 77
  • 106
  • where should I import the Toolkit class from? – Amir Arad Oct 05 '08 at 08:05
  • has this solution overcome the problems described in this blog post? It kind of describes this method as unreliable, but it's old.... http://weblogs.java.net/blog/2007/11/02/log-me-log-me-out – slothbear Nov 02 '09 at 04:55
0

I don't know much about Java (mostly code in C#) but what about having a small loader program written in C or something that then launches your Java app with some parameters (like whether or not a certain key is down)?

Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
  • A difficult solution in that the program wants to run on Mac/Windows/Linux, and that I don't know enough C. – Zarkonnen Oct 02 '08 at 00:18
  • @Zarkonnen Does it have JVM bundled with the installer? If the answer is yes, then the `KeyboadUtils.isPressed` would work on Windows, Mac and Linux – AlexV Jul 29 '12 at 23:56
0

So it appears that you can do this, but only for caps lock et al. Hence, I've switched to using caps lock for this purpose. Not perfect, but OK.

Zarkonnen
  • 22,200
  • 14
  • 65
  • 81