Hi

Just wondering if anyone can help. I have a gui and have assigned a KeyListener to it, It recognizes keyboard input for standard keys but doesnt seem to recognise special keys like shift and function keys? Is there some special way to be able to get it to react to function keys. I need to be able to store when a shift key is pressed or when 'F2' is pressed. To give you an idea im doing a replica of MineSweeper and need it to restart the game if F2 is pressed and need to be able to get the shift and return key for the cheat to work. Any help woul dbe great.

Thanks in Advance

Dani AI

Generated

As eventually found, modifier and function keys won’t produce a keyTyped event — keyTyped represents a character input. For a sequence detector (like "X,Y,Z,Z,Y,left-shift,enter") treat every press as a discrete keyPressed event, record the keyCode, and use keyReleased to reset state. Also remember KeyListener only sees events when the component has focus; that’s why many Swing apps prefer Key Bindings for UI actions.

A robust, simple approach is to suppress auto-repeat and keep a rolling list of recent keyCodes. The pattern below logs a press only the first time the key goes down, trims the sequence to the cheat length, and then compares:

Set<Integer> down = new HashSet<>();
List<Integer> seq = new ArrayList<>();
int[] cheat = { KeyEvent.VK_X, KeyEvent.VK_Y, KeyEvent.VK_Z, KeyEvent.VK_Z, KeyEvent.VK_Y, KeyEvent.VK_SHIFT, KeyEvent.VK_ENTER };

public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if (down.add(code)) {               // only when key transitions to down
        seq.add(code);
        while (seq.size() > cheat.length) seq.remove(0);
        if (matchesCheat(seq, cheat)) activateCheat();
    }
}
public void keyReleased(KeyEvent e) { down.remove(e.getKeyCode()); }
public void keyTyped(KeyEvent e) { }

For simple UI shortcuts (restart on F2) prefer Swing Key Bindings so the action works even when a child component has focus:

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
     .put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "restart");
panel.getActionMap().put("restart", new AbstractAction() {
    public void actionPerformed(ActionEvent e) { restartGame(); }
});

Extra tips: use getKeyLocation() if left vs right Shift matters; store keyCodes (not keyChar) to avoid layout-dependent mismatches; and ensure the listening component is focusable or use WHEN_IN_FOCUSED_WINDOW. Echoing : modifiers are special-case keys, and ’s pointer to the Swing docs is a useful read for deeper details.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

Hi

Just wondering if anyone can help. I have a gui and have assigned a KeyListener to it, It recognizes keyboard input for standard keys but doesnt seem to recognise special keys like shift and function keys? Is there some special way to be able to get it to react to function keys. I need to be able to store when a shift key is pressed or when 'F2' is pressed. To give you an idea im doing a replica of MineSweeper and need it to restart the game if F2 is pressed and need to be able to get the shift and return key for the cheat to work. Any help woul dbe great.

Thanks in Advance

Possibly this, although I haven't had time to read over it.

http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

If worse comes to the worst I guess you could use some other keys or create your own button in the GUI?

the KeyEvent would state any modifiers on the key. a modifier would be something like a shift or control key.

unfortunatly its not tryin gto pick up the shift as a modifyer, it needs to recognize when just the shift key is pressed. the cheat code is like, 'press x,y,z,z,y,left shift, return' and they need to be pressed seperatly. when the keys get pressed the keycode gets put into an array and if that array matchs the cheat array then the cheat is active.

Got it!!, It doesnt recognize things like Function keys and shift etc in teh KeyTyped method, it only sees those keys seperatly in the KeyPressed or KeyReleased Methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.