Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  How do you read a key press?  (Read 1454 times)
0 Members and 1 Guest are viewing this topic.
Offline kents1

Senior Newbie




Java games rock!


« Posted 2003-03-26 10:40:01 »

Is there a way to perform a key-press rather than a key down?  By checking if a key is down, then swapping a state (in my instance switching on and off lighting), it constantly switches state and it's random which state I get because I'm not fast enough to get my finger off the button before it has 'looped' again.
There has to be a better way to do this?
Offline cfmdobbie

Senior Member




Who, me?


« Reply #1 - Posted 2003-03-26 10:55:20 »

Sure, implement something like the following:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
boolean F12_DOWN = false ;

if(Keyboard.isKeyDown(Keyboard.KEY_F12))
{
      if(F12_DOWN == false)
      {
            F12_DOWN = true ;
            swapLighting() ;
      }
}
else
      F12_DOWN = false ;

Hellomynameis Charlie Dobbie.
Offline princec
« League of Dukes »

JGO Kernel


Medals: 194
Projects: 3


Eh? Who? What? ... Me?


« Reply #2 - Posted 2003-03-26 14:25:56 »

Also the new keyboard buffer will do it I believe:

1  
2  
3  
4  
5  
6  
7  
8  
9  
Keyboard.enableBuffer();
...
Keyboard.read();
while (Keyboard.next()) {
      if (Keyboard.state) {
            int keyCode = Keyboard.key;
            ...
      }
}


This gives you a series of discrete up and down events as they occur instead of a simple polling state.

Cas Smiley

Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline kents1

Senior Newbie




Java games rock!


« Reply #3 - Posted 2003-03-26 15:42:53 »

Quote
Sure, implement something like the following:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
boolean F12_DOWN = false ;

if(Keyboard.isKeyDown(Keyboard.KEY_F12))
{
      if(F12_DOWN == false)
      {
            F12_DOWN = true ;
            swapLighting() ;
      }
}
else
      F12_DOWN = false ;



This is kind of what I had.  The thing is, if your game does 200 FPS (...9700 Pro  Smiley) and you push the button for 1/4 second, the F12_DOWN variable has switched on and off about 50 times.  It is not guaranteed to be the opposite of the state it was when you pushed the key.

Cas... how does the buffer state help?  Can I detect in the buffer the key being pushed, followed by it being released?
Offline princec
« League of Dukes »

JGO Kernel


Medals: 194
Projects: 3


Eh? Who? What? ... Me?


« Reply #4 - Posted 2003-03-26 17:55:11 »

If you look a bit more closely at Charlie's code you'll see that it does nothing on subsequent frames until the key is released, although he hasn't made it clear that F12_down is an instance variable somewhere in a class, not a local variable.

This is more or less what's going on in the directinput driver behind the Keyboard buffer anyway. You get single key down / key up events. Key repeat does not occur.

Cas Smiley

Offline cfmdobbie

Senior Member




Who, me?


« Reply #5 - Posted 2003-03-26 20:51:00 »

Er, yeah.  Sorry, I thought about mentioning that explicitly, but for some reason decided not to! Grin

Interesting, I hadn't noticed the keyboard buffer system in LWJGL yet - I'll be sure to give it a try.

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

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (32 views)
2013-05-17 21:29:12

alaslipknot (41 views)
2013-05-16 21:24:48

gouessej (71 views)
2013-05-16 00:53:38

gouessej (69 views)
2013-05-16 00:17:58

theagentd (78 views)
2013-05-15 15:01:13

theagentd (73 views)
2013-05-15 15:00:54

StreetDoggy (113 views)
2013-05-14 15:56:26

kutucuk (137 views)
2013-05-12 17:10:36

kutucuk (137 views)
2013-05-12 15:36:09

UnluckyDevil (145 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.118 seconds with 21 queries.