Results 1 to 5 of 5
  1. #1
    timoxa is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default 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!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    timoxa is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Problem with KeyEventDispatcher

    Do you know to solve this problem?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with KeyEventDispatcher

    Quote Originally Posted by timoxa View Post
    Do you know to solve this problem?
    Yes I do: read the API documentation for the KeyEvent class and only act on a KEY_PRESSED or KEY_RELEASED event type.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    rybread is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •