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 2 of 2

Thread: How to get keys pressed by the user to control the unit in a game.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to get keys pressed by the user to control the unit in a game.

    I am making a game and i want it to work like this:

    1): the users presses a key and gets a player asigned to him/her.
    2): The user can use the arrow keys or w/s/d/a to control the unit.
    3): The space bar key is pressed and a shot is fired.

    and allot more things.

    i have this code (just a simple code, not yet my game code) that just starts about getting key from the user.

    import java.awt.event.*; 
    import javax.swing.*; 
     
    public class KeyStrokeTest { 
        public static void main(String[] args) { 
            JPanel panel = new JPanel(); 
     
            /* add a new action named "foo" to the panel's action map */ 
            panel.getActionMap().put("foo", new AbstractAction() { 
                    public void actionPerformed(ActionEvent e) { 
                        if(e == KeyStroke.getKeyStroke(Character.valueOf('a')))
                        {
                            System.out.println(e);
                        }
                    } 
                }); 
     
            /* connect two keystrokes with the newly created "foo" action: 
               - a 
               - CTRL-a 
            */ 
            InputMap inputMap = panel.getInputMap(); 
            inputMap.put(KeyStroke.getKeyStroke(Character.valueOf('a'), 0), "foo"); 
            inputMap.put(KeyStroke.getKeyStroke(Character.valueOf('a'), InputEvent.CTRL_DOWN_MASK), "foo"); 
     
            /* display the panel in a frame */ 
            JFrame frame = new JFrame(); 
            frame.getContentPane().add(panel); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setSize(400, 400); 
            frame.setVisible(true); 
        } 
    }

    when the a key is pressed it displays a message, now how do i make a deferint message, action ect. for each other key?

    Thanks...


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to get keys pressed by the user to control the unit in a game.

    Moving this from the Member Introduction forum. Please read this: http://www.javaprogrammingforums.com...e-posting.html
    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. HTML Unit Help
    By Seegee in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 2nd, 2012, 08:38 PM
  2. Trying to start a game when 'Enter' is pressed.
    By Shaybay92 in forum AWT / Java Swing
    Replies: 6
    Last Post: September 27th, 2011, 07:48 AM
  3. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM
  4. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM