Jump to content
  • Advertisement
Sign in to follow this  
_MM_UnL1573d

Detecting Keypresses

This topic is 4851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, On my game I'm doing, when the user presses a key to turn the object, it works, but I need to know if there is a C++/C/Allegro function I can use to make the game think the key is no longer pressed, if you know what I mean. Do you guys know of any such function, or can you help me figure out a way to overcome this obstacle? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
you might want to look into buffered input. Are you using getasynckeystate or dircet input?

buffered input basically chcks if key is down, then executes an action but wont recognize keypress again till the key is released then pressed, or you could just check for keyup instead. WHich is the more hacked way of doing it.

Share this post


Link to post
Share on other sites
I haven't heard of any function, perhaps there is one but I don't know of it.

What you could do is have an input handler within your game loop set to detect when a key is pressed, and when it is released. You could use a switch or if statement to determine what to do on certain keypresses. You could then set boolean variables to true or false depending on whether said key is pressed or not.

Pseudocode:

bool spacepressed; // Boolean variable - true means space is pressed, false means it isn't.

Button press detected:

if(Button pressed is space)
{
spacepressed=true;
}


Button release detected:

if(Button released is space)
{
spacepressed=false;
}


while(spacepressed)
{
do what you want while space is held down - if you have a game loop set up one press will only affect its own tick, providing your keyhandler function is in the game loop as well
}


I hope this has given you a handle on how to do it. This is how I do it with SDL, if you're confused please let me know and I'll try and explain further.

HTH,

ukdeveloper.

Share this post


Link to post
Share on other sites
It might actually be better to have an array control it:
bool bKeyDown = bool array of all keys on the keyboard;

Event_KeyDown(keyNumber){
bKeyDown[keyNumber] = true;
}

Event_KeyUp(keyNumber){
bKeyDown[keyNumber] = false;
}

You could then make an enumeration of all the keys to determine which key was pressed:
enum Key {
A=32,
B=61,
C=41 // ... and so on
}

if(bKeyDown[Key.B])
// The B key is down!!!
This might even already be done by the API you're using, just check the documentation. Oh, and excuse my horrible pseudocode.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!