Results 1 to 5 of 5
Thread: Problem with KeyEventDispatcher
- 03-26-2012, 10:39 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Problem with KeyEventDispatcher
Please help ho know,
code example:
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManage r();
manager.addKeyEventDispatcher(this);
public boolean dispatchKeyEvent(KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.VK_F2) {
JFrame frame = new JFrame();
frame.setVisible(true);
}
return false;
}
code work normal but the frame open twice ???
Where is problem
Thank you!
- 03-26-2012, 10:43 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: Problem with KeyEventDispatcher
If a key is pressed it also is released in the near future; check the KeyEvent type (read the API documentation for that class).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-26-2012, 12:59 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Problem with KeyEventDispatcher
Do you know to solve this problem?
- 03-26-2012, 01:29 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 05-17-2013, 09:02 PM #5
Member
- Join Date
- May 2013
- Posts
- 1
- Rep Power
- 0
Re: Problem with KeyEventDispatcher
Thanks for your help.
If you use the KeyEvent.getID() function you can compare it with KeyEvent.KEY_RELEASED.
I am changing the KeyStroke to be either released or not to my function.
I am using it like this:
Java Code:KeyboardFocusManager.getCurrentKeyboardFocusManager() .addKeyEventDispatcher(new KeyEventDispatcher() { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_RELEASED){ return sendKeyCodeToDisplay(KeyStroke.getKeyStroke(e.getKeyChar(),0,true),e,e.getModifiers(),true); } else{ return sendKeyCodeToDisplay(KeyStroke.getKeyStroke(e.getKeyChar(),0,false),e,e.getModifiers(),true); } } });Last edited by rybread; 05-17-2013 at 09:06 PM.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks