Simple SDL2 keyboard key status

Welcome to this tutorial where we are going to see how to implement a simple keyboard key status using SDL 2. This tutorial will help us to check the status of a key (triggered, pressed or released).

First of all we need to define a header that contains the necessary functions and variables to be able to check the status of a key.

#ifndef __INPUTKEYBOARD_H__
#define __INPUTKEYBOARD_H__
#pragma once
///////////////////////////////

class InputKeyboard
{
public:
	///--- Constructor/Destructor
	InputKeyboard() ;
	~InputKeyboard();

	///--- Object control
	bool Initialize(void);
	void Update(void);

	///--- Keyboars state function
	bool IsKeyTriggered(const SDL_Scancode iKeyCode) const;
	bool IsKeyPressed(const SDL_Scancode iKeyCode) const;
	bool IsKeyReleased(const SDL_Scancode iKeyCode) const;
private:
	///--- Last frame Keyboard state
	Uint8 m_piPreviousInput[SDL_NUM_SCANCODES];
	///--- Current frame keyboard state
	Uint8 m_piCurrentInput[SDL_NUM_SCANCODES];
};

///////////////////////////////
#endif   //__INPUTKEYBOARD_H__

SDL gives us the opportunity to get the current state of all the keys through the function SDL_GetKeyboardState. This function returns a pointer to an array of Uint8 of size SDL_NUM_SCANCODES.

Since SDL gives us that data we can create the array directly at the header preallocating the necessary memory.

We are going to initialize set the previous frame state array to 0 since we do not have data and to avoid get false states during the first frame and get the information of the current frame from SDL.

bool InputKeyboard::Initialize(void)
{
	///--- Initialize the las frame array to 0
	memset(m_piPreviousInput, 0, sizeof(Uint8)*SDL_NUM_SCANCODES);
	///--- Copy the current status
	memcpy(m_piCurrentInput, SDL_GetKeyboardState(NULL), sizeof(Uint8)*SDL_NUM_SCANCODES);
	return true;
}

The Update function must be called every frame to make it work correctly and we only have to copy the current frame memory to the previous frame state array and get the current state from SDL.

void InputKeyboard::Update(void)
{
	memcpy(m_piPreviousInput, m_piCurrentInput, sizeof(Uint8)*SDL_NUM_SCANCODES);	
	memcpy(m_piCurrentInput, SDL_GetKeyboardState(NULL), sizeof(Uint8)*SDL_NUM_SCANCODES);
}

As you can see the function SDL_GetKeyboardState can take a pointer to an int that returns the number of keys, since the value is exactly SDL_NUM_SCANCODES it’s not necessary for us to get that information.

Now we have the information of the previous and the current frame status, so the only thing we have to do is compare the values to chek if the key is triggered, pressed or released.

The IsKeyTriggered function is used to check when a key is pushed down this current frame. So when we press down the key this function will return true since the state on the previous frame was 0 and the state on the current frame is 1.

bool InputKeyboard::IsKeyTriggered(const SDL_Scancode iKeyCode) const
{
	return (m_piCurrentInput[iKeyCode]==1 && m_piPreviousInput[iKeyCode]==0);
}

The IsKeyPressed function is used to check if a key is down. In this case we only have to check if the current state of the key is 1.

bool InputKeyboard::IsKeyPressed(const SDL_Scancode iKeyCode) const
{
	return (m_piCurrentInput[iKeyCode]==1);
}

The IsKeyReleased function is used to check when the key stops being pushed. It’s just the opposite of triggered function so we check if the last frame state was 1 and the current state is 0.

bool InputKeyboard::IsKeyReleased(const SDL_Scancode iKeyCode) const
{
	return (m_piCurrentInput[iKeyCode]==0 && m_piPreviousInput[iKeyCode]==1);
}

Since we do not have to allocate anything the constructor and destructor are empty functions.

Feel free to use this code without any restriction and share this page if you think this may be useful for other developers.

Guardar

Guardar

Guardar

Guardar

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.