Debouncing key presses involves ensuring that a single key press is registered only once, even if the key is held down for a longer duration.
This is particularly useful for preventing multiple triggers of a single action due to rapid key events.
Here’s an example using a timing-based approach to debounce key presses:
#include <SDL.h>
#include <iostream>
#include <chrono>
using namespace std::chrono;
class Window {
public:
Window() {
SDL_Init(SDL_INIT_VIDEO);
SDLWindow = SDL_CreateWindow(
"Debounce Key Presses",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN
);
}
~Window() {
SDL_DestroyWindow(SDLWindow);
SDL_Quit();
}
void HandleKeyboard() {
const Uint8* state = SDL_GetKeyboardState(NULL);
auto now = high_resolution_clock::now();
auto elapsed = duration_cast<milliseconds>(
now - lastPressTime).count();
if (state[SDL_SCANCODE_SPACE]
&& elapsed > debounceDelay) {
std::cout << "Space key pressed\n";
lastPressTime = now;
}
}
private:
SDL_Window* SDLWindow{nullptr};
high_resolution_clock::time_point lastPressTime{};
const int debounceDelay{200}; // 200 milliseconds
};
int main(int argc, char* argv[]) {
Window GameWindow;
bool running = true;
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
GameWindow.HandleKeyboard();
// Rendering code here
}
return 0;
}
Space key pressed
Here’s an example using a state-based approach:
#include <SDL.h>
#include <iostream>
class Window {
public:
Window() {
SDL_Init(SDL_INIT_VIDEO);
SDLWindow = SDL_CreateWindow(
"State-Based Debounce",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN
);
std::memset(prevState, 0, sizeof(prevState));
}
~Window() {
SDL_DestroyWindow(SDLWindow);
SDL_Quit();
}
void HandleKeyboard() {
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_SPACE]
&& !prevState[SDL_SCANCODE_SPACE]) {
std::cout << "Space key pressed\n";
}
std::memcpy(
prevState, state, sizeof(prevState));
}
private:
SDL_Window* SDLWindow{nullptr};
Uint8 prevState[SDL_NUM_SCANCODES];
};
int main(int argc, char* argv[]) {
Window GameWindow;
bool running = true;
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
GameWindow.HandleKeyboard();
// Rendering code here
}
return 0;
}
Space key pressed
Debouncing key presses in SDL can be achieved through timing-based or state-based methods.
The timing-based method uses a timer to ensure a key press is registered only after a certain delay, while the state-based method checks the state transition of the key.
Both methods are effective, and the choice depends on the specific requirements of your application.
Answers to questions are automatically generated and may not have been reviewed.
Learn how to detect and handle keyboard input in SDL2 using both event-driven and polling methods. This lesson covers obtaining and interpreting the keyboard state array.