Detect when key is being pressed then released

S

Si

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();
}
}
 
R

Rhino

Si said:
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();
}
}
See http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html.
 
V

Vova Reznik

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);
}
 
A

Andrey Kuznetsov

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?

use need KeyStroke.getKeyStroke(int keyCode, int modifiers, boolean
onKeyRelease)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top