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: Key Bindings

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Key Bindings

    i can't seem to get this example to work. the goal is for "REDO" to be printed when you type ctrl+y, and "UNDO" when you type ctrl+z. there are no errors, but nothing is outputted when you type ctrl+y or ctrl+z.

    import java.awt.event.*;
    import javax.swing.*;
     
    public class Test extends JPanel{
     
    	public Test(){
    		setFocusable(true);
    		ActionMap a = getActionMap();
    		InputMap  i = getInputMap();
    		KeyStroke undoKeyStroke = KeyStroke.getKeyStroke("control typed z");
    		KeyStroke redoKeyStroke = KeyStroke.getKeyStroke("control typed y");
    		i.put(undoKeyStroke, "undo");
    		i.put(redoKeyStroke, "redo");
    		a.put("undo",undoAction);
    		a.put("redo",redoAction);
    	}
     
    	public static void main(String[] args){
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,300);
    		frame.getContentPane().add(new Test());
    		frame.setVisible(true);
    	}
     
    	Action redoAction = new AbstractAction(){
    		public void actionPerformed(ActionEvent e){
    			System.out.println("REDO");
    		}
    	};
    	Action undoAction = new AbstractAction(){
    		public void actionPerformed(ActionEvent e){
    			System.out.println("UNDO");
    		}
    	};
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Key Bindings

    Try removing the typed element when creating the keystroke. See Keystroke

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: Key Bindings

    Quote Originally Posted by copeg View Post
    Try removing the typed element when creating the keystroke. See Keystroke
    that didn't work. i've also tried changing control to ctrl, putting both control and ctrl, changing z to Z and y to Y, putting "VK_" in front of z and y (which returns null).

    i've resorted to this, which works:

    KeyStroke undoKeyStroke = KeyStroke.getKeyStroke("control pressed Z");
    KeyStroke redoKeyStroke = KeyStroke.getKeyStroke("control pressed Y");

Similar Threads

  1. Question...Key bindings
    By TheEnd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2010, 08:10 PM