Detecting Multiple Keys in a Specific Order
How can I detect if multiple keys are held down in a specific order?
Detecting multiple keys held down in a specific order can be useful for complex input sequences, such as cheat codes or combo moves in a game.
This involves tracking the sequence of key presses and ensuring they match the desired order.
Approach
- Track Key Presses: Maintain a list to record the sequence of key presses.
- Compare Sequence: Compare the recorded sequence with the desired sequence.
- Reset on Mismatch: Reset the sequence if a mismatch occurs.
Example
Here's an example demonstrating this approach. This example checks for two presses of the up arrow, followed by two presses of the down arrow:
#include <SDL.h>
#include <iostream>
#include <vector>
class Window {
public:
Window() {
SDL_Init(SDL_INIT_VIDEO);
SDLWindow = SDL_CreateWindow(
"Detect Key Order",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN
);
}
~Window() {
SDL_DestroyWindow(SDLWindow);
SDL_Quit();
}
void HandleKeyboard() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
keySequence.push_back(
event.key.keysym.scancode);
if (keySequence.size() > desiredSequence.size()) {
keySequence.erase(keySequence.begin());
}
CheckSequence();
}
}
}
private:
SDL_Window* SDLWindow{nullptr};
std::vector<SDL_Scancode> keySequence;
const std::vector<SDL_Scancode> desiredSequence {
SDL_SCANCODE_UP, SDL_SCANCODE_UP,
SDL_SCANCODE_DOWN, SDL_SCANCODE_DOWN
};
void CheckSequence() {
if (keySequence == desiredSequence) {
std::cout << "Sequence matched!\n";
keySequence.clear(); // Reset sequence after match
}
}
};
int main(int argc, char* argv[]) {
Window GameWindow;
bool running = true;
while (running) {
GameWindow.HandleKeyboard();
// Rendering code here
}
return 0;
}
Sequence matched!
Explanation
- Track Key Presses: The
keySequence
vector is used to record the sequence of key presses. - Compare Sequence: The recorded sequence is compared with the desired sequence (
desiredSequence
). If they match, a message is printed. - Reset on Mismatch: If the length of
keySequence
exceedsdesiredSequence
, the oldest key is removed to maintain the correct length.
Considerations
- Timing: You might want to add a timeout mechanism to reset the sequence if the keys are not pressed within a certain time frame.
- Edge Cases: Consider edge cases where the same key might be pressed multiple times unintentionally.
Conclusion
Detecting multiple keys in a specific order involves tracking the sequence of key presses and comparing it with the desired order.
This method is useful for implementing complex input sequences and can be extended with additional features such as timeouts for more robust handling.
Understanding Keyboard State
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.