Detect when key is being pressed then released

Discussion in 'Java' started by Si, Feb 28, 2006.

  1. Si

    Si Guest

    I use the code below to detect when a key is being pressed. Unfortunately it
    only responds when the key is first pressed.

    What I would like to do is detect when the key is initially pressed, and
    when it is released.

    How do I modify the code below to achieve this?

    Thanks.

    =============================

    //below is in main application view constructor

    contentPane.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(javax.swing.KeyStroke.getKeyStroke('a'),"a_pressed");
    contentPane.getActionMap().put("a_pressed", new AbstractActionGB(gb));


    //below is inner class in main application view

    class AbstractActionGB extends javax.swing.AbstractAction {

    model.GameBoard gb;

    public AbstractActionGB(GameBoard gb) {
    this.gb = gb;
    }

    public void actionPerformed(ActionEvent e)
    {
    System.out.println("A WAS PRESSED!!!");
    gb.flipFlipper();
    }
    }
     
    Si, Feb 28, 2006
    #1
    1. Advertisements

  2. Si

    Rhino Guest

    See http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html.
     
    Rhino, Feb 28, 2006
    #2
    1. Advertisements

  3. Si

    Vova Reznik Guest

    Yo may use method I wrote several years ago:

    How to call:

    registerKeyboardAction(c, actionForKeyRelease, KeyEvent.VK_A,
    0, JComponent.WHEN_IN_FOCUSED_WINDOW, true);

    registerKeyboardAction(c, actionForKeyPress, KeyEvent.VK_A,
    0, JComponent.WHEN_IN_FOCUSED_WINDOW, false);



    /**
    * @param component
    * @param action @see javax.swing.Action
    * @param keyCode @see java.awt.event.KeyEvent
    * @param modifier @see java.awt.event.InputEvent
    * CTRL_MASK for example or 0 if no modifiers
    * @param focusModifier @see JComponent
    * @param onKeyRelease
    * @throws IllegalArgumentException
    */
    public static void registerKeyboardAction(JComponent component,
    Action action, int keyCode, int modifier,
    int focusModifier, boolean onKeyRelease) throws
    IllegalArgumentException {

    InputMap inputMap = component.getInputMap(focusModifier);
    ActionMap actionMap = component.getActionMap();
    if (actionMap == null) {
    actionMap = new ActionMap();
    component.setActionMap(actionMap);
    }

    inputMap.put(KeyStroke.getKeyStroke(keyCode, modifier, onKeyRelease),
    action);
    actionMap.put(action, action);
    }
     
    Vova Reznik, Feb 28, 2006
    #3
  4. I use the code below to detect when a key is being pressed. Unfortunately
    use need KeyStroke.getKeyStroke(int keyCode, int modifiers, boolean
    onKeyRelease)
     
    Andrey Kuznetsov, Feb 28, 2006
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.