Results 1 to 4 of 4
  1. #1
    Germoose is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default KeyBinding not responsive

    I have been trying to create a basic Rubik's cube simulator, where when i press certain keys on the keyboard, and a corresponding side on the cube turns. I currently am using a Cube class (stores data for cube and has turnSide methods) and a QCubeComponent Class (draws the cube) along side the main class. I went searching around and found the KeyBinding documentation and attempted to implement it, but with no success.

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JFrame;
    import javax.swing.KeyStroke;
    
    public class TestProgram3 {
    	public static void main(String [] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI(); }
    		});
    	}
    
    	private static void createAndShowGUI() {
    		JFrame frame = new JFrame();
    		frame.getContentPane().setPreferredSize(new Dimension(467, 639));
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("Take Input from Keyboard");
    		frame.setFocusable(true);
    		
    		final Cube cube = new Cube();
    		final QCubeComponent component = new QCubeComponent(cube);
    		
    		class KeyBindAction extends AbstractAction {
    			public KeyBindAction() {
    				System.out.println("KeyBindAction class created");
    			}
    			public void actionPerformed(ActionEvent e) {
    				System.out.println("action detected");
    				cube.UTurn(); //Turns one side of the cube and draws it
    			}
    		}
    		KeyBindAction anAction = new KeyBindAction();
    		
    		anAction.setEnabled(true);
    		frame.setFocusable(true);
    		
    		component.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"TurnSide");
    		component.getActionMap().put("TurnSide",anAction);
    		
    		frame.add(component);	
    		frame.pack();
    		frame.setVisible(true);
    	}	
    }
    Thank-you for your time.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: KeyBinding not responsive

    You're using the wrong InputMap, the default one that requires that the component has focus. Instead use the overload of getInputMap that takes an int condition and feed it:

    Java Code:
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          component.getInputMap(condition).put(KeyStroke.getKeyStroke("SPACE"), "TurnSide");
    This will engage the InputMap that is active when the component is in a focused top-level window, but the component doesn't have to have focus itself.

    For those wanting to test this themselves, here's my sscce:

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.BorderFactory;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    
    public class TestProgram3 {
       public static void main(String[] args) {
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGUI();
             }
          });
       }
    
       private static void createAndShowGUI() {
          JFrame frame = new JFrame();
          frame.getContentPane().setPreferredSize(new Dimension(467, 639));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setTitle("Take Input from Keyboard");
          frame.setFocusable(true);
    
          final Cube cube = new Cube();
          final QCubeComponent component = new QCubeComponent(cube);
    
          class KeyBindAction extends AbstractAction {
             public KeyBindAction() {
                System.out.println("KeyBindAction class created");
             }
    
             public void actionPerformed(ActionEvent e) {
                System.out.println("action detected");
                cube.UTurn(); // Turns one side of the cube and draws it
             }
          }
          KeyBindAction anAction = new KeyBindAction();
    
          anAction.setEnabled(true);
          frame.setFocusable(true);
    
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          component.getInputMap(condition).put(KeyStroke.getKeyStroke("SPACE"), "TurnSide");
          component.getActionMap().put("TurnSide", anAction);
    
          frame.add(component);
          frame.pack();
          frame.setVisible(true);
       }
    }
    
    class Cube {
    
       public void UTurn() {
          System.out.println("u turn called");
       }
    
    }
    
    class QCubeComponent extends JPanel {
    
       private Cube cube;
    
       public QCubeComponent(Cube cube) {
          this.cube = cube;
          add(new JLabel("QCubeComponent"));
          setBorder(BorderFactory.createTitledBorder("QCubeComponent"));
       }
    
    }
    To the original poster: please note the changes that I've made to your program that keeps the same essential code and adds a little additional code so that it compiles and runs for us and allows us to test and modify your program, and thereby fix it. This is called creating an SSCCE. Please have a look at the link, it's a worthwhile look!
    Last edited by Fubarable; 04-21-2012 at 10:58 PM.

  3. #3
    Germoose is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: KeyBinding not responsive

    Wow, thank you so much. I'll be sure to use SSCCE format next time I have a problem. Thanks-again :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: KeyBinding not responsive

    You're quite welcome and glad that it helped.

Similar Threads

  1. Keybinding
    By mine0926 in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 05:33 AM
  2. KeyBinding key released doesn't work so well -.-
    By Addez in forum New To Java
    Replies: 1
    Last Post: 09-22-2010, 12:09 PM
  3. KeyBinding Help
    By Lil_Aziz1 in forum New To Java
    Replies: 12
    Last Post: 07-27-2010, 03:58 PM
  4. Replies: 0
    Last Post: 01-05-2009, 10:48 PM
  5. ZombieEscape: Multiple KeyBinding at one moment?
    By Unome in forum Java Applets
    Replies: 0
    Last Post: 11-17-2008, 06:51 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
  •