Max OS 0.3
Loading...
Searching...
No Matches
keyboard.h
Go to the documentation of this file.
1
9#ifndef MAX_OS_DRIVERS_PERIPHERALS_KEYBOARD_H
10#define MAX_OS_DRIVERS_PERIPHERALS_KEYBOARD_H
11
12#include <common/map.h>
13#include <common/eventHandler.h>
14#include <common/inputStream.h>
15#include <common/string.h>
16#include <common/vector.h>
17#include <drivers/driver.h>
21#include <cstdint>
22
23
24namespace MaxOS::drivers::peripherals {
25
30 enum class ScanCodeType : int {
31 REGULAR,
32 EXTENDED,
33 EXTENDED_BUFFER
34 };
35
40 enum class KeyCode : uint16_t {
41
42 // Alphabet
43 A = 'A', a = 'a',
44 B = 'B', b = 'b',
45 C = 'C', c = 'c',
46 D = 'D', d = 'd',
47 E = 'E', e = 'e',
48 F = 'F', f = 'f',
49 G = 'G', g = 'g',
50 H = 'H', h = 'h',
51 I = 'I', i = 'i',
52 J = 'J', j = 'j',
53 K = 'K', k = 'k',
54 L = 'L', l = 'l',
55 M = 'M', m = 'm',
56 N = 'N', n = 'n',
57 O = 'O', o = 'o',
58 P = 'P', p = 'p',
59 Q = 'Q', q = 'q',
60 R = 'R', r = 'r',
61 S = 'S', s = 's',
62 T = 'T', t = 't',
63 U = 'U', u = 'u',
64 V = 'V', v = 'v',
65 W = 'W', w = 'w',
66 X = 'X', x = 'x',
67 Y = 'Y', y = 'y',
68 Z = 'Z', z = 'z',
69
70 // Numbers
71 zero = '0',
72 one = '1',
73 two = '2',
74 three = '3',
75 four = '4',
76 five = '5',
77 six = '6',
78 seven = '7',
79 eight = '8',
80 nine = '9',
81
82 // Symbols
83 comma = ',',
84 fullStop = '.',
85 exclamationMark = '!',
86 questionMark = '?',
87 quotationMark = '\"',
88 semicolon = ';',
89 colon = ':',
90 apostrophe = '\'',
91 slantedApostrophe = '`',
92
93 // Signs
94 powerSign = '^',
95 dollarSign = '$',
96 percentSign = '%',
97 andSign = '&',
98 atSign = '@',
99
100 // Special Characters
101 underscore = '_',
102 lineThing = '|',
103 hash = '#',
104 backslash = '\\',
105 forwardSlash = '/',
106 squigglyLine = '~',
107
108 // Math Symbols
109 plus = '+',
110 minus = '-',
111 equals = '=',
112 multiply = '*',
113 lessThan = '<',
114 greaterThan = '>',
115
116 // Brackets
117 openBracket = '(',
118 closeBracket = ')',
119 openSquareBracket = '[',
120 closeSquareBracket = ']',
121 openCurlyBracket = '{',
122 closeCurlyBracket = '}',
123
124 // Writing
125 space = ' ',
126 tab = '\t',
127 enter = '\n',
128 backspace = '\b',
129
131
132 // Functions
133 f1 = 1000, //force it to start at 1000 so it doesn't conflict with ascii
134 f2,
135 f3,
136 f4,
137 f5,
138 f6,
139 f7,
140 f8,
141 f9,
142 f10,
143 f11,
144 f12,
145
146 // Top Row
147 escape,
148 printScreen,
149 scrollLock,
150 pauseBreak,
151
152 // Arrow s
153 upArrow,
154 downArrow,
155 leftArrow,
156 rightArrow,
157
158 // Keys Above Arrows
159 insert,
160 home,
161 pageUp,
162 deleteKey,
163 end,
164 pageDown,
165
166 // Left Side
167 capsLock,
168 leftShift,
169 leftControl,
170 leftOS, //weird ass Windows key or command on Mac
171 leftAlt,
172
173 // Right Side
174 rightAlt,
175 functionKey,
176 rightControl,
177 rightShift,
178
179 // Number Pad
180 numberPadLock,
181 numberPadForwardSlash,
182 numberPadMultiply,
183 numberPadMinus,
184 numberPadPlus,
185 numberPadEnter,
186 numberPadZero,
187 numberPadOne,
188 numberPadTwo,
189 numberPadThree,
190 numberPadFour,
191 numberPadFive,
192 numberPadSix,
193 numberPadSeven,
194 numberPadEight,
195 numberPadNine,
196 numberPadFullStop,
197
198 // Number Pad (Non Number Lock)
199 numberPadHome,
200 numberPadPageDown,
201 numberPadPageUp,
202 numberPadEnd,
203 numberPadInsert,
204 numberPadUpArrow,
205 numberPadDownArrow,
206 numberPadLeftArrow,
207 numberPadRightArrow,
208 };
209
217 public:
220
221 // Left and Right
222 bool left_shift = false;
223 bool right_shift = false;
224 bool left_control = false;
225 bool right_control = false;
226 bool left_alt = false;
227 bool right_alt = false;
228
229 // Other Stuff
230 bool caps_lock = false;
231 bool number_pad_lock = false;
232 bool scroll_lock = false;
233 };
234
239 enum class KeyboardEvents {
240 KEYDOWN,
241 KEYUP
242 };
243
248 class KeyUpEvent : public common::Event<KeyboardEvents> {
249 public:
251 ~KeyUpEvent();
252
255 };
256
261 class KeyDownEvent : public common::Event<KeyboardEvents> {
262 public:
265
268 };
269
274 class KeyboardEventHandler : public common::EventHandler<KeyboardEvents> {
275 public:
278
280
281 virtual void on_key_down(KeyCode, const KeyboardState&);
282 virtual void on_key_up(KeyCode, const KeyboardState&);
283 };
284
289 class KeyboardInterpreter : public common::InputStreamEventHandler<uint8_t>, public common::EventManager<KeyboardEvents> {
290
291 protected:
293
297
298 public:
301
302 void on_key_read(bool, const KeyboardState&, KeyCode);
303
304 };
305
311
312 public:
313
318 enum class KeyCodeEN_US {
319 // First Row
320 escape = 0x01,
321 f1 = 0x3B,
322 f2 = 0x3C,
323 f3 = 0x3D,
324 f4 = 0x3E,
325 f5 = 0x3F,
326 f6 = 0x40,
327 f7 = 0x41,
328 f8 = 0x42,
329 f9 = 0x43,
330 f10 = 0x44,
331 f11 = 0x57,
332 f12 = 0x58,
333 printScreen = 0x37,
334 scrollLock = 0x46,
335 pauseBreak = 0x45,
336
337 // Second Row
338 squigglyLine = 0x29,
339 one = 0x02,
340 two = 0x03,
341 three = 0x04,
342 four = 0x05,
343 five = 0x06,
344 six = 0x07,
345 seven = 0x08,
346 eight = 0x09,
347 nine = 0x0A,
348 zero = 0x0B,
349 minus = 0x0C,
350 equals = 0x0D,
351 backspace = 0x0E,
352 insert = 0x52,
353 home = 0x47,
354 pageUp = 0x49,
355 numberPadLock = 0x45,
356 numberPadForwardSlash = 0x35,
357 numberPadMultiply = 0x37,
358 numberPadMinus = 0x4A,
359
360 // Third Row
361 tab = 0x0F,
362 Q = 0x10,
363 W = 0x11,
364 E = 0x12,
365 R = 0x13,
366 T = 0x14,
367 Y = 0x15,
368 U = 0x16,
369 I = 0x17,
370 O = 0x18,
371 P = 0x19,
372 openSquareBracket = 0x1A,
373 closeSquareBracket = 0x1B,
374 backslash = 0x2B,
375 deleteKey = 0x53,
376 end = 0x4F,
377 pageDown = 0x51,
378 numberPadSeven = 0x47,
379 numberPadEight = 0x48,
380 numberPadNine = 0x49,
381 numberPadPlus = 0x4E,
382
383 // Fourth Row
384 capsLock = 0x3A,
385 A = 0x1E,
386 S = 0x1F,
387 D = 0x20,
388 F = 0x21,
389 G = 0x22,
390 H = 0x23,
391 J = 0x24,
392 K = 0x25,
393 L = 0x26,
394 semicolon = 0x27,
395 apostrophe = 0x28,
396 enter = 0x1C,
397 numberPadFour = 0x4B,
398 numberPadFive = 0x4C,
399 numberPadSix = 0x4D,
400
401 // Fifth Row
402 leftShift = 0x2A,
403 Z = 0x2C,
404 X = 0x2D,
405 C = 0x2E,
406 V = 0x2F,
407 B = 0x30,
408 N = 0x31,
409 M = 0x32,
410 comma = 0x33,
411 fullStop = 0x34,
412 forwardSlash = 0x35,
413 rightShift = 0x36,
414 upArrow = 0x48,
415 numberPadOne = 0x4F,
416 numberPadTwo = 0x50,
417 numberPadThree = 0x51,
418 numberPadEnter = 0x1C,
419
420 // Sixth Row
421 leftControl = 0x1D,
422 leftOS = 0x5B,
423 leftAlt = 0x38,
424 space = 0x39,
425 rightAlt = 0x38,
426 function = 0x5D,
427 rightControl = 0x1D,
428 leftArrow = 0x4B,
429 downArrow = 0x50,
430 rightArrow = 0x4D,
431 numberPadZero = 0x52,
432 numberPadFullStop = 0x53
433 };
434
437
438 void on_stream_read(uint8_t scan_code) final;
439
440 };
441
447
448 private:
450 hardwarecommunication::Port8Bit m_command_port;
451
452 public:
455
456 void handle_interrupt() final;
457
458 void activate() final;
459 string device_name() final;
460 };
461}
462
463
464#endif //MAX_OS_DRIVERS_PERIPHERALS_KEYBOARD_H
Defines classes and structures for handling the Advanced Programmable Interrupt Controller (APIC) in ...
Used to handle an event.
Manages the m_handlers for a type of event, raises events and calls the m_handlers.
Used to store information about an event, has a type and a return value.
Manages the connection of a stream to handlers.
Handles read and end of stream events from a set of streams.
Definition inputStream.h:29
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
base class for all drivers, handles the activation, deactivation, initialisation and reset of the dri...
Definition driver.h:27
Event that is triggered when a key is pressed.
Definition keyboard.h:261
KeyCode key_code
The key code of the key that was pressed.
Definition keyboard.h:266
KeyboardState keyboard_state
The state of the keyboard when the key was pressed.
Definition keyboard.h:267
Event that is triggered when a key is released.
Definition keyboard.h:248
KeyboardState keyboard_state
The state of the keyboard when the key was released.
Definition keyboard.h:254
KeyCode key_code
The key code of the key that was released.
Definition keyboard.h:253
Driver for the Keyboard Controller, manages the events and the keyboard state.
Definition keyboard.h:446
void handle_interrupt() final
deactivate the keyboard driver
Definition keyboard.cpp:105
KeyboardDriver()
Construct a new Keyboard Driver object, registering it as an interrupt handler for interrupt 0x21.
Definition keyboard.cpp:67
string device_name() final
Get the device name.
Definition keyboard.cpp:117
void activate() final
activate the keyboard driver
Definition keyboard.cpp:81
Handles the events that are triggered by the Keyboard Driver.
Definition keyboard.h:274
virtual void on_key_up(KeyCode, const KeyboardState &)
Handle the key up event.
Definition keyboard.cpp:36
virtual void on_key_down(KeyCode, const KeyboardState &)
Handle the key down event.
Definition keyboard.cpp:27
common::Event< KeyboardEvents > * on_event(common::Event< KeyboardEvents > *) override
Handle the trigger of an event.
Definition keyboard.cpp:45
Interprets the scan codes from the keyboard for the EN_US keyboard layout.
Definition keyboard.h:310
KeyboardInterpreterEN_US()
Construct a new Keyboard Interpreter for the EN_US layout.
Definition keyboard.cpp:156
KeyCodeEN_US
The scan codes for the EN_US keyboard layout.
Definition keyboard.h:318
void on_stream_read(uint8_t scan_code) final
Handle the key down event.
Definition keyboard.cpp:171
Interprets the scan codes from the keyboard.
Definition keyboard.h:289
KeyboardInterpreter()
Construct a new Keyboard Interpreter object.
Definition keyboard.cpp:128
bool m_next_is_first_extended_code
If true, the next code should be interpreted as an extended code (set when receiving 0xE0)
Definition keyboard.h:294
void on_key_read(bool, const KeyboardState &, KeyCode)
Handle the key event.
Definition keyboard.cpp:143
uint8_t m_extended_scan_code_bytes
How many bytes are left to read for the current extended scan code.
Definition keyboard.h:295
uint16_t m_extended_code_buffer
The storage for scan codes that take more than one byte.
Definition keyboard.h:296
KeyboardState m_keyboard_state
The current state of what special keys are pressed on the keyboard.
Definition keyboard.h:292
Holds the state of the keyboard.
Definition keyboard.h:216
bool left_control
Is the left control key pressed.
Definition keyboard.h:224
bool left_shift
Is the left shift key pressed.
Definition keyboard.h:222
bool right_control
Is the right control key pressed.
Definition keyboard.h:225
bool number_pad_lock
Is number pad lock enabled.
Definition keyboard.h:231
bool scroll_lock
Is scroll lock enabled.
Definition keyboard.h:232
bool caps_lock
Is caps lock enabled.
Definition keyboard.h:230
bool right_alt
Is the right alt key pressed.
Definition keyboard.h:227
bool left_alt
Is the left alt key pressed.
Definition keyboard.h:226
bool right_shift
Is the right shift key pressed.
Definition keyboard.h:223
Handles a certain interrupt number.
Definition interrupts.h:30
Defines a base Driver class and DriverManager for managing drivers.
Defines classes for handling events and event managers.
Defines a generic input stream and related classes for handling input data streams.
Defines a InterruptManager and InterruptHandler for managing hardware and software interrupts.
ScanCodeType
The type of scan code being sent by the keyboard.
Definition keyboard.h:30
KeyCode
A mapping of key codes to their values.
Definition keyboard.h:40
@ f1
OTHER CODES THAT ARE NOT CHARACTERS ///.
KeyboardEvents
The events that can be triggered by the Keyboard Driver.
Definition keyboard.h:239
Defines a Map class for storing key-value pairs.
Defines classes for handling hardware communication ports (8, 16, and 32 bit)
Defines a String class for dynamically sized strings with various operations.
Defines a Vector class for dynamically storing an array of elements.