Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Detect when key is being pressed then released

Reply
Thread Tools

Detect when key is being pressed then released

 
 
Si
Guest
Posts: n/a
 
      02-28-2006
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.WHE N_IN_FOCUSED_WINDOW).put(javax.swing.KeyStroke.get KeyStroke('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();
}
}


 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      02-28-2006

"Si" <> wrote in message
news:tJ_Mf.51218$...
>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.WHE N_IN_FOCUSED_WINDOW).put(javax.swing.KeyStroke.get KeyStroke('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/tutor...ylistener.html.

--
Rhino


 
Reply With Quote
 
 
 
 
Vova Reznik
Guest
Posts: n/a
 
      02-28-2006
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);
}

Si wrote:
> 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.WHE N_IN_FOCUSED_WINDOW).put(javax.swing.KeyStroke.get KeyStroke('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();
> }
> }
>
>

 
Reply With Quote
 
Andrey Kuznetsov
Guest
Posts: n/a
 
      02-28-2006
>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)

--
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect whether a key is being held before script execution? Thomas Wilson Ruby 3 04-14-2010 01:43 AM
How to intercept Ctrl key - eg Ctrl E being pressed on Java console program Angus Java 5 11-18-2006 04:19 PM
How Do I Detect if the User has pressed the ENTER key??? Shanknbake Java 3 10-09-2005 04:49 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
How to detect that a key is being pressed, not HAS been pressed earlier!?? Rune Python 6 01-29-2004 12:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57