Hello,
I'm trying to make my GUI application react to keyevents,
but I'm having a hard time making my Dialog (also tried with Frame)
react to keyevents.
I have the following rows in my ctor (for Dialog):
start of ctor
..
..
// Listen to keys.
this.setFocusable(true);
this.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
dialogKeyPressed(evt);
}
});
..
..
end of ctor
And I have:
private void dialogKeyPressed(java.awt.event.KeyEvent evt) {
System.err.println("Dialog Frame registered KEY PRESSED!");
}
But the method is never called and the error message never shows (I've
tried pressing ALOT of keys)!
Could someone please tell me what I'm doing wrong!!??
Right now I'm using a workaround where I connect the keyevent to a
certain component on the dialog, then I have to click on the component
to get the focus and finally I can receive keyevents from this
component.
But this is not what I want! I want my application to react to certain
F-keys no matter which component is focused.
Thanks for any kind of tips or guides.
/Bo
Kleopatra - 24 Feb 2004 12:22 GMT
If you are on AWT, simply ignore this post.
If you are on Swing:
> ..
> ..
[quoted text clipped - 19 lines]
>
> Could someone please tell me what I'm doing wrong!!??
D O N O T U S E K E Y L I S T E N E R S!!!!
Have a look at InputMap/ActionMap framework instead. For details, feed
your favorite search tool with "keybinding".
Greetings
Jeanette
Andrew Chase - 24 Feb 2004 22:45 GMT
I happen to have some keyevent code laying around, so I'll give a quick
example of what Jeanette mentioned with the keymap.
"this" is a JPanel
[begin code]
this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(
KeyEvent.VK_D,
InputEvent.CTRL_DOWN_MASK),
"delete");
this.getActionMap().put("delete", deleteAction);
[end code]
Important things to note:
WHEN_IN_FOCUSED_WINDOW will make the component you're adding the keylistener
event to receive events in the widest array of cases. There are other
options, such as when receiving key events only when the specific component
has the focus.
You will need to make to method calls to setup a keyevent,
getInputMap(...).put(...) and getActionMap(...).put(...). The getInputMap
call links a keystroke to a string, the getActionMap then associates that
same string with an Action.
Cheers,
Andrew
> Hello,
>
[quoted text clipped - 37 lines]
> Thanks for any kind of tips or guides.
> /Bo
Kid - 25 Feb 2004 08:26 GMT
Thanks for your effort Andrew and Jeanette.
Unfortunately I'm bound to use AWT because of some hardware restrictions.
Any other tip? Anyone?
/Bo
Andrew Thompson - 25 Feb 2004 10:19 GMT
"Kid" ...
...
> Unfortunately I'm bound to use AWT because of some hardware restrictions.
>
> Any other tip? Anyone?
I detect KeyEvents in the Calculator
http://www.physci.org/launcher.jsp#Calculator
If you cannot figure where you are going
wrong from looking at that, try..
http://www.physci.org/codes/sscce.jsp
--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
Mark A. Washburn - 25 Feb 2004 21:38 GMT
Kid,
"
..
// Listen to keys.
this.setFocusable(true);
this.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
dialogKeyPressed(evt);
}
});
..
"
make sure /this/ is a Component object like Canvas ( or subclass of)
( don't use Panel )
Mark A. Washburn
maw
http://mywebpage.netscape.com/mawcowboy/homepage.html
http://www.geocities.com/dolphinconsultant/myforth.html