KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
keyboard.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/maple/keyboard.h
4 Copyright (C) 2000-2002 Jordan DeLong and Megan Potter
5 Copyright (C) 2012 Lawrence Sebald
6 Copyright (C) 2025 Falco Girgis
7
8*/
9
10/** \file dc/maple/keyboard.h
11 \brief Definitions for using the keyboard device.
12 \ingroup kbd
13
14 This file contains the definitions needed to access the Maple keyboard
15 device. Obviously, this corresponds to the MAPLE_FUNC_KEYBOARD function
16 code.
17
18 \author Jordan DeLong
19 \author Megan Potter
20 \author Lawrence Sebald
21 \author Falco Girgis
22*/
23
24#ifndef __DC_MAPLE_KEYBOARD_H
25#define __DC_MAPLE_KEYBOARD_H
26
27#include <sys/cdefs.h>
28__BEGIN_DECLS
29
30#include <dc/maple.h>
31#include <kos/regfield.h>
32
33#include <stdbool.h>
34#include <stdint.h>
35
36/** \defgroup kbd Keyboard
37 \brief Driver for the Dreamcast's Keyboard Input Device
38 \ingroup peripherals
39*/
40
41/** \defgroup kbd_status_grp Device Status
42 \brief Types relating to overall keyboard state
43 \ingroup kbd
44
45 Types and API functions revolving around individual constituents of
46 the overall keyboard state. These values can either be retrieved manually
47 with \ref kbd_polling.
48
49 @{
50*/
51
52/** \defgroup kbd_mods_grp Modifier Keys
53 \brief Types associated with keyboard modifier keys
54 \ingroup kbd_status_grp
55
56 Modifier keys are represented by the kbd_mods_t union type. Each key state
57 can be accessed by:
58 1. Directly using a convenience bit field.
59 2. Bitwise `AND` of kbd_mods_t::raw with one of the \ref kbd_mods_flags.
60
61 @{
62*/
63
64/** \defgroup kbd_mods_flags Flags
65 \brief Keyboard modifier key flags
66
67 These are the various modifiers that can be pressed on the keyboard. Their
68 current state is stored within kbd_cond_t::modifiers.
69
70 \sa kbd_mods_t::raw
71
72 @{
73*/
74/* Single-Key Modifiers */
75#define KBD_MOD_LCTRL BIT(0) /**< \brief Left Control key */
76#define KBD_MOD_LSHIFT BIT(1) /**< \brief Left Shift key */
77#define KBD_MOD_LALT BIT(2) /**< \brief Left alternate key */
78#define KBD_MOD_S1 BIT(3) /**< \brief S1 key */
79#define KBD_MOD_RCTRL BIT(4) /**< \brief Right Control key */
80#define KBD_MOD_RSHIFT BIT(5) /**< \brief Right Shift key */
81#define KBD_MOD_RALT BIT(6) /**< \brief Right Alternate key */
82#define KBD_MOD_S2 BIT(7) /**< \brief S2 key */
83
84/* Multi-Key Modifiers */
85/** \brief Either Control key */
86#define KBD_MOD_CTRL (KBD_MOD_LCTRL | KBD_MOD_RCTRL)
87/** \brief Either Shift key */
88#define KBD_MOD_SHIFT (KBD_MOD_LSHIFT | KBD_MOD_RSHIFT)
89/** \brief Either Alternate key */
90#define KBD_MOD_ALT (KBD_MOD_LALT | KBD_MOD_RALT)
91/** @} */
92
93/** \brief Modifier Keys
94
95 Convenience union containing the state of all keyboard modifier keys.
96
97 \sa kbd_mods_flags, kbd_state_t::modifiers
98*/
99typedef union kbd_mods {
100 /** \brief Convenience Bitfields */
101 struct {
102 uint8_t lctrl : 1; /**< \brief Left Control key */
103 uint8_t lshift : 1; /**< \brief Left Shift key */
104 uint8_t lalt : 1; /**< \brief Left Alternate key */
105 uint8_t s1 : 1; /**< \brief S1 key */
106 uint8_t rctrl : 1; /**< \brief Right Control key */
107 uint8_t rshift : 1; /**< \brief Right Shift key */
108 uint8_t ralt : 1; /**< \brief Right Alternate key */
109 uint8_t s2 : 1; /**< \brief S2 key */
110 };
111 uint8_t raw; /**< \brief Packed 8-bit unsigned integer of bitflags */
112} kbd_mods_t;
113
114/** @} */
115
116/** \defgroup kbd_leds_grp LEDs
117 \brief Types associated with keyboard LEDs
118 \ingroup kbd
119
120 LEDs are represented by the kbd_leds_t union type. Each individual LED
121 can be accessed by:
122 1. Directly using a convenience bit field.
123 2. Bitwise `AND` of kbd_leds_t::raw with one of the \ref kbd_led_flags.
124
125 @{
126*/
127
128/** \defgroup kbd_led_flags Flags
129 \brief Keyboard LED flags
130
131 These are the LEDs that can be turned on and off on the keyboard. This list
132 may not be exhaustive. Think of these sort of like an extension of the
133 modifiers list.
134
135 \sa kbd_leds_t::raw
136
137 @{
138*/
139#define KBD_LED_NUMLOCK BIT(0) /**< \brief Num Lock LED */
140#define KBD_LED_CAPSLOCK BIT(1) /**< \brief Caps Lock LED */
141#define KBD_LED_SCRLOCK BIT(2) /**< \brief Scroll Lock LED */
142#define KBD_LED_UNKNOWN1 BIT(3) /**< \brief Unknown LED 1 */
143#define KBD_LED_UNKNOWN2 BIT(4) /**< \brief Unknown LED 2 */
144#define KBD_LED_KANA BIT(5) /**< \brief Kana LED */
145#define KBD_LED_POWER BIT(6) /**< \brief Power LED */
146#define KBD_LED_SHIFT BIT(7) /**< \brief Shift LED */
147/** @} */
148
149/** \brief Keyboard LEDs
150
151 Union containing the state of all keyboard LEDs.
152
153 \sa kbd_led_flags, kbd_cond_t::leds
154*/
155typedef union kbd_leds {
156 /** \brief Convenience Bitfields */
157 struct {
158 uint8_t num_lock : 1; /**< \brief Num Lock LED */
159 uint8_t caps_lock : 1; /**< \brief Caps Lock LED */
160 uint8_t scroll_lock : 1; /**< \brief Scroll Lock LED */
161 uint8_t unknown1 : 1; /**< \brief Unknown LED 1 */
162 uint8_t unknown2 : 1; /**< \brief Unknown LED 2 */
163 uint8_t kana : 1; /**< \brief Kana LED */
164 uint8_t power : 1; /**< \brief Power LED */
165 uint8_t shift : 1; /**< \brief Shift LED */
166 };
167 uint8_t raw; /**< \brief Packed 8-bit unsigned integer of bitflags */
168} kbd_leds_t;
169
170/** @} */
171
172/** \brief Region Codes for the Dreamcast keyboard
173
174 This is the list of possible values for kbd_state_t::region.
175*/
176typedef enum kbd_region {
177 KBD_REGION_JP = 1, /**< \brief Japanese keyboard */
178 KBD_REGION_US = 2, /**< \brief US keyboard */
179 KBD_REGION_UK = 3, /**< \brief UK keyboard */
180 KBD_REGION_DE = 4, /**< \brief German keyboard */
181 KBD_REGION_FR = 5, /**< \brief French keyboard */
182 KBD_REGION_IT = 6, /**< \brief Italian keyboard (not supported yet) */
183 KBD_REGION_ES = 7 /**< \brief Spanish keyboard */
185
186/** \brief Raw Keyboard Key Identifiers
187
188 This is the list of keys that are on the keyboard that may be pressed. The
189 keyboard returns keys in this format.
190
191 \note
192 These are the raw keycodes returned by the US keyboard, and thus only cover
193 the keys on US keyboards (even though they can be used with other keyboards).
194*/
195typedef enum __packed kbd_key {
196 KBD_KEY_NONE = 0x00, /**< \brief No key */
197 KBD_KEY_ERROR = 0x01, /**< \brief ERROR_ROLLOVER */
198 KBD_KEY_ERR2 = 0x02, /**< \brief Unknown error */
199 KBD_KEY_ERR3 = 0x03, /**< \brief Unknown error */
200 KBD_KEY_A = 0x04, /**< \brief A key */
201 KBD_KEY_B = 0x05, /**< \brief B key */
202 KBD_KEY_C = 0x06, /**< \brief C key */
203 KBD_KEY_D = 0x07, /**< \brief D key */
204 KBD_KEY_E = 0x08, /**< \brief E key */
205 KBD_KEY_F = 0x09, /**< \brief F key */
206 KBD_KEY_G = 0x0a, /**< \brief G key */
207 KBD_KEY_H = 0x0b, /**< \brief H key */
208 KBD_KEY_I = 0x0c, /**< \brief I key */
209 KBD_KEY_J = 0x0d, /**< \brief J key */
210 KBD_KEY_K = 0x0e, /**< \brief K key */
211 KBD_KEY_L = 0x0f, /**< \brief L key */
212 KBD_KEY_M = 0x10, /**< \brief M key */
213 KBD_KEY_N = 0x11, /**< \brief N key */
214 KBD_KEY_O = 0x12, /**< \brief O key */
215 KBD_KEY_P = 0x13, /**< \brief P key */
216 KBD_KEY_Q = 0x14, /**< \brief Q key */
217 KBD_KEY_R = 0x15, /**< \brief R key */
218 KBD_KEY_S = 0x16, /**< \brief S key */
219 KBD_KEY_T = 0x17, /**< \brief T key */
220 KBD_KEY_U = 0x18, /**< \brief U key */
221 KBD_KEY_V = 0x19, /**< \brief V key */
222 KBD_KEY_W = 0x1a, /**< \brief W key */
223 KBD_KEY_X = 0x1b, /**< \brief X key */
224 KBD_KEY_Y = 0x1c, /**< \brief Y key */
225 KBD_KEY_Z = 0x1d, /**< \brief Z key */
226 KBD_KEY_1 = 0x1e, /**< \brief 1 key */
227 KBD_KEY_2 = 0x1f, /**< \brief 2 key */
228 KBD_KEY_3 = 0x20, /**< \brief 3 key */
229 KBD_KEY_4 = 0x21, /**< \brief 4 key */
230 KBD_KEY_5 = 0x22, /**< \brief 5 key */
231 KBD_KEY_6 = 0x23, /**< \brief 6 key */
232 KBD_KEY_7 = 0x24, /**< \brief 7 key */
233 KBD_KEY_8 = 0x25, /**< \brief 8 key */
234 KBD_KEY_9 = 0x26, /**< \brief 9 key */
235 KBD_KEY_0 = 0x27, /**< \brief 0 key */
236 KBD_KEY_ENTER = 0x28, /**< \brief Enter key */
237 KBD_KEY_ESCAPE = 0x29, /**< \brief Escape key */
238 KBD_KEY_BACKSPACE = 0x2a, /**< \brief Backspace key */
239 KBD_KEY_TAB = 0x2b, /**< \brief Tab key */
240 KBD_KEY_SPACE = 0x2c, /**< \brief Space key */
241 KBD_KEY_MINUS = 0x2d, /**< \brief Minus key */
242 KBD_KEY_PLUS = 0x2e, /**< \brief Plus key */
243 KBD_KEY_LBRACKET = 0x2f, /**< \brief [ key */
244 KBD_KEY_RBRACKET = 0x30, /**< \brief ] key */
245 KBD_KEY_BACKSLASH = 0x31, /**< \brief \ key */
246 KBD_KEY_SEMICOLON = 0x33, /**< \brief ; key */
247 KBD_KEY_QUOTE = 0x34, /**< \brief " key */
248 KBD_KEY_TILDE = 0x35, /**< \brief ~ key */
249 KBD_KEY_COMMA = 0x36, /**< \brief , key */
250 KBD_KEY_PERIOD = 0x37, /**< \brief \. key */
251 KBD_KEY_SLASH = 0x38, /**< \brief Slash key */
252 KBD_KEY_CAPSLOCK = 0x39, /**< \brief Caps Lock key */
253 KBD_KEY_F1 = 0x3a, /**< \brief F1 key */
254 KBD_KEY_F2 = 0x3b, /**< \brief F2 key */
255 KBD_KEY_F3 = 0x3c, /**< \brief F3 key */
256 KBD_KEY_F4 = 0x3d, /**< \brief F4 key */
257 KBD_KEY_F5 = 0x3e, /**< \brief F5 key */
258 KBD_KEY_F6 = 0x3f, /**< \brief F6 key */
259 KBD_KEY_F7 = 0x40, /**< \brief F7 key */
260 KBD_KEY_F8 = 0x41, /**< \brief F8 key */
261 KBD_KEY_F9 = 0x42, /**< \brief F9 key */
262 KBD_KEY_F10 = 0x43, /**< \brief F10 key */
263 KBD_KEY_F11 = 0x44, /**< \brief F11 key */
264 KBD_KEY_F12 = 0x45, /**< \brief F12 key */
265 KBD_KEY_PRINT = 0x46, /**< \brief Print Screen key */
266 KBD_KEY_SCRLOCK = 0x47, /**< \brief Scroll Lock key */
267 KBD_KEY_PAUSE = 0x48, /**< \brief Pause key */
268 KBD_KEY_INSERT = 0x49, /**< \brief Insert key */
269 KBD_KEY_HOME = 0x4a, /**< \brief Home key */
270 KBD_KEY_PGUP = 0x4b, /**< \brief Page Up key */
271 KBD_KEY_DEL = 0x4c, /**< \brief Delete key */
272 KBD_KEY_END = 0x4d, /**< \brief End key */
273 KBD_KEY_PGDOWN = 0x4e, /**< \brief Page Down key */
274 KBD_KEY_RIGHT = 0x4f, /**< \brief Right Arrow key */
275 KBD_KEY_LEFT = 0x50, /**< \brief Left Arrow key */
276 KBD_KEY_DOWN = 0x51, /**< \brief Down Arrow key */
277 KBD_KEY_UP = 0x52, /**< \brief Up Arrow key */
278 KBD_KEY_PAD_NUMLOCK = 0x53, /**< \brief Keypad Numlock key */
279 KBD_KEY_PAD_DIVIDE = 0x54, /**< \brief Keypad Divide key */
280 KBD_KEY_PAD_MULTIPLY = 0x55, /**< \brief Keypad Multiply key */
281 KBD_KEY_PAD_MINUS = 0x56, /**< \brief Keypad Minus key */
282 KBD_KEY_PAD_PLUS = 0x57, /**< \brief Keypad Plus key */
283 KBD_KEY_PAD_ENTER = 0x58, /**< \brief Keypad Enter key */
284 KBD_KEY_PAD_1 = 0x59, /**< \brief Keypad 1 key */
285 KBD_KEY_PAD_2 = 0x5a, /**< \brief Keypad 2 key */
286 KBD_KEY_PAD_3 = 0x5b, /**< \brief Keypad 3 key */
287 KBD_KEY_PAD_4 = 0x5c, /**< \brief Keypad 4 key */
288 KBD_KEY_PAD_5 = 0x5d, /**< \brief Keypad 5 key */
289 KBD_KEY_PAD_6 = 0x5e, /**< \brief Keypad 6 key */
290 KBD_KEY_PAD_7 = 0x5f, /**< \brief Keypad 7 key */
291 KBD_KEY_PAD_8 = 0x60, /**< \brief Keypad 8 key */
292 KBD_KEY_PAD_9 = 0x61, /**< \brief Keypad 9 key */
293 KBD_KEY_PAD_0 = 0x62, /**< \brief Keypad 0 key */
294 KBD_KEY_PAD_PERIOD = 0x63, /**< \brief Keypad Period key */
295 KBD_KEY_S3 = 0x65 /**< \brief S3 key */
297
298/** \brief Converts a kbd_key_t value into its corresponding ASCII value
299
300 This function attempts to convert \p key to its ASCII representation
301 using an internal translation table and additional keyboard state context.
302 To note, this is actually ISO-8859-15 where applicable for non-English
303 regions.
304
305 \param key The raw key type to convert to ASCII.
306 \param region The region type of the keyboard containing the key.
307 \param mods The modifier flags impacting the key.
308 \param leds The LED state flags impacting the key.
309
310 \returns The ASCII value corresponding to \p key or NULL if
311 the translation was unsuccessful.
312*/
314 kbd_mods_t mods, kbd_leds_t leds);
315
316/** \defgroup key_state_grp Key States
317 \brief Types associated with key states
318 \ingroup kbd_status_grp
319
320 Key states are represented by the kbd_state_t union type. The state may be
321 accessed by:
322 1. Directly comparing key_state_t::value to a \ref key_state_value_t.
323 2. Directly using a convenience bit field.
324 3. Bitwise `AND` of key_state_t::raw with one of the
325 \ref key_state_flags.
326
327 @{
328*/
329
330/** \defgroup key_state_flags Flags
331 \brief Keyboard key state bit flags
332
333 A key_state_t is a combination of two flags:
334 1. `KEY_STATE_IS_DOWN`: whether the key is currently pressed
335 this frame.
336 2. `KEY_STATE_WAS_DOWN`: whether the key was previously pressed
337 last frame.
338
339 Between these two flags, you can know whether a key state transition
340 event has occurred (when the two flags have different values).
341
342 \sa key_state_t::raw, key_state_value_t
343
344 @{
345*/
346#define KEY_STATE_IS_DOWN BIT(0) /**< \brief If key is currenty down */
347#define KEY_STATE_WAS_DOWN BIT(1) /**< \brief If key was previously down */
348/** \brief Mask of all key state flags */
349#define KEY_STATE_MASK (KEY_STATE_IS_DOWN | KEY_STATE_WAS_DOWN)
350/** @} */
351
352/** \brief Creates a packed key_state_t
353
354 This macro is used to pack two frames worth of key state information
355 into a key_state_t, one bit per frame.
356*/
357#define KEY_STATE_PACK(is_down, was_down) \
358 (((!!(is_down))? KEY_STATE_IS_DOWN : 0) | \
359 ((!!(was_down))? KEY_STATE_WAS_DOWN : 0))
360
361/** \brief Valid values for key_state_t::value
362
363 Enumerates each of the 4 different states a key can be in,
364 by combining two frames worth of key down information
365 into two bits.
366
367 \note
368 Two of the values are for `HELD` states, meaning the same state has been
369 observed for both the current and the previous frame, while the other two
370 values are for `CHANGE` states, meaning the current frame has a different
371 state from the previous frame.
372
373 \sa key_state_flags, key_state_t::value
374*/
376 /** \brief Key has been in an up state for at least the last two frames */
378 /** \brief Key transitioned from up to pressed this frame */
380 /** \brief Key transitioned from down to released this frame */
382 /** \brief Key has been held down for at least the last two frames */
385
386/* Short-term compatibility helpers. */
387static const uint8_t KEY_STATE_NONE __depr("Please use KEY_STATE_HELD_UP.") = KEY_STATE_HELD_UP;
388static const uint8_t KEY_STATE_WAS_PRESSED __depr("Please use KEY_STATE_CHANGED_UP.") = KEY_STATE_CHANGED_UP;
389static const uint8_t KEY_STATE_PRESSED __depr("Please see key_state_value_t.") = KEY_STATE_HELD_DOWN;
390
391/** \brief Keyboard Key State
392
393 Union containing the the previous and current frames' state information for
394 a keyboard key.
395
396 \sa key_state_flags, key_state_value_t, kbd_state::key_states
397*/
398typedef union key_state {
399 /** \brief Convenience Bitfields */
400 struct {
401 bool is_down : 1; /**< \brief Whether down the current frame */
402 bool was_down : 1; /**< \brief Whether down the previous frame */
403 uint8_t : 6;
404 };
405 key_state_value_t value; /**< \brief Enum for specific state */
406 uint8_t raw; /**< \brief Packed 8-bit unsigned integer of bitflags */
408
409/** @} */
410
411/** \brief Maximum number of keys the DC can read simultaneously.
412
413 This is a hardware constant. The define prevents the magic number '6' from appearing.
414**/
415#define KBD_MAX_PRESSED_KEYS 6
416
417/* Short-term compatibility helper. */
418static const int MAX_PRESSED_KEYS __depr("Please use KBD_MAX_PRESSED_KEYS.") = KBD_MAX_PRESSED_KEYS;
419
420/** \brief Maximum number of keys a DC keyboard can have.
421
422 This is a hardware constant. The define prevents the magic number '256' from appearing.
423**/
424#define KBD_MAX_KEYS 256
425
426/* Short-term compatibility helper. */
427static const int MAX_KBD_KEYS __depr("Please use KBD_MAX_KEYS.") = KBD_MAX_KEYS;
428
429typedef void kbd_keymap_t __depr("Please open an issue, there should be no reason for external code to have used this.");
430
431/** \brief Keyboard raw condition structure.
432
433 This structure is what the keyboard responds with as its current status.
434
435 \headerfile dc/maple/keyboard.h
436*/
437typedef struct {
438 kbd_mods_t modifiers; /**< \brief Bitmask of set modifiers. */
439 kbd_leds_t leds; /**< \brief Bitmask of set LEDs */
440 kbd_key_t keys[KBD_MAX_PRESSED_KEYS]; /**< \brief Key codes for currently pressed keys. */
441} kbd_cond_t;
442
443/** \brief Keyboard status structure.
444
445 This structure holds information about the current status of the keyboard
446 device. This is what maple_dev_status() will return.
447
448 \headerfile dc/maple/keyboard.h
449*/
450typedef struct kbd_state {
451 /** \brief The latest raw condition of the keyboard. */
453
454 /** \brief Current (and previous) state of all keys in kbd_keys_t */
455 union {
456 uint8_t matrix[KBD_MAX_KEYS] __depr("Please see key_state_t and use key_states to access this.");
458 };
459
460 /** \brief Modifier key status. Stored to track changes. */
461 union {
462 int shift_keys __depr("Please see kbd_mods_t and use last_modifiers.raw to access this.");
464 };
465
466 /** \brief Keyboard type/region. */
469
470/** @} */
471
472/** \defgroup kbd_input Querying for Input
473 \brief Various methods for checking keyboard input
474 \ingroup kbd
475
476 There are 3 different ways to check for input with the keyboard API:
477
478 Mechanism | Description
479 ----------------------|--------------------------------------------
480 \ref kbd_polling |Manual checks each key state every frame
481 \ref kbd_event_handler|Automatic callbacks upon key state changes
482 \ref kbd_queue |Monitor for new key press events every frame
483
484 @{
485*/
486
487/** \defgroup kbd_polling State Polling
488 \brief Frame-based polling for keyboard input
489
490 One method of checking for key input is to simply poll
491 kbd_state_t::key_states for the desired key states each frame.
492
493 First, lets grab a pointer to the kbd_state_t:
494 \code{.c}
495 kbd_state_t *kbd = kbd_get_state(device);
496 \endcode
497 Then let's "move" every frame an arrow key is held down:
498
499 if(kbd->key_states[KBD_KEY_LEFT].is_down)
500 printf("Moving left!\n");
501 if(kbd->key_states[KBD_KEY_RIGHT].is_down)
502 printf("Moving right!\n");
503 if(kbd->key_states[KBD_KEY_UP].is_down)
504 printf("Moving up!\n");
505 if(kbd->key_states[KBD_KEY_DOWN].is_down)
506 printf("Moving down!\n");
507
508 Finally, let's "attack" for only a single frame each time the button is
509 pressed, requiring it to be released and pressed again to start the next
510 attack:
511
512 if(kbd->key_states[KBD_KEY_SPACE].value == KEY_STATE_CHANGED_DOWN)
513 printf("Attacking!\n");
514
515 @{
516*/
517
518/** \brief Retrieves the keyboard state from a maple device
519
520 Accessor method for safely retrieving a kbd_state_t from a maple_device_t
521 of a `MAPLE_FUNC_KEYBOARD` type. This function also checks for whether
522 the given device is actually a keyboard and for whether it is currently
523 valid.
524
525 \param device Handle corresponding to a `MAPLE_FUNC_KEYBOARD`
526 device.
527
528 \retval kbd_state_t* A pointer to the internal keyboard state on success.
529 \retval NULL On failure.
530
531*/
533
534/** @} */
535
536/** \defgroup kbd_event_handler Event Handling
537 \brief Event-driven keyboard input mechanism
538
539 One method of checking for key input is to register an event handler,
540 which will be notified every time a key changes state.
541
542 We first create a simple handler to print `ENTER` key events:
543
544 static void on_key_event(maple_device_t *dev, kbd_key_t key,
545 key_state_t state, kbd_mods_t mods,
546 kbd_leds_t leds, void *user_data) {
547
548 if(key == KBD_KEY_ENTER) {
549 if(state.value == KEY_STATE_CHANGED_DOWN)
550 printf("Enter pressed!\n");
551 else if(state.value == KEY_STATE_CHANGED_UP)
552 printf("Enter released!\n");
553 }
554 }
555
556 We then register our callback as the keyboard event handler:
557
558 kbd_set_event_handler(on_key_event, NULL);
559
560 @{
561*/
562
563/** \brief Keyboard Event Handler Callback
564
565 Function type which can be registered to listen to keyboard key events.
566
567 \param dev The maple device the event originated from.
568 \param key The raw key ID whose state change triggered the event.
569 \param state The new (transition) state of the key in question:
570 1. KEY_STATE_CHANGED_DOWN: key pressed event
571 2. KEY_STATE_CHANGED_UP: key released event
572 \param mods Flags containing the states of all modifier keys.
573 \param leds Flags containing the states of all LEDs.
574 \param ud Arbitrary user-pointer which was registered with this handler.
575
576 \sa kbd_set_event_handler
577*/
579 key_state_t state, kbd_mods_t mods,
580 kbd_leds_t leds, void *ud);
581
582/** \brief Registers an Event Handler
583
584 This function registers a function pointer as the keyboard event callback
585 handler, also taking a generic user data pointer which gets passed back
586 to the handler upon invocation.
587
588 The callback will be invoked any time a key state transition occurs on any
589 connected keyboard. That is a key has either gone from released to pressed
590 or from pressed to released.
591
592 \param callback Pointer to the function to be called upon key
593 transition events.
594 \param user_data Generic pointer which is stored internally and gets
595 passed back to \p callback upon an event firing.
596
597 \sa kbd_get_event_handler(), kbd_event_handler_t
598*/
599void kbd_set_event_handler(kbd_event_handler_t callback, void *user_data);
600
601/** \brief Returns the Registered Event Handler
602
603 This function returns the handler that has currently been registered to be
604 notified upon keyboard events. The handler is returned in the form of its
605 callback function and associated userdata.
606
607 \param callback Pointer-to-pointer which will be modifed to point to
608 the address of the currently installed callback.
609 \param user_data Pointer-to-pointer which will be modified to point to
610 the address of the userdata associated with the
611 currently installed callback.
612
613 \sa kbd_set_event_handler(), kbd_event_handler_t
614
615*/
616void kbd_get_event_handler(kbd_event_handler_t *callback, void **user_data);
617
618/** @} */
619
620/** \defgroup kbd_queue Queue Monitoring
621 \brief Monitor queue for key press events.
622
623 \par Popping from the Queue
624 One method of checking for key input is to use the internal key press
625 queue. This is most frequently used when keyboard input is used within
626 a text processing context, which is only concerned with individual key
627 press events, rather than the frame-by-frame state.
628
629 \par
630 We simply pop keys off of the queue in a loop, until the queue is empty:
631 \code{.c}
632 int k;
633
634 while((k = kbd_queue_pop(device, 1)) != KBD_QUEUE_END)
635 printf("Key pressed: %c!\n", (char)k);
636 \endcode
637 \par Repeated Presses
638 As with a text processor, a key which has been held down for a duration of
639 time will generate periodic key press events which will be pushed onto the
640 queue.
641
642 @{
643*/
644
645/** \brief Size of a keyboard queue.
646
647 Each keyboard queue will hold this many elements. Once the queue fills, no
648 new elements will be placed on the queue. As long as you check the queue
649 relatively frequently, the default of 16 should be plenty.
650
651 \note This <strong>MUST</strong> be a power of two.
652*/
653#define KBD_QUEUE_SIZE 16
654
655/** \brief Delimiter value for kbd_queue_pop()
656
657 Value returned from kbd_queue_pop() when there are no more keys in the
658 queue.
659
660 \sa kbd_queue_pop()
661*/
662#define KBD_QUEUE_END -1
663
664/** \brief Configures held key auto-repeat intervals
665
666 This function is used to configure the specific timing behavior for how the
667 internal queue treats a key which is being held down. Giving non-zero values
668 for both parameters will cause the held key to be re-enqueued every
669 \p interval milliseconds after it has been held for the initial \p start time
670 in milliseconds.
671
672 Specifying a value of zero for the two parameters disables this repeating key
673 behavior.
674
675 \note
676 By default, the \p start time is 600ms while the repeating \p interval is 20ms.
677
678 \param start The duration after which the held key starts to
679 register as repeated key presses (or zero to disable
680 this behavior).
681 \param interval The duration between subsequent key repeats after the
682 initial \p start time has elapsed.
683
684 \sa kbd_queue_pop()
685*/
686void kbd_set_repeat_timing(uint16_t start, uint16_t interval);
687
688/** \brief Pop a key off a specific keyboard's queue.
689
690 This function pops the front element off of the specified keyboard queue,
691 and returns the value of that key to the caller.
692
693 If the \p xlat parameter is true and the key represents an ISO-8859-1
694 character, that is the value that will be returned from this function.
695 If the key cannot be converted into a valid ISO-8859-1 character the
696 raw key code, shifted up by 8 bits, will be returned.
697
698 If the \p xlat parameter is false, the lower 8 bits of the returned
699 value will be the raw key code. The next 8 bits will be the modifier
700 keys that were down when the key was pressed (kbd_mods_t). The next
701 8 bits will be the lock key/LED statuses (kbd_leds_t).
702
703 \param dev The keyboard device to read from.
704 \param xlat Set to true to do key translation. Otherwise,
705 you'll simply get the raw key value. Raw key values
706 are not mapped at all, so you are responsible for
707 figuring out what it is by the region.
708
709 \return The value at the front of the queue, or KBD_QUEUE_END
710 if there are no keys in the queue.
711*/
712int kbd_queue_pop(maple_device_t *dev, bool xlat);
713
714/** \brief Activate or deactivate global key queueing.
715 \deprecated
716
717 This function will turn the internal keyboard queueing on or off. Note that
718 there is only one queue for the whole system, no matter how many keyboards
719 are attached, and the queue is of fairly limited length. Turning queueing
720 off is useful (for instance) in a game where individual keypresses don't
721 mean as much as having the keys up or down does.
722
723 You can clear the queue (without popping all the keys off) by setting the
724 active value to a different value than it was.
725
726 The queue is by default on, unless you turn it off.
727
728 \param active Set to non-zero to activate the queue.
729 \note The global queue does not account for non-US
730 keyboard layouts and is deprecated. Please use the
731 individual queues instead for future code.
732*/
734
735/** \brief Pop a key off the global keyboard queue.
736 \deprecated
737
738 This function pops the front off of the keyboard queue, and returns the
739 value to the caller. The value returned will be the ASCII value of the key
740 pressed (accounting for the shift keys being pressed).
741
742 If a key does not have an ASCII value associated with it, the raw key code
743 will be returned, shifted up by 8 bits.
744
745 \return The value at the front of the queue, or KBD_QUEUE_END
746 if there are no keys in the queue or queueing is off.
747 \note This function does not account for non-US keyboard
748 layouts properly (for compatibility with old code),
749 and is deprecated. Use the individual keyboard
750 queues instead to properly account for non-US
751 keyboards.
752 \see kbd_queue_pop()
753*/
755
756/** @} */
757
758/** @} */
759
760/* \cond */
761/* Init / Shutdown */
762void kbd_init(void);
763void kbd_shutdown(void);
764/* \endcond */
765
766__END_DECLS
767
768#endif /* __DC_MAPLE_KEYBOARD_H */
void kbd_get_event_handler(kbd_event_handler_t *callback, void **user_data)
Returns the Registered Event Handler.
void(* kbd_event_handler_t)(maple_device_t *dev, kbd_key_t key, key_state_t state, kbd_mods_t mods, kbd_leds_t leds, void *ud)
Keyboard Event Handler Callback.
Definition keyboard.h:578
void kbd_set_event_handler(kbd_event_handler_t callback, void *user_data)
Registers an Event Handler.
kbd_state_t * kbd_get_state(maple_device_t *device)
Retrieves the keyboard state from a maple device.
void kbd_set_repeat_timing(uint16_t start, uint16_t interval)
Configures held key auto-repeat intervals.
int kbd_queue_pop(maple_device_t *dev, bool xlat)
Pop a key off a specific keyboard's queue.
int kbd_get_key(void) __deprecated
Pop a key off the global keyboard queue.
void kbd_set_queue(int active) __deprecated
Activate or deactivate global key queueing.
kbd_region_t
Region Codes for the Dreamcast keyboard.
Definition keyboard.h:176
static const int MAX_KBD_KEYS("Please use KBD_MAX_KEYS.")
#define KBD_MAX_PRESSED_KEYS
Maximum number of keys the DC can read simultaneously.
Definition keyboard.h:415
char kbd_key_to_ascii(kbd_key_t key, kbd_region_t region, kbd_mods_t mods, kbd_leds_t leds)
Converts a kbd_key_t value into its corresponding ASCII value.
enum __packed kbd_key
Raw Keyboard Key Identifiers.
Definition keyboard.h:195
#define KBD_MAX_KEYS
Maximum number of keys a DC keyboard can have.
Definition keyboard.h:424
enum __packed kbd_key kbd_key_t
Raw Keyboard Key Identifiers.
static const int MAX_PRESSED_KEYS("Please use KBD_MAX_PRESSED_KEYS.")
void kbd_keymap_t("Please open an issue, there should be no reason for external code to have used this.")
Definition keyboard.h:429
@ KBD_REGION_FR
French keyboard.
Definition keyboard.h:181
@ KBD_REGION_US
US keyboard.
Definition keyboard.h:178
@ KBD_REGION_IT
Italian keyboard (not supported yet)
Definition keyboard.h:182
@ KBD_REGION_UK
UK keyboard.
Definition keyboard.h:179
@ KBD_REGION_ES
Spanish keyboard.
Definition keyboard.h:183
@ KBD_REGION_DE
German keyboard.
Definition keyboard.h:180
@ KBD_REGION_JP
Japanese keyboard.
Definition keyboard.h:177
static const uint8_t KEY_STATE_PRESSED("Please see key_state_value_t.")
enum __packed key_state_value key_state_value_t
Valid values for key_state_t::value.
static const uint8_t KEY_STATE_NONE("Please use KEY_STATE_HELD_UP.")
#define KEY_STATE_PACK(is_down, was_down)
Creates a packed key_state_t.
Definition keyboard.h:357
static const uint8_t KEY_STATE_WAS_PRESSED("Please use KEY_STATE_CHANGED_UP.")
enum __packed key_state_value
Valid values for key_state_t::value.
Definition keyboard.h:375
#define __packed
Force a structure, enum, or other type to be packed as small as possible.
Definition cdefs.h:73
#define __deprecated
Mark something as deprecated.
Definition cdefs.h:116
KBD_KEY_R
R key.
Definition keyboard.h:217
KBD_KEY_C
C key.
Definition keyboard.h:202
KBD_KEY_LEFT
Left Arrow key.
Definition keyboard.h:275
KBD_KEY_F9
F9 key.
Definition keyboard.h:261
KBD_KEY_PAD_6
Keypad 6 key.
Definition keyboard.h:289
KBD_KEY_PERIOD
.
Definition keyboard.h:250
KBD_KEY_PLUS
Plus key.
Definition keyboard.h:242
KBD_KEY_F4
F4 key.
Definition keyboard.h:256
KBD_KEY_ERR3
Unknown error.
Definition keyboard.h:199
KBD_KEY_G
G key.
Definition keyboard.h:206
KBD_KEY_2
2 key
Definition keyboard.h:227
KBD_KEY_O
O key.
Definition keyboard.h:214
KBD_KEY_N
N key.
Definition keyboard.h:213
KBD_KEY_QUOTE
" key
Definition keyboard.h:247
KBD_KEY_TAB
Tab key.
Definition keyboard.h:239
KBD_KEY_COMMA
, key
Definition keyboard.h:249
KBD_KEY_DEL
Delete key.
Definition keyboard.h:271
KBD_KEY_PAUSE
Pause key.
Definition keyboard.h:267
KBD_KEY_H
H key.
Definition keyboard.h:207
KEY_STATE_CHANGED_DOWN
Key transitioned from up to pressed this frame.
Definition keyboard.h:379
KBD_KEY_SEMICOLON
; key
Definition keyboard.h:246
KEY_STATE_HELD_DOWN
Key has been held down for at least the last two frames.
Definition keyboard.h:383
KBD_KEY_B
B key.
Definition keyboard.h:201
KEY_STATE_HELD_UP
Key has been in an up state for at least the last two frames.
Definition keyboard.h:377
KBD_KEY_PAD_DIVIDE
Keypad Divide key.
Definition keyboard.h:279
KBD_KEY_8
8 key
Definition keyboard.h:233
KBD_KEY_END
End key.
Definition keyboard.h:272
KBD_KEY_3
3 key
Definition keyboard.h:228
KBD_KEY_0
0 key
Definition keyboard.h:235
KBD_KEY_K
K key.
Definition keyboard.h:210
KBD_KEY_TILDE
~ key
Definition keyboard.h:248
KBD_KEY_F6
F6 key.
Definition keyboard.h:258
KBD_KEY_PAD_1
Keypad 1 key.
Definition keyboard.h:284
KBD_KEY_F10
F10 key.
Definition keyboard.h:262
KBD_KEY_L
L key.
Definition keyboard.h:211
KBD_KEY_ERROR
ERROR_ROLLOVER.
Definition keyboard.h:197
KBD_KEY_I
I key.
Definition keyboard.h:208
KBD_KEY_F12
F12 key.
Definition keyboard.h:264
KBD_KEY_Z
Z key.
Definition keyboard.h:225
KBD_KEY_PAD_9
Keypad 9 key.
Definition keyboard.h:292
KBD_KEY_T
T key.
Definition keyboard.h:219
KBD_KEY_D
D key.
Definition keyboard.h:203
KBD_KEY_PRINT
Print Screen key.
Definition keyboard.h:265
KBD_KEY_PAD_NUMLOCK
Keypad Numlock key.
Definition keyboard.h:278
KBD_KEY_F5
F5 key.
Definition keyboard.h:257
KBD_KEY_SCRLOCK
Scroll Lock key.
Definition keyboard.h:266
KBD_KEY_PAD_MINUS
Keypad Minus key.
Definition keyboard.h:281
KBD_KEY_MINUS
Minus key.
Definition keyboard.h:241
KBD_KEY_ESCAPE
Escape key.
Definition keyboard.h:237
KBD_KEY_PAD_3
Keypad 3 key.
Definition keyboard.h:286
KBD_KEY_BACKSLASH
\ key
Definition keyboard.h:245
KBD_KEY_PGUP
Page Up key.
Definition keyboard.h:270
KBD_KEY_SPACE
Space key.
Definition keyboard.h:240
KBD_KEY_PAD_0
Keypad 0 key.
Definition keyboard.h:293
KEY_STATE_CHANGED_UP
Key transitioned from down to released this frame.
Definition keyboard.h:381
KBD_KEY_F11
F11 key.
Definition keyboard.h:263
KBD_KEY_PAD_5
Keypad 5 key.
Definition keyboard.h:288
KBD_KEY_E
E key.
Definition keyboard.h:204
KBD_KEY_S
S key.
Definition keyboard.h:218
KBD_KEY_PAD_2
Keypad 2 key.
Definition keyboard.h:285
KBD_KEY_6
6 key
Definition keyboard.h:231
KBD_KEY_9
9 key
Definition keyboard.h:234
KBD_KEY_M
M key.
Definition keyboard.h:212
KBD_KEY_RIGHT
Right Arrow key.
Definition keyboard.h:274
KBD_KEY_J
J key.
Definition keyboard.h:209
KBD_KEY_W
W key.
Definition keyboard.h:222
KBD_KEY_PAD_PLUS
Keypad Plus key.
Definition keyboard.h:282
KBD_KEY_PGDOWN
Page Down key.
Definition keyboard.h:273
KBD_KEY_5
5 key
Definition keyboard.h:230
KBD_KEY_NONE
No key.
Definition keyboard.h:196
KBD_KEY_PAD_4
Keypad 4 key.
Definition keyboard.h:287
KBD_KEY_RBRACKET
] key
Definition keyboard.h:244
KBD_KEY_F3
F3 key.
Definition keyboard.h:255
KBD_KEY_F1
F1 key.
Definition keyboard.h:253
KBD_KEY_F
F key.
Definition keyboard.h:205
KBD_KEY_1
1 key
Definition keyboard.h:226
KBD_KEY_PAD_PERIOD
Keypad Period key.
Definition keyboard.h:294
KBD_KEY_A
A key.
Definition keyboard.h:200
KBD_KEY_UP
Up Arrow key.
Definition keyboard.h:277
KBD_KEY_U
U key.
Definition keyboard.h:220
KBD_KEY_PAD_8
Keypad 8 key.
Definition keyboard.h:291
KBD_KEY_4
4 key
Definition keyboard.h:229
KBD_KEY_F2
F2 key.
Definition keyboard.h:254
KBD_KEY_PAD_7
Keypad 7 key.
Definition keyboard.h:290
KBD_KEY_P
P key.
Definition keyboard.h:215
KBD_KEY_HOME
Home key.
Definition keyboard.h:269
KBD_KEY_BACKSPACE
Backspace key.
Definition keyboard.h:238
KBD_KEY_ENTER
Enter key.
Definition keyboard.h:236
KBD_KEY_ERR2
Unknown error.
Definition keyboard.h:198
KBD_KEY_Q
Q key.
Definition keyboard.h:216
KBD_KEY_LBRACKET
[ key
Definition keyboard.h:243
KBD_KEY_DOWN
Down Arrow key.
Definition keyboard.h:276
KBD_KEY_V
V key.
Definition keyboard.h:221
KBD_KEY_7
7 key
Definition keyboard.h:232
KBD_KEY_INSERT
Insert key.
Definition keyboard.h:268
KBD_KEY_PAD_MULTIPLY
Keypad Multiply key.
Definition keyboard.h:280
KBD_KEY_F8
F8 key.
Definition keyboard.h:260
KBD_KEY_Y
Y key.
Definition keyboard.h:224
KBD_KEY_F7
F7 key.
Definition keyboard.h:259
KBD_KEY_X
X key.
Definition keyboard.h:223
KBD_KEY_PAD_ENTER
Keypad Enter key.
Definition keyboard.h:283
KBD_KEY_CAPSLOCK
Caps Lock key.
Definition keyboard.h:252
KBD_KEY_SLASH
Slash key.
Definition keyboard.h:251
Maple Bus driver interface.
Macros to help dealing with register fields.
Keyboard raw condition structure.
Definition keyboard.h:437
kbd_mods_t modifiers
Bitmask of set modifiers.
Definition keyboard.h:438
kbd_leds_t leds
Bitmask of set LEDs.
Definition keyboard.h:439
Keyboard status structure.
Definition keyboard.h:450
kbd_mods_t last_modifiers
Definition keyboard.h:463
kbd_cond_t cond
The latest raw condition of the keyboard.
Definition keyboard.h:452
kbd_region_t region
Keyboard type/region.
Definition keyboard.h:467
One maple device.
Definition maple.h:271
Keyboard LEDs.
Definition keyboard.h:155
uint8_t raw
Packed 8-bit unsigned integer of bitflags.
Definition keyboard.h:167
uint8_t kana
Kana LED.
Definition keyboard.h:163
uint8_t caps_lock
Caps Lock LED.
Definition keyboard.h:159
uint8_t scroll_lock
Scroll Lock LED.
Definition keyboard.h:160
uint8_t power
Power LED.
Definition keyboard.h:164
uint8_t unknown2
Unknown LED 2.
Definition keyboard.h:162
uint8_t num_lock
Num Lock LED.
Definition keyboard.h:158
uint8_t unknown1
Unknown LED 1.
Definition keyboard.h:161
uint8_t shift
Shift LED.
Definition keyboard.h:165
Modifier Keys.
Definition keyboard.h:99
uint8_t rshift
Right Shift key.
Definition keyboard.h:107
uint8_t lctrl
Left Control key.
Definition keyboard.h:102
uint8_t lalt
Left Alternate key.
Definition keyboard.h:104
uint8_t lshift
Left Shift key.
Definition keyboard.h:103
uint8_t s2
S2 key.
Definition keyboard.h:109
uint8_t s1
S1 key.
Definition keyboard.h:105
uint8_t rctrl
Right Control key.
Definition keyboard.h:106
uint8_t ralt
Right Alternate key.
Definition keyboard.h:108
uint8_t raw
Packed 8-bit unsigned integer of bitflags.
Definition keyboard.h:111
Keyboard Key State.
Definition keyboard.h:398
uint8_t raw
Packed 8-bit unsigned integer of bitflags.
Definition keyboard.h:406
bool is_down
Whether down the current frame.
Definition keyboard.h:401
key_state_value_t value
Enum for specific state.
Definition keyboard.h:405
bool was_down
Whether down the previous frame.
Definition keyboard.h:402