Understanding Keyboard State

Detecting Multiple Keys in a Specific Order

How can I detect if multiple keys are held down in a specific order?

Abstract art representing computer programming

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

  1. Track Key Presses: Maintain a list to record the sequence of key presses.
  2. Compare Sequence: Compare the recorded sequence with the desired sequence.
  3. 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 exceeds desiredSequence, 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.

This Question is from the Lesson:

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.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

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.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 36 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved