Sign in to follow this  

SDL holding a keydown

This topic is 3592 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

I have the arrow keys set to move the player around and I need it to allow key holding instead of pressing constantly. I have done it before in other projects following a tutorial but that method doesnt work in this case for some reason.. Here's what I have so far-
void Player::Events(SDL_Event event)
{
	if(event.type == SDL_KEYDOWN)
	{
		switch(event.key.keysym.sym)
		{
		case SDLK_UP:
			Move(0, -4);
			break;
		case SDLK_DOWN:
			Move(0, 4);
			break;
		case SDLK_RIGHT:
			Move(4, 0);
			break;
		case SDLK_LEFT:
			Move(-4, 0);
			break;
		}
	}
	else if(event.type == SDL_KEYUP)
	{
		switch(event.key.keysym.sym)
        {
        case SDLK_UP:
			break;
		case SDLK_DOWN:
			break;
		case SDLK_RIGHT:
			break;
		case SDLK_LEFT:
			break;   
        }
	}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Did you try SDL_EnableKeyRepeat()?


I have now ;) works great now.

Is there anyway of pressing/holding down two keys at once? I've tried this
event.key.keysym.sym == SDLK_DOWN && event.key.keysym.sym == SDLK_RIGHT
but that didnt work.

Share this post


Link to post
Share on other sites
Quote:
Original post by namingway
Quote:
Original post by SiCrane
Did you try SDL_EnableKeyRepeat()?


I have now ;) works great now.

Is there anyway of pressing/holding down two keys at once? I've tried this
event.key.keysym.sym == SDLK_DOWN && event.key.keysym.sym == SDLK_RIGHT
but that didnt work.

I can explain why that doesn't work. When you use the code above to check which type of event your event is, you are only checking the current event. For example, your event may be SDLK_DOWN, with another one sitting behind it in line called SDLK_LEFT. You have checked, however, whether or not the SDLK_DOWN event is both the SDLK_DOWN and SDLK_LEFT event, which can't happen. See what I'm getting at? What you need to do is just check each key separately. To do this, use the SDL_PollEvent(SDL_EVENT &event) function as follows:

while( SDL_PollEvent(&event) )
{
//this is all your existing code
}




This will check each event in the queue until it runs out of events. This means that you should have a separate event for pressing each direction you are holding down and each one will be handled separately. This is fine because you will move the player both directions, simulating a diagonal.

Share this post


Link to post
Share on other sites
Try this:



//keyboard_control.h

class keyboard_control;
#ifndef _KEYBOARD_CONTROL_
#define _KEYBOARD_CONTROL_

class keyboard_control
{
private:
unsigned char key_state[127];
public:
keyboard_control::keyboard_control();
void keyboard_control::set_key(unsigned char key_code,unsigned char new_state);
unsigned char keyboard_control::get_key(unsigned char key_code);
};
#endif





#include "keyboard_control.h"

keyboard_control::keyboard_control()
{
int index = 0;
for(index = 0; index <= 127; index++)
key_state[index]=(char)0;
};

void keyboard_control::set_key(unsigned char key_code,unsigned char new_state)
{
key_state[key_code]=new_state;
}

unsigned char keyboard_control::get_key(unsigned char key_code)
{
return key_state[key_code];
}





Simple but effective. When you get a keydown event, use set_key(SDLK_a,1). When you get a keyup event, use set_key(SDLK_a,0);

then, in your code, use get_key(SDLK_a) to figure out if they are pressing A.


Share this post


Link to post
Share on other sites

This topic is 3592 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this