Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 1771 times)
0 Members and 1 Guest are viewing this topic.
Offline kents1

Senior Newbie




Java games rock!


« Posted 2003-03-26 09: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 Duke


Medals: 1


Who, me?


« Reply #1 - Posted 2003-03-26 09: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

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2003-03-26 13: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!
Legends of Yore - The Casual Retro Roguelike
Offline kents1

Senior Newbie




Java games rock!


« Reply #3 - Posted 2003-03-26 14: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

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2003-03-26 16: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 Duke


Medals: 1


Who, me?


« Reply #5 - Posted 2003-03-26 19: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.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (53 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (43 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!