1

I have a method where he performs another method depending on the button clicked. Only he seems to be running the methods called twice, and I do not know why. Can anyone help me and explain to me why this is? Follows the source

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        @Override
        public void eventDispatched(final AWTEvent event) {
            final KeyEvent evt = (KeyEvent) event;
            switch (evt.getKeyCode()) {
                case KeyEvent.VK_F1: { 
                    //F1
                    doSomething(); // this method is running twice

                    break;
                }
                case KeyEvent.VK_F2: {
                    //F2
                    doSomething();
                    break;
                }
                }, AWTEvent.KEY_EVENT_MASK);

Thanks

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rafael
  • 13
  • 3
  • Look at the answer from this post: http://stackoverflow.com/questions/9864297/onkey-event-dispatched-twice-when-i-type-some-text-into-a-textbox-how-to-preven – mdewitt Jan 23 '14 at 21:14
  • @mdewitt That won't help. He's checking o emsure the correct key is pressed. – Epiglottal Axolotl Jan 23 '14 at 21:18
  • @Stendika But he might be getting an event from the down action and the up from the correct key press. Both would have the correct key code. – mdewitt Jan 23 '14 at 21:20
  • I got it, and how can I check to only run in the KeyPress and not in keyrelease? – Rafael Jan 23 '14 at 21:24

2 Answers2

2

The problem appears to be that both the key-press and key-released methods are triggering the event. I would recommend using java.awt.event.KeyListener, which has separate event handlers for the press and release events.

Epiglottal Axolotl
  • 1,048
  • 8
  • 17
1

You are receiving an event for KEY_PRESSED and KEY_RELEASED.

You need to check if, in addition to the KeyEvent having the correct KeyCode you want to make sure it is the correct action, ie KEY_PRESSSED. One way to fix this is add a check for the action before you go into your switch statement.

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
    @Override
    public void eventDispatched(final AWTEvent event) {
        if (event.getKeyChar() == KeyEvent.KEY_PRESSED)
        {
            final KeyEvent evt = (KeyEvent) event;
            switch(event.getKeyCode()){
                 //switch statement code
            }
        }   
    }, AWTEvent.KEY_EVENT_MASK);

Another thing you could do is create a KeyEventDispatcher and add it to the KeyboardFocusManager like this:

    //create KeyEventDispatcher myKeyEventDispatcher
    KeyboardFocusManager focusManager = KeyboardFocusManager.
            getCurrentKeyboardFocusManager(); 
    focusManager.addKeyEventDispatcher(myKeyEventDispatcher);

That way you will only get key event and you can dispatch them yourself

mdewitt
  • 2,526
  • 19
  • 23
  • You're welcome. If one of the answers helped you, you can accept it so this is no longer an unanswered question. – mdewitt Jan 23 '14 at 21:50