where do the keystrokes go?

Discussion in 'Java' started by Martijn Mulder, Dec 18, 2005.

  1. I try to attach a KeyListener to my JComponent but no
    keystrokes make it to the JComponent. MouseMotions, on
    the other hand, are percieved by the JComponent. I do not
    understand where the keystrokes go, since both Listeneres
    are attached in identical ways. This code illustrates the
    problem. When the mouse is moved over the JFrame,
    a long stream of 'mouseMoved()' messages is send to
    stdout. Pressing keys when the JFrame has focus yields
    no output.


    //class MouseAndKeys
    class MouseAndKeys extends javax.swing.JComponent
    {

    //constructor
    MouseAndKeys()
    {
    super();
    addMouseMotionListener(new javax.swing.event.MouseInputAdapter()
    {
    public void mouseMoved(java.awt.event.MouseEvent a)
    {
    System.out.println("mouseMoved()");
    }
    });
    addKeyListener(new KeyAdapter()
    {
    public void keyPressed(java.awt.event.KeyEvent a)
    {
    System.out.println("keyPressed()");
    }
    });
    }

    //main
    public static void main(String[]a)
    {
    javax.swing.JFrame jframe=new javax.swing.JFrame("MouseAndKeys");
    jframe.getContentPane().add(new MouseAndKeys());
    jframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    jframe.pack();
    jframe.setSize(610,377);
    jframe.setVisible(true);
    }
    }
     
    Martijn Mulder, Dec 18, 2005
    #1
    1. Advertising

  2. Martijn Mulder

    Chris Smith Guest

    Martijn Mulder <> wrote:
    > I try to attach a KeyListener to my JComponent but no
    > keystrokes make it to the JComponent. MouseMotions, on
    > the other hand, are percieved by the JComponent. I do not
    > understand where the keystrokes go, since both Listeneres
    > are attached in identical ways. This code illustrates the
    > problem. When the mouse is moved over the JFrame,
    > a long stream of 'mouseMoved()' messages is send to
    > stdout. Pressing keys when the JFrame has focus yields
    > no output.


    Well, for one thing, your component needs focus. The containing frame
    having focus isn't good enough. (Can you imagine if every time you
    pressed a key while a dialog had focus, EVERY textfield in that dialog
    received the key event?) To give focus to your new control, you'll
    first need to call setFocusable(true).

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 18, 2005
    #2
    1. Advertising

  3. Re: [SOVLED] where do the keystrokes go?

    :) Thank you, Chris. Hours of thumbing through books
    led nowhere, posting did. Here is the fixed code. Moving
    the mouse over the JFrame or pressing keys when JFrame
    has focus makes the JComponent respond:



    //class MouseAndKeys
    class MouseAndKeys extends javax.swing.JComponent
    {

    //constructor
    MouseAndKeys()
    {
    super();
    setFocusable(true);
    addMouseMotionListener(new javax.swing.event.MouseInputAdapter()
    {
    public void mouseMoved(java.awt.event.MouseEvent a)
    {
    System.out.println("mouseMoved()");
    }
    });
    addKeyListener(new KeyAdapter()
    {
    public void keyPressed(java.awt.event.KeyEvent a)
    {
    System.out.println("keyPressed()");
    }
    });
    }

    //main
    public static void main(String[]a)
    {
    javax.swing.JFrame jframe=new javax.swing.JFrame("MouseAndKeys");
    jframe.getContentPane().add(new MouseAndKeys());
    jframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    jframe.pack();
    jframe.setSize(610,377);
    jframe.setVisible(true);
    }
    }
     
    Martijn Mulder, Dec 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob T

    Capturing Keystrokes

    Rob T, May 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    817
    Vidar Petursson
    May 14, 2004
  2. Lucas Tam
    Replies:
    5
    Views:
    6,239
    WizyDig
    Dec 2, 2004
  3. Replies:
    3
    Views:
    432
    VisionSet
    Jun 28, 2004
  4. Simon
    Replies:
    2
    Views:
    396
    Simon
    Mar 2, 2006
  5. Steven Raasch

    Sending keystrokes to QT applications

    Steven Raasch, Jul 17, 2003, in forum: C++
    Replies:
    2
    Views:
    501
    Rolf Magnus
    Jul 17, 2003
Loading...

Share This Page