Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 18, 2013 2:12 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Mon Apr 23, 2012 6:01 pm 
Offline

Joined: Mon Apr 16, 2012 5:04 pm
Posts: 14
I looked through the javadoc and did a search but couldn't turn anything up. I don't see any methods such as iisKeyPressed() that just tell me if any key is being pressed. I know I could do it by running a loop over all the possible keys and checking those, but that seems rather inefficient.

For example, I was mocking up a little typing game. I wanted it to deduct points if you pressed the wrong key, but I couldn't figure out a way to detect any key using Slick without checking all the keys individually.

Just a simple isKeyPressed() and isKeyDown() to return a boolean value if any key is pressed or held down would help.


Top
 Profile  
 
PostPosted: Mon Apr 23, 2012 6:30 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1466
Override keyPressed/keyReleased in BasicGame or BasicGameState.


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 7:23 am 
Offline

Joined: Mon Apr 16, 2012 5:04 pm
Posts: 14
That works. Is there a reason that it's not an included method?


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 7:36 am 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
It 'is' included, you get it from the Input object in GameContainer for use in the update method.

Code:
public void update(GameContainer gc, int delta){
   if(gc.getInput().isKeyPressed(Input.someKeyConstant)){
      // the key was pressed do something here.
   }
}

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 7:41 am 
Offline

Joined: Mon Apr 16, 2012 5:04 pm
Posts: 14
By 'it' I meant the generic isKeyPressed() and isKeyDown() method I was looking for, not isKeyPressed(key) and isKeyDown(key).


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 7:52 am 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
Oh guess i missed a few things when i read your post, anyway it's not included because of the methods that davedes mentioned, so it isn't really needed, but if you think its needed try writing an RFE.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 8:16 am 
Offline

Joined: Mon Apr 16, 2012 5:04 pm
Posts: 14
It's not needed but it would make for a good addition in my opinion so programmers don't have to reinvent that wheel all the time. They would be nice, easy to write functions that could be used in a lot of games.


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 5:01 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1466
I could add isKeyDown() if you want, as well as isMouseDown/etc. This will simply return true if 'any' key/mouse button/controller button is currently down.

isKeyPressed(), I'm not really sure how that would work or what purpose it would serve, as it checks whether a particular key was down the last frame, and is now up. As I said, you can already check for "any key release" by overriding keyReleased or "any key down" by overriding keyPressed.

Overriding keyPressed/keyReleased is the "appropriate" way to handle input in Slick when you don't need to poll for it every frame (i.e. when you just need to listen for the event).


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 6:15 pm 
Offline

Joined: Mon Apr 16, 2012 5:04 pm
Posts: 14
A generic isKeyPressed() could be used for when you're required to hit a certain sequence of keys without messing up. For example you have to hit Q,W,E,R,T, and Y in order, so the code could be something like if (isKeyPressed(currentKey)) //continue else if (isKeyPressed()) //you missed.

Just the isKeyDown() methods would be good though if you don't feel the pressed variants are necessary. Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group