Cogs.Core
Keyboard.h
1#pragma once
2#include "../FoundationBase.h"
3
4#include <variant>
5#include <vector>
6#include <string>
7
8namespace Cogs
9{
10 enum class Key
11 {
12 A, B, C, D, E,
13 F, G, H, I, J,
14 K, L, M, N, O,
15 P, Q, R, S, T,
16 U, V, W, X, Y,
17 Z,
18
19 Zero, One, Two, Three, Four,
20 Five, Six, Seven, Eight, Nine,
21
22 Left, Right, Up, Down,
23
24 Shift, LeftShift, RightShift,
25 Control, LeftControl, RightControl,
26 Alt, LeftAlt, RightAlt,
27
28 CapsLock,
29 Tab,
30 Escape,
31 Enter,
32 Space,
33
34 Insert, Delete, Backspace,
35 Home, End,
36 PageUp, PageDown,
37
38 F1, F2, F3, F4,
39 F5, F6, F7, F8,
40 F9, F10, F11, F12,
41
42 Count,
43 None = -1,
44 };
45
46 class COGSFOUNDATION_API Keyboard
47 {
48 public:
49 struct State
50 {
51 bool keysDown[static_cast<size_t>(Key::Count)] = {};
52 std::string chars;
53 };
54
55 struct Event {
56 enum class Type {
57 Press,
58 Release,
59 AddChar,
60 Reset
61 };
62
63 Type type;
64 double timestamp_ms;
65
67 std::variant<Key, std::string> data;
68 };
69
70 void update();
71
72 void submitKeyDown(Key key, double timestamp);
73 void submitKeyUp(Key key, double timestamp);
74 void submitChar(std::string ch, double timestamp);
75 void submitReset(double timestamp);
76
77 // Convenience getters.
78 bool isKeyDown(Key key) const { return state.keysDown[static_cast<size_t>(key)]; }
79 bool isKeyUp(Key key) const { return !isKeyDown(key); }
80 bool hasChar(std::string_view ch) const { return state.chars.find(ch) != std::string::npos; }
81 bool wasKeyPressed(Key key) const;
82 bool wasKeyReleased(Key key) const;
83
84 // Getters for the full state and events.
86 const State& getState() const { return state; }
88 const std::vector<Event>& getEvents() const { return events; }
89
90 private:
91 State state;
92 std::vector<Event> events;
93
94 std::vector<Event> eventQueue;
95 };
96}
const State & getState() const
Get the previous frame's keyboard state. Updated at the beginning of each new frame.
Definition: Keyboard.h:86
const std::vector< Event > & getEvents() const
Get the previous frame's event list. Updated at the beginning of each new frame.
Definition: Keyboard.h:88
@ Zero
Disable all color writes.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
@ None
Default flags.
std::variant< Key, std::string > data
Data associated with the event: Key for Press and Release events, std::string for AddChar events and ...
Definition: Keyboard.h:67
Type type
Event type.
Definition: Keyboard.h:63
double timestamp_ms
Timestamp in miliseconds.
Definition: Keyboard.h:64
std::string chars
List of characters generated by the keyboard this frame.
Definition: Keyboard.h:52