Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By camickr

Thread: KeyListener isn't responding when JFrame isn't focused

  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default KeyListener isn't responding when JFrame isn't focused

    So when I run my code and quickly click on another window or my desktop before the JFrame has appeared the KeyListener won't work. I've tried requesting focus to the JFrame but it doesn't work. I've searched Google but couldn't find anything useful, so I need your help to explain how to make this work (or give me a link to a similar problem).

    SSCCE code:

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class KeyboardProblem implements KeyListener{
    
    	JFrame frame;
    	JPanel panel;
    	
    	public KeyboardProblem(){
    		double i = 1.0;
    		// this is for me to be able to unfocus the window before the JFrame gets displayed.
    		while(i<1000000000){
    			i*=1.0001;
    		}
    		frame = new JFrame();
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		panel = new JPanel();
    		panel.setPreferredSize(new Dimension(100,100));
    		frame.add(panel);
    		frame.pack();
    		frame.addKeyListener(this);
    	}
    	
    	@Override
    	public void keyPressed(KeyEvent arg0) {
    		System.out.println(arg0.getKeyCode());
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    	}
    
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    	}
    	
    	public static void main(String args[]){
    		new KeyboardProblem();
    	}
    	
    }
    Thanks guys.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: KeyListener isn't responding when JFrame isn't focused

    So what are you trying to do? Keyboard events are fired by the component that has the keyboard focus. So if the frame does not have the focus it will not receive the events.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: KeyListener isn't responding when JFrame isn't focused

    I'm in the middle of creating a game and when I start up the game it takes like 5 seconds for the game to start and that gets quite annoying. Sometimes I press somewhere on my screen (before the JFrame has appeared) and then when the JFrame has appeared (behind, for example, the internet window) and I try to refocus the game frame my keyListener won't Listen to anything. Hope you understood that explanation, I'm not good at explaining.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: KeyListener isn't responding when JFrame isn't focused

    Once the frame appears do you then click on it with the mouse?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: KeyListener isn't responding when JFrame isn't focused

    Don't use a KeyListener. Swing was designed to be used with Key Bindings, which can get around these focus issues.

    See: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    DarrylBurke likes this.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: KeyListener isn't responding when JFrame isn't focused

    If you really need to use a KeyListener (for example, because you really want to receive every key event) then you need to make sure the component on which the KeyListener is registered has the input focus. First, if it's something like a JPanel that can't normally be focused, call setFocusable(true) on it. To gain the focus, you can add a mouse listener that calls requestFocusInWindow() when the component is clicked. If you want to receive the tab key, call setFocusTraversalKeysEnabled(false); otherwise the focus traversal system will consume it.

    If you want to receive all key events regardless of what component has focus, add KeyEventDispatchers and/or KeyEventPostProcessors to the KeyboardFocusManager. If you go this route, you have to figure out what events were (or you might want to allow to be) consumed by other components. AbstractButtons (which includes JMenuItems) consume KEY_PRESSED events. On the other hand, JTextComponents append to their documents on KEY_PRESSED events, but they consume the corresponding KEY_TYPED events. So, for example, if you're working with KEY_PRESSED, you may want to ignore events that have been consumed or whose focus is a JTextComponent.
    Get in the habit of using standard Java naming conventions!

  7. #7
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: KeyListener isn't responding when JFrame isn't focused

    See: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    I barely understand anything from that page, I've read it through 2 times and I still don't see how this works... I've never liked Oracles tutorials, I find them more often than not hard to understand.

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

    Default Re: KeyListener isn't responding when JFrame isn't focused

    > I barely understand anything from that page,

    Not sure what you expect us to do about that. We don't know what you find confusing.

    The tutorial contains working examples. Did you download any of the examples and play with them?

    If you don't like the Oracle examles then search the forum/web for other examples. Pick keywords like ActionMap, InputMap.

Similar Threads

  1. Replies: 3
    Last Post: 08-12-2012, 09:32 AM
  2. adding keylistener to JFrame
    By Alkor in forum New To Java
    Replies: 3
    Last Post: 03-08-2012, 12:02 PM
  3. Adding a KeyListener to a JFrame with buttons.
    By jamhead in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 07:29 PM
  4. Scrolling to focused component
    By berkeleybross in forum AWT / Swing
    Replies: 1
    Last Post: 07-21-2010, 02:03 PM
  5. Last focused component
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 11-21-2009, 05:45 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
  •