As far as I know, there is no way in the Java API to detect a double keyboard press. While the mouse click events can detect a double or single click, there is no way with a KeyEvent class except to make one from scratch.
Here is a class I made to detect a double press on any key of the keyboard. If you notice, there is a variable called lastKeyPressedCode which records the last key pressed. With this class, you can easily detect a double key press on the same character by doing this in code:
|
1 2 3 4 |
// where ke is the KeyEvent variable if (KeyPressTool.isDoublePress(ke) && KeyPressTool.lastKeyPressedCode == ke.getKeyCode()) { System.out.println("double pressed " + ke.getKeyText(ke.getKeyCode()); } |
Here is the KeyPressTool class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class KeyPressTool { private static int doublePressSpeed = 300; // double keypressed in ms private static long timeKeyDown = 0; // last keyperessed time public static int lastKeyPressedCode; public static boolean isDoublePress(KeyEvent ke) { if ((ke.getWhen() - timeKeyDown) < doublePressSpeed) { return true; } else { timeKeyDown = ke.getWhen(); } lastKeyPressedCode = ke.getKeyCode(); return false; } } |