Public Access
Apps heard a key once, when it went down. A Game that moves something for as long as a key is held (issue #101) asks the key mapper instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
54 lines
1.8 KiB
C++
54 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
#include "key_event.h"
|
|
|
|
namespace roro {
|
|
|
|
// The physical keyboard state as reported by the keyboard driver: every character key currently
|
|
// held (already shifted) plus the modifier and special keys.
|
|
struct RawKeys {
|
|
std::vector<char> chars;
|
|
bool fn = false;
|
|
bool opt = false;
|
|
bool ctrl = false;
|
|
bool alt = false;
|
|
bool shift = false;
|
|
bool enter = false;
|
|
bool del = false;
|
|
bool tab = false;
|
|
};
|
|
|
|
// Turns keyboard state changes into logical KeyEvents: only newly pressed keys produce events;
|
|
// Fn + ; . , / are arrows, and so are ; . , / alone when no text is being entered; ` is Back and
|
|
// Fn + ` is Home; Fn + h is Help anywhere, and so is ? when no text is being entered; the Compose Key (opt) followed by an accent and a letter types the accented
|
|
// letter (opt ' e -> é).
|
|
class KeyMapper {
|
|
public:
|
|
std::vector<KeyEvent> update(const RawKeys& keys);
|
|
|
|
// Whether the foreground App is editing text (default). Outside text entry, the arrow keys
|
|
// don't need Fn.
|
|
void setTextEntry(bool active) { textEntry_ = active; }
|
|
|
|
// Is this key down right now, as of the last update()? Events only say when a key goes down:
|
|
// a Game that moves something for as long as a key is held asks this (issue #101). The arrows
|
|
// are ; . , / as for events: alone outside Text Entry, with Fn anywhere.
|
|
bool held(Key key) const;
|
|
bool heldChar(char c) const; // a character key, as it types
|
|
|
|
// 0 when idle, '*' when opt was pressed, or the accent character waiting for its letter.
|
|
char pendingCompose() const { return compose_; }
|
|
|
|
private:
|
|
void onChar(char c, const RawKeys& keys, std::vector<KeyEvent>& out);
|
|
|
|
RawKeys previous_;
|
|
char compose_ = 0;
|
|
bool textEntry_ = true;
|
|
};
|
|
|
|
} // namespace roro
|