Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating a keylistener that reacts across the entire application?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    78
    Thanks
    55
    Thanked 0 Times in 0 Posts

    Default Creating a keylistener that reacts across the entire application?

    Hi, I was wondering if there is any way to make the keylistener "listen" across the entire application? Currently whenever I've used it I have always attached it to something else, like a JTextField or the like. However that doesn't work all that well if I for example want to close down the current JDialog by pressing escape, because no object that has the keylistener attached to it is currently focused.

    When I searched around for the answer if found similar questions on stackoverflow, but those seemed to focus on listening globally, even when the application itself is not focused, which isn't quite what I want.

    Here is a simple piece of code that I'm currently experimenting with:
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.IOException;
    import java.security.spec.InvalidKeySpecException;
     
    import javax.swing.*;
     
     
    public class MainFrame implements KeyListener 
    {
     
    	JFrame frame;
     
    	public static void main(String[] args) throws IOException, InvalidKeySpecException
    	{
    		new MainFrame();
    	}
     
    	JPanel totalGUI;
     
    	public JPanel createContentPane()
    	{
    		totalGUI = new JPanel();
     
     
    		JLabel someLabel = new JLabel("test");
    		someLabel.setSize(200,20);
    		someLabel.setLocation(50,50);
    		totalGUI.add(someLabel);
     
     
    		return totalGUI;
    	}
     
    	public MainFrame() throws InvalidKeySpecException
    	{
    		frame = new JFrame();
    		frame.setContentPane(createContentPane());
    		frame.setSize(500,500);
    		frame.setUndecorated(false);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
     
     
     
    	}
     
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyChar() == KeyEvent.VK_D)
    		{
    			JOptionPane.showMessageDialog(null, "D pressed");
    		}
    		if(e.getKeyChar() == KeyEvent.VK_A)
    		{
    			JOptionPane.showMessageDialog(null, "A pressed");
     
    		}
    	}
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		if(e.getKeyChar() == KeyEvent.VK_D)
    		{
    			JOptionPane.showMessageDialog(null, "D released");
     
    		}
    		if(e.getKeyChar() == KeyEvent.VK_A)
    		{			
    			JOptionPane.showMessageDialog(null, "D released");
     
    		}		
    	}
     
    	@Override
    	public void keyTyped(KeyEvent arg0) {
     
    	}
    }

    It's supposed to simply create a frame and give me a message whenever I press the A or D buttons, as well as notify me whenever they are released. As of currently it obviously doesn't work because the keylistener hasn't been attached to anything, but that's also where I'm unsure of how to proceed.

    Any help is as always greatly appreciated!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,384
    My Mood
    Mellow
    Thanks
    256
    Thanked 789 Times in 774 Posts

    Default Re: Creating a keylistener that reacts across the entire application?

    Why wouldn't the JDialog have focus? If not, what does have focus? How would the app know that the <ESC> keypress would apply to a component that does not have focus?

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Creating a keylistener that reacts across the entire application?

    I don't see where you're adding the KeyListener to a component. You should either add it to your JFrame or to a focusable component inside the JFrame.

    But anyway, this sounds like a job for key bindings: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Pressing Cancel in FileDialog close entire application (Using SWT)
    By Avatar19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 3rd, 2013, 02:22 PM
  2. Creating java application
    By akbiswal in forum Member Introductions
    Replies: 0
    Last Post: December 15th, 2012, 11:31 PM
  3. Creating a Java Desktop Application in NetBeans
    By cslx99 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 2nd, 2012, 01:25 AM
  4. creating Sound Application Help
    By waseempki in forum Java SE APIs
    Replies: 1
    Last Post: August 25th, 2011, 03:54 AM
  5. WHY AM I HAVING TO PUT THE ENTIRE FILE PATH?!?!
    By CameronHussey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2011, 06:32 AM