Results 1 to 5 of 5
  1. #1
    jfio is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default KeyListener ignores key events despite apparently having focus

    The following code never calls the key press/release methods in the KeyFocusView
    listener. The code does print:

    focused window
    focused client view!
    What's gone wrong?

    Java Code:
    package keyfocus;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public final class KeyFocus
    {
      final KeyFocusView drawable;
    
      final static int   WINDOW_WIDTH  = 640;
      final static int   WINDOW_HEIGHT = 480;
    
      public final class KeyFocusView extends JComponent implements KeyListener
      {
        private static final long serialVersionUID = 1L;
    
        @Override public void paint(final Graphics g)
        {
          g.clearRect(0, 0, KeyFocus.WINDOW_WIDTH, KeyFocus.WINDOW_HEIGHT);
          g.setColor(Color.BLACK);
          g.fillRect(0, 0, KeyFocus.WINDOW_WIDTH, KeyFocus.WINDOW_HEIGHT);
        }
    
        @Override public void keyReleased(final KeyEvent e)
        {
          System.out.println("keyReleased");
        }
    
        @Override public void keyPressed(final KeyEvent e)
        {
          System.out.println("keyPressed");
        }
    
        @Override public void keyTyped(final KeyEvent e)
        {
          System.out.println("keyTyped");
        }
      }
    
      public KeyFocus()
      {
        this.drawable = new KeyFocusView();
    
        final JFrame frame = new JFrame("GUI");
        final Container container = frame.getContentPane();
        container.add(this.drawable);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(KeyFocus.WINDOW_WIDTH, KeyFocus.WINDOW_HEIGHT);
        frame.addWindowFocusListener(new WindowAdapter() {
          @Override public void windowGainedFocus(final WindowEvent e)
          {
            System.out.println("focused window");
            boolean cvf = KeyFocus.this.drawable.requestFocusInWindow();
            assert (cvf);
            System.out.println("focused client view!");
          }
        });
    
        frame.setVisible(true);
      }
    
      public static void main(final String args[])
      {
        new KeyFocus();
      }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: KeyListener ignores key events despite apparently having focus

    KeyEvents are only dispatched to the component with focus. By default a JComponent does not receive focus.

    Check out How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features). You can do bindings even when the component doesn't have focus.

  3. #3
    jfio is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: KeyListener ignores key events despite apparently having focus

    I'm aware of key bindings, I just dislike the API intensely.

    As I understand it, then, the following SHOULD work:

    Java Code:
    this.drawable.setFocusable(true);
    However, it still doesn't.

    I have discovered, though, that if KeyFocusView has a KeyListener added:

    Java Code:
        public KeyFocusView()
        {
          this.addKeyListener(new KeyListener() {
            @Override public void keyReleased(final KeyEvent e)
            {
              System.out.println("keyReleased");
            }
    
            @Override public void keyPressed(final KeyEvent e)
            {
              System.out.println("keyPressed");
            }
    
            @Override public void keyTyped(final KeyEvent e)
            {
              System.out.println("keyTyped");
            }
          });
        }
    ...as opposed to implementing the interface, the program works as expected, regardless
    of whether or not I make it "focusable".

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: KeyListener ignores key events despite apparently having focus

    Quote Originally Posted by jfio View Post
    I'm aware of key bindings, I just dislike the API intensely.
    So you expect someone here to help you hack together a wrong way to achieve your objective? I would wish you good luck finding such 'help' but the wish would be insincere.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: KeyListener ignores key events despite apparently having focus

    Quote Originally Posted by jfio View Post
    I have discovered, though, that if KeyFocusView has a KeyListener added:

    ...as opposed to implementing the interface, the program works as expected, regardless of whether or not I make it "focusable".
    And the same with a class implementing any other interface/listener; there's no magic that adds the listener just by implementing the interface. The add... method has to be invoked.

    But take the trouble to learn key bindings. Since you say you dislike it, you obviously haven't.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. simple array processing(apparently!)
    By stateofyouboi in forum New To Java
    Replies: 2
    Last Post: 02-05-2011, 06:17 PM
  2. AbstractProcessor "ignores" some classes ...
    By Gonzoide in forum Advanced Java
    Replies: 0
    Last Post: 01-06-2011, 11:37 AM
  3. instruction apparently not executed
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-30-2010, 02:30 AM
  4. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 01:22 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
  •