Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Best way to handle Keyboard Input?  (Read 1449 times)
0 Members and 1 Guest are viewing this topic.
Offline pifreak

Senior Newbie





« Posted 2009-03-03 01:20:21 »

I am going to try a few ways I've seen, they are all very different, but I want to know what is your favorite way to do it Cheesy

What is easy?
What is efficient?
What is the best for games?
Offline Gudradain
« Reply #1 - Posted 2009-03-03 02:26:59 »

I'm interested with this answer too  Grin.

I did my own system for my game but I'm not sure if it's the best. What I did is basically putting a KeyListener in my JFrame than this KeyListener send the character data it receive as an event to all my Object. Than I create a class call KeyController and I associate one to every Object. The KeyController receive the data sent by the KeyListener in the JFrame than check if this data (character) correspond to an action and if yes modify the Object.

I really just take the easiest road here, so it might be the worse method (I don't know) but it works.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #2 - Posted 2009-03-04 14:49:34 »

http://www.google.com/search?hl=en&q=how+to+get+keyboard+input+in+java&btnG=Google+Search&aq=f&oq=

this is a very simple thing.

post back after you read it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #3 - Posted 2009-03-04 15:01:19 »

I find it slightly ironic that you would make a reply like that, h3ckboy. ;-)

For keyboard input, I usually make a keyboard handler class that listens to keyboard events from my game component (usually a JComponent or a Canvas). When a key is pressed, I mark it as pressed in a boolean[], when it is released, I unmark it.
I can also forward specific key events from there to ensure individual key presses don't get missed in a low framerate situation (very very frustrating for the user).
If an event requires the user to keep a key pressed (like moving a character), I use the boolean[]. If it should trigger on a single key press (like entering letters into a chat, or activating hyperspace, or whatever), I use the actual key listeners.
Keep in mind that just forwarding the key events from the awt event dispatcher thread can cause synchronization problems, so a more complex solution would be to queue all key presses (or rather, key types) into a list, then request them every frame from the game thread.

One thing to keep in mind is that you will not get key release events if your component doesn't have keyboard focus. This is a very common problem in many games, including "Big" titles. So I usually add a focus listener as well, and unset ALL keys when the focus is lost.

Play Minecraft!
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #4 - Posted 2009-03-04 15:29:56 »

Yeah I was thinking abouit that as I posted.

For the record though I had already googled it.

I would have found it except I didnt know it was called vector math.

back on topic:

ok, so if oyu are using java 2D, you can implement keyListener, try to compile it will explain the rest Smiley.

you can name the variables it specifies anything oyu want.

GL.

P.S: what oyu never seen a hypocrite b4 Wink
Offline fletchergames

Senior Member





« Reply #5 - Posted 2009-03-04 19:06:37 »

For keyboard input, I usually make a keyboard handler class that listens to keyboard events from my game component (usually a JComponent or a Canvas). When a key is pressed, I mark it as pressed in a boolean[], when it is released, I unmark it.
I do much the same thing except that I use an enum to store the key state as either UNPRESSED, PRESSED, RELEASED_AND_UNPRESSED, or RELEASED_AND_PRESSED.

For some keys, I check whether they've been released, and for others I check whether they're currently pressed.

One thing to keep in mind is that you will not get key release events if your component doesn't have keyboard focus. This is a very common problem in many games, including "Big" titles. So I usually add a focus listener as well, and unset ALL keys when the focus is lost.
I "solved" this problem by only having one JFrame containing one JPanel with no other Swing components.  I use my own custom widgets inside of the JPanel.  That's a somewhat extreme solution, but there were other things I was trying to deal with as well.

An alternative would be to use JInput.  I've tried it out for keyboard input, but it doesn't seem to be any particular improvement.  I plan to use JInput for reading gamepad input, KeyListener for keyboard input, and MouseListener for mouse input.  The KeyListener will be implemented as described above.

Edit: I should mention that my custom widgets don't work that way except for the main game widget  For the other widgets, I just send the key event to the focused widget like Swing would do.  I actually overrode the regular key input method for my main game widget to store the presses and releases.  Since the main game widget is the only focusable widget visible while you're playing the game, I didn't need to worry about redirecting the input to other widgets within the main game widget.
Offline pifreak

Senior Newbie





« Reply #6 - Posted 2009-03-06 13:13:33 »

oh, sorry for delayed response, I think either my key input is perfect and my movement is bad, or the key input is bad.

I added a class for a keylistener, and it flips booleans on or off for up, down, right, left in the main class.

In the main class I have this to repeat:

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  
51  
52  
for(;;)
           {

              if (down && right)
              {
                                //change the image to fit the direction he is going
                          sprite.setImg("sprite_rd.png");

                                //move him one pixel for each direction he is going
                          sprite.setXY(sprite.getX()+1,sprite.getY()+1);
              }
              else if (up && right)
              {
                                //etc...
                sprite.setImg("sprite_ru.png");  
                 sprite.setXY(sprite.getX()+1,sprite.getY()-1);
              }
              else if (down && left)
              {
                 sprite.setImg("sprite_ld.png");  
                 sprite.setXY(sprite.getX()-1,sprite.getY()+1);
              }
              else if (up && left)
              {
                 sprite.setImg("sprite_lu.png");  
                 sprite.setXY(sprite.getX()-1,sprite.getY()-1);
              }
              else if (down)
              {
                 sprite.setXY(sprite.getX(),sprite.getY()+1);
                           sprite.setImg("sprite_d.png");  
              }
              else if (up)
              {
                 sprite.setXY(sprite.getX(),sprite.getY()-1);
                 sprite.setImg("sprite_u.png");  
              }
              else if (right)
              {
                 sprite.setXY(sprite.getX()+1,sprite.getY());
                 sprite.setImg("sprite_r.png");  
              }
              else if (left)
              {
                 sprite.setXY(sprite.getX()-1,sprite.getY());
                 sprite.setImg("sprite_l.png");  
              }        

              //update
             sprite.repaint();

           }


I guess now that I have keys responding very quickly, there are two problems:
-he moves very fast >.<
-the cpu usage is really high even when he isn't doing anything because it is looping constantly and going through the if-statements.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #7 - Posted 2009-03-06 14:33:24 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
you need to have it check only once every... 5 milliseconds.

like this:

[code]
if(System.currentTimeMillis() - lastReload > 5)
{
lastReload = System.currentTimeMillis();
//all taht chekcing stuff
}

if problem persists, jsut increas the amount of time it has to wait.[/code]
Offline pjt33
« Reply #8 - Posted 2009-03-06 16:22:11 »

I guess now that I have keys responding very quickly, there are two problems:
-he moves very fast >.<
-the cpu usage is really high even when he isn't doing anything because it is looping constantly and going through the if-statements.
You should have some kind of waiting mechanism in your loop to limit the frame rate. I won't go into details here: you can find examples in the resources page for the 4k contest forum.
Offline pifreak

Senior Newbie





« Reply #9 - Posted 2009-03-06 22:26:29 »

okay it works now, my lagg problem is fixed with a 1ms thread sleeping.

It laggs when *attempting* to refresh the images constantly, like 10000fps so I need to limit it since it won't ever do that good, it just laggs.
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.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (26 views)
2014-09-07 01:10:19

mitcheeb (34 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!