Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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 2505 times)
0 Members and 1 Guest are viewing this topic.
Offline MoO_coW

Junior Newbie




Java games rock!


« Posted 2003-03-11 03: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 02: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 23: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 04: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 05: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 12: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 Coder


Medals: 11


falling into the abyss of reality


« Reply #6 - Posted 2003-06-05 14: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 11: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 23: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 - Posted 2003-07-12 08: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tortoise

Junior Member




<3 Shmups


« Reply #10 - Posted 2003-07-12 22: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]
  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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (23 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!