Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help with Keylistener  (Read 1555 times)
0 Members and 1 Guest are viewing this topic.
Offline marrw

Junior Newbie





« Posted 2012-05-07 20:10:15 »

Hello Java Gaming! Smiley

I have just started with my own game.. At the moment the movement for my box looks like this
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
this.addMouseMotionListener(new MouseMotionListener()
      {
         
         public void mouseDragged(MouseEvent e)
         {
            x = e.getX();
            y = e.getY();
         }

         
         public void mouseMoved(MouseEvent arg0) {}
      });
     


And i want it to be controlled with my keyboard for smooth motion, but i cant seem to figure out how the Keylistener works!
So abit of help would be appriciated! Smiley

Cheers  Grin
Offline jonjava
« Reply #1 - Posted 2012-05-07 20:41:37 »

It works exactly the same :o

Except for dragging and moved, you get pressed, released and typed.

Then all you need to do is decide what to do with depending on what key is triggered.

1  
2  
3  
4  
5  
6  
7  
8  
...
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("Space pressed!);
}
}
...

Offline Rorkien
« Reply #2 - Posted 2012-05-07 21:15:09 »

It works exactly the same Shocked

Except for dragging and moved, you get pressed, released and typed.

Then all you need to do is decide what to do with depending on what key is triggered.

1  
2  
3  
4  
5  
6  
7  
8  
...
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("Space pressed!);
}
}
...


This will work, but very clumsily, since keyPressed will keep activating even when you hold down the key (Like when you press some key on notepad, letters will keep popping)

Refer to this thread: http://www.java-gaming.org/topics/how-would-you-know-if-the-key-was-being-held-for-ld23/26293/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deepthought
« Reply #3 - Posted 2012-05-07 21:47:52 »

personally i would use jinput and polling.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-05-07 22:11:50 »

JInput is overkill for what the OP wants.

Offline DruLeeParsec

Junior Devvie


Medals: 2
Projects: 1



« Reply #5 - Posted 2012-05-08 03:36:18 »

What I often do is have booleans such as
boolean upKeyIsPressed = false;
boolean downKeyIsPressed = false;

Then on the keyPressed event I catch the key in the key event and set the appropriate boolean to true.
In keyReleased I set the appropriate boolean to false.  That way the repeating keys have no effect.

Now, what I actually do is have a Singleton class called KeyAssignments.  In that class I map any key which I want to watch to the object which must listen to it.  So if I want this object to listen to the f4 key (for example) to show a debug screen I set it up like this.

KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_F4, this);

KeyAssignments is actually mapping a KeyEvent to a list of objects so I can make many things turn on a single key stroke if I want.

Then, every object which must response to a key event must implement KeyAssignable

1  
2  
3  
4  
5  
public interface KeyAssignable {

   public void doKeyAction(int keyCode, boolean keyDown);
   
}


My main panel which catches the key events does something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
      addKeyListener( new KeyAdapter(){
         public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();
            boolean keyDown = true;
            KeyAssignments.getInstance().doActionsForKey(keyCode, keyDown);
         }
         
         public void keyReleased(KeyEvent e){
            int keyCode = e.getKeyCode();
            boolean keyDown = false;
            KeyAssignments.getInstance().doActionsForKey(keyCode, keyDown);
           
         }
      });


If I have a controlable entity which must respond to ASWD keys to move up down right and left then mapping those keys is as easy as:

1  
2  
3  
4  
      KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_A, this);
      KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_S, this);
      KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_W, this);
      KeyAssignments.getInstance().addKeyAssignment(KeyEvent.VK_D, this);


That may very well be overkill at this point.  I think that the boolean values may fix your immediate problem of repeating key strokes.  But this may be something to think about once you move forward.
Offline marrw

Junior Newbie





« Reply #6 - Posted 2012-05-08 12:27:50 »

It works exactly the same Shocked

Except for dragging and moved, you get pressed, released and typed.

Then all you need to do is decide what to do with depending on what key is triggered.

1  
2  
3  
4  
5  
6  
7  
8  
...
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("Space pressed!);
}
}
...


This will work, but very clumsily, since keyPressed will keep activating even when you hold down the key (Like when you press some key on notepad, letters will keep popping)

Refer to this thread: http://www.java-gaming.org/topics/how-would-you-know-if-the-key-was-being-held-for-ld23/26293/view.html


Thanks alot for that thread! I solved it now thanks! By using delta x and delta y it moves the box smooth :-)
Offline ReBirth
« Reply #7 - Posted 2012-05-08 12:30:57 »

You solve it, now let's see when you'll come back asking about how to use same method on menu/option Wink

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (15 views)
2014-12-28 10:39:27

CopyableCougar4 (19 views)
2014-12-28 02:10:29

BurntPizza (24 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!