SDL keypress not detected until key is pressed multiple times

I am trying to make a very simple game engine (sort of) as a learning experience and I've gotten everything working fine right now, although there's a lot I have to optimize and some of it is very messy, except for my code to make the player object jump. It checks to see if the 'w' key has been pressed and if the player is not in the air, and it works the problem is sometimes I have to hit the 'w' key multiple times before the key press is detected and it only occurs when I check for 'w' key.

I have tried to find the answer on my own, looking at my code over and over again but I just can't figure out why sometimes I have to press the key multiple times and sometimes I don't. Here is my code for it, sorry you have to look at it to answer my question and if it's hard to understand:

Event handling code (I know it's probably a bad way to do it)-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Input.h"
#include "SDL/SDL.h"

//The pointer that holds all the keys states this
//can be used to see if a key is being held down
static Uint8 *keysdown = SDL_GetKeyState( NULL );

//The event structure used mainly for a direct key-press
//check and not if a key is being held down
static SDL_Event event;

//This is updated every time EventUpdate is called and is used to tell if
//events have been polled or not, if they have then it's true else it's false
static bool events_polled = false;

void EventWait()
{
	SDL_WaitEvent( &event );
}

bool EventUpdate()
{
	//Poll events and if things were updated then return true
	if( SDL_PollEvent( &event ) )
	{
		events_polled = true;
		return true;
	}
	
	//Otherwise if it failed or didn't need to update return false
	events_polled = false;
	return false;
}

bool quit_check()
{
	//If the quit event has been polled return true
	if( events_polled && event.type == SDL_QUIT )
	{
		return true;
	}
	
	//If it has not then return false
	return false;
}

bool keyboard_check( SDLKey key )
{
        //If the key is being pressed then return true
    if( keysdown[ key ] )
    {
       return true;
    }

	//If it is not then return false
	return false;
}

bool keyboard_check_pressed( SDLKey key )
{
	//If the key has just been pressed
	if( events_polled && event.type == SDL_KEYDOWN && event.key.keysym.sym == key )
	{
		return true;
	}

	//If it was not then return false
	return false;
}

bool keyboard_check_released( SDLKey key )
{
    //If the key has just been released
    if( events_polled && event.type == SDL_KEYUP && event.key.keysym.sym == key )
    {
		return true;
    }

    //If it was not then return false
    return false;
}

int mouse_check_button()
{
	if( events_polled && ( event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP ) )
	{
		return event.button.button;
	}
	
	return 0;
}


Jump code for player, the key_up_pressed variable is set before this using keyboard_check_pressed()-
1
2
3
4
5
6
7
8
9
10
11
12
13
//This will tell the Draw event whether the player is jumping or not
if( collision_tile_bottom( x, w, y, h, level, TILE_FLOOR ) == noone && !( yvel >= 0 && collision_tile_bottom( x, w, y, h, level, TILE_LEDGE ) != noone ) )
{
	inAir = true;
}
else{ inAir = false; }
	
//if we are on the ground and the up key is pressed
if( !inAir && collision_tile_top( x, w, y, h, level, TILE_FLOOR ) == noone && key_up_pressed )
{
	//make the player jump
	jumping = true; jumpTime += frameTime;
}


If you need any more of my code to understand it better I apologize and will post it asap. Thanks in advance to anyone who looks at this.
Topic archived. No new replies allowed.