Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Keyboard input?  (Read 2594 times)
0 Members and 1 Guest are viewing this topic.
Offline MoO_coW

Junior Newbie




Java games rock!


« Posted 2003-03-11 02:10:04 »

Ok, so right now I have it on the keyPressed and keyReleased() calls a method is called that passes the key pressed and they set bools to on or off, and then call a move method which incriments my games player pos, etc, etc. But when holding down a key there is a slight pause which gets quite anoying,  also it gets confused sometimes when too many keys are pressed, or you are say moving diaginal and then press an opposing key(say youre moving up and right and then hit left) it seems to kind of crap out. Its kind of hard to explain but im guessing other people have had this problem so any help would be apreciated with a more efficient way of doing it, or one that is just cleaner and better all around. Thanks alot
Offline lefty

Senior Newbie




Java ees kewl, man!


« Reply #1 - Posted 2003-03-12 01:40:17 »

Something like this might work:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
public class Game
extends KeyAdapter
{
      private boolean goLeft = false;
      private boolean goRight = false;

      public void mainGameLoop()
      {
            while (true)
            {
                  if (goLeft)
                  {
                        // go left
                 }
                  else if (goRight)
                  {
                        // go right
                 }
            }
      }

      public void keyPressed(KeyEvent e)
      {
            int code = e.getCode();
            if (code == KeyEvent.VK_LEFT)
            {
                  goLeft = true;
                  goRight = false;
            }
            else if (code == KeyEvent.VK_RIGHT)
            {
                  goRight = true;
                  goLeft = false;
            }
      }

      public void keyReleased(KeyEvent e)
      {
            int code = e.getCode();
            if (code == KeyEvent.VK_LEFT)
            {
                  goLeft = false;
            }
            else if (code == KeyEvent.VK_RIGHT)
            {
                  goRight = false;
            }
      }

}
Offline gruguru

Junior Newbie




Java games rock!


« Reply #2 - Posted 2003-05-28 21:20:54 »

I know what you're talking about, I have the same problem. If you find something, can you please tell me?

I'm trying to make a 2-player Pong game. The first player's pad is controlled with W and S, the second player's with the arrow keys. The problem is that for example if you press any other key while you're holding the S key, the pad will just stop wherever it is, until you release and repress the S key. So, that's really annoying. I programmed this using keystrokes and actions (if that's a very bad way of doing this, I apologize, I'm a newb).

Basically I have these lines in the main class's constructor:

1  
2  
3  
4  
5  
6  
7  
8  
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "p1pressedDown");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "p1pressedUp");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "p2pressedDown");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "p2pressedUp");
getActionMap().put("p1pressedDown", p1downAction);
getActionMap().put("p1pressedUp", p1upAction);
getActionMap().put("p2pressedDown", p2downAction);
getActionMap().put("p2pressedUp", p2upAction);


Then I have 4 inner classes (one for each action), each with a constructor, and an actionPerformed() method that calls the method used for moving the pad.

If someone could help me, that'd be great! Thanks a lot
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gruguru

Junior Newbie




Java games rock!


« Reply #3 - Posted 2003-05-31 02:18:24 »

ok MoO_Cow, I figured it out using the code at this url: http://forum.java.sun.com/thread.jsp?forum=406&thread=369480
I don't know if you're writing it for an applet or like me, my class extends JPanel. Well, there isn't much difference anyways. You just have to write an isFocusable method like that:
1  
2  
3  
public boolean isFocusable() {
            return true;
      }

That will override the isFocusable() method for java.Awt.Component .
If you ask for it, I'll send you my code. Hope this will help you.
Offline wjtprgm

Junior Newbie




Java games rock!


« Reply #4 - Posted 2003-06-05 03:27:34 »

yeah, that's a good link.  I see the problem as simply being that when another key is pressed, the repeat of the first key stops so the paddle stops moving.  My solution to keyboard problems has always been to set a pressed flag for the key in keyPressed and set it to false in keyReleased, that way you can press anything else you want and the game is not dependant on OS repeat delays, etc.
Offline Dr_Dogg

Junior Newbie




Dr. Dogg's private army will come for you too!


« Reply #5 - Posted 2003-06-05 10:26:19 »

Definitly agree about setting flags! With Java's input model, any other way is just asking for trouble. The problem gets worse when you start using 1.4's fullscreen api - syncing key presses to a fast running rendering loop just doesn't work very well.

Coming from a C++ game programming background I would appreciate some way of polling the input devices manually but I'll probably be flame grilled for saying that :-)
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #6 - Posted 2003-06-05 12:48:02 »

Quote
Definitly agree about setting flags! With Java's input model, any other way is just asking for trouble. The problem gets worse when you start using 1.4's fullscreen api - syncing key presses to a fast running rendering loop just doesn't work very well.

Coming from a C++ game programming background I would appreciate some way of polling the input devices manually but I'll probably be flame grilled for saying that :-)


U won't get flamed for saying that here.

*every1* wants that  Grin

oh the joy that is LWJGL  Cool

however,
Its looking brighter in that department.
The game api parts of MIDP2.0 allow you to poll the key states directly.
So u never know, we might see it in Java1.5
(now, wheres that fingers crossed smiley...)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Dr_Dogg

Junior Newbie




Dr. Dogg's private army will come for you too!


« Reply #7 - Posted 2003-06-06 09:31:43 »

Fingers need to be crossed pretty tight if you want to see many changes like that in 1.5 - seems mainly to be language updates (I like the look of generics - finally a typesafe way of using vectors etc.)

Also, LWJGL is pretty good - heck I learnt alot about OpenGL from it but with the hass' of loading textures into byte buffers etc. I am always tempted to revert to C++! If they improve the memory transfer system I'm in  Cheesy
Offline Jeff

JGO Coder




Got any cats?


« Reply #8 - Posted 2003-06-24 21:24:26 »

See JInput for controlelr discovery and polling, including the keyboard.

http://jinput.games.dev.java.net

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
8: Undefined index: online
File: /home/jgo/public_html/Themes/default/Display.template.php (main sub template - eval?)
Line: 151