Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Keyboard input?  (Read 1799 times)
0 Members and 2 Guests are viewing this topic.
Offline MoO_coW

JGO n00b
*

Posts: 4


Java games rock!


« on: 2003-03-10 21: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

JGO n00b
*

Posts: 16


Java ees kewl, man!


« Reply #1 on: 2003-03-11 20: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

JGO n00b
*

Posts: 2


Java games rock!


« Reply #2 on: 2003-05-28 17: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! Go get 'em!
Offline gruguru

JGO n00b
*

Posts: 2


Java games rock!


« Reply #3 on: 2003-05-30 22: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

JGO n00b
*

Posts: 2


Java games rock!


« Reply #4 on: 2003-06-04 23: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

JGO n00b
*

Posts: 2


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


« Reply #5 on: 2003-06-05 06: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 Kernel
*****

Posts: 1866
Medals: 5


falling into the abyss of reality


« Reply #6 on: 2003-06-05 08: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...)
Offline Dr_Dogg

JGO n00b
*

Posts: 2


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


« Reply #7 on: 2003-06-06 05: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 Kernel
*****

Posts: 3535


Got any cats?


« Reply #8 on: 2003-06-24 17: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
Archimedes
Guest
« Reply #9 on: 2003-07-12 02:36:45 »

Ok, so in the keyPressed and keyReleased event handlers you store the current state of the "movement" keys in key state variables.

The question is: how often and where do you test these variables? (I am talking about a Java application, not a applet)

With a Model-View-Controller (MVC) architecture there's an interrupt driven simulation module whichs handles the model and its timing (usually the only fixed timer driven event). So a TimerTask (or that like) is already running.
However you'll hesitate to poll the key state variables from within that simulation TimerTask, because logically it doesn't fit in there, it has to be in the controller module (of course).

So you've to start another TimerTask just to handle the key state variables. This looks to be quite "heavy" to me - another Thread just to scan 5 keys, oh dear. :-)
Since you also want to use the mouse buttons as movement "keys", too, I think you'll have to create a second TimerTask, isn't it?

Or do I miss the point now?

Okay, you could use Jinput. However for now it's Win32 only (the other platforms coming). Also I hesitate to include another binary binding (next to Jogl's) "just" to be able to scan 5 keys and a mouse. :-)
No question, for larger Java games which support Joypads/etc. Jinput is very important.
Games published by our own members! Go get 'em!
Offline tortoise

Full Member
**

Posts: 230


<3 Shmups


« Reply #10 on: 2003-07-12 16:13:13 »

be warned, the typical keyPressed keyReleased boolean approach will not work in Linux. It reports key events differently than other OSes do, a key held down will report as many keyReleased events as it does keyPressed events.

Until I get off my rear and look into jinput and such I'm dealing with this by disabling key repeat when in Linux (just a simple Runtime.exec("xset r off")). And yup, that is as much a ripe pain the ass as it sounds.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.112 seconds with 21 queries.