src/keyboard.c

Wed, 29 Dec 2010 01:38:54 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 29 Dec 2010 01:38:54 +0000
changeset 74
51cbc7a44cd9
child 80
9581358e92b0
child 86
ebce87d87808
permissions
-rw-r--r--

add first cut keyboard driver

philpem@74 1 #include "SDL.h"
philpem@74 2
philpem@74 3 /**
philpem@74 4 * Key map -- a mapping from SDLK_xxx constants to scancodes and vice versa.
philpem@74 5 */
philpem@74 6 struct {
philpem@74 7 SDLKey key; ///< SDLK_xxx key code constant
philpem@74 8 int extended; ///< 1 if this is an extended keycode
philpem@74 9 unsigned char scancode; ///< Keyboard scan code
philpem@74 10 } keymap[] = {
philpem@74 11 { SDLK_UP, 0, 0x01 }, // ROLL/Up [UpArrow]
philpem@74 12 { SDLK_KP2, 0, 0x01 }, // ROLL/Up [Keypad 2]
philpem@74 13 // { SDLK_, 1, 0x02 }, // Clear Line
philpem@74 14 // { SDLK_, 1, 0x03 }, // Rstrt / Ref
philpem@74 15 // { SDLK_, 1, 0x04 }, // Exit
philpem@74 16 { SDLK_KP1, 0, 0x05 }, // PREV [Keypad 1]
philpem@74 17 // { SDLK_, 1, 0x06 }, // Msg
philpem@74 18 // { SDLK_, 1, 0x07 }, // Cancl
philpem@74 19 { SDLK_BACKSPACE, 0, 0x08 }, // Backspace
philpem@74 20 { SDLK_TAB, 0, 0x09 }, // Tab
philpem@74 21 // { SDLK_RETURN, 1, 0x0a }, // ENTER
philpem@74 22 { SDLK_DOWN, 0, 0x0b }, // ROLL/Down [DownArrow]
philpem@74 23 { SDLK_KP0, 0, 0x0b }, // ROLL/Down [Keypad 0]
philpem@74 24 { SDLK_KP3, 0, 0x0c }, // NEXT [Keypad 3]
philpem@74 25 { SDLK_RETURN, 0, 0x0d }, // RETURN [Return]
philpem@74 26 { SDLK_LEFT, 0, 0x0e }, // <-- [LeftArrow]
philpem@74 27 { SDLK_KP_MINUS, 0, 0x0e }, // <-- [Keypad -]
philpem@74 28 { SDLK_RIGHT, 0, 0x0f }, // --> [RightArrow]
philpem@74 29 { SDLK_KP_PERIOD, 0, 0x0f }, // --> [Keypad .]
philpem@74 30 // { SDLK_, 1, 0x10 }, // Creat
philpem@74 31 // { SDLK_, 1, 0x11 }, // Save
philpem@74 32 // { SDLK_, 1, 0x12 }, // Move
philpem@74 33 // { SDLK_, 1, 0x13 }, // Ops
philpem@74 34 // { SDLK_, 1, 0x14 }, // Copy
philpem@74 35 { SDLK_F1, 0, 0x15 }, // F1
philpem@74 36 { SDLK_F2, 0, 0x16 }, // F2
philpem@74 37 { SDLK_F3, 0, 0x17 }, // F3
philpem@74 38 { SDLK_F4, 0, 0x18 }, // F4
philpem@74 39 { SDLK_F5, 0, 0x19 }, // F5
philpem@74 40 { SDLK_F6, 0, 0x1a }, // F6
philpem@74 41 { SDLK_ESCAPE, 0, 0x1b }, // ESC/DEL [Escape]
philpem@74 42 { SDLK_F7, 0, 0x1c }, // F7
philpem@74 43 { SDLK_F8, 0, 0x1d }, // F8
philpem@74 44 // { SDLK_, 1, 0x1e }, // Suspd
philpem@74 45 // { SDLK_, 1, 0x1f }, // Rsume
philpem@74 46 { SDLK_SPACE, 0, 0x20 }, // SPACE [Spacebar]
philpem@74 47 // { SDLK_, 1, 0x21 }, // Undo
philpem@74 48 // { SDLK_, 1, 0x22 }, // Redo
philpem@74 49 // { SDLK_, 1, 0x23 }, // FIND
philpem@74 50 // { SDLK_, 1, 0x24 }, // RPLAC
philpem@74 51 { SDLK_BREAK, 0, 0x25 }, // RESET/BREAK [Pause/Break]
philpem@74 52 // { SDLK_, 1, 0x26 }, // DleteChar
philpem@74 53 { SDLK_QUOTE, 0, 0x27 }, // ' (single-quote)
philpem@74 54 // { SDLK_, 1, 0x28 }, // SLCT/MARK
philpem@74 55 // { SDLK_, 1, 0x29 }, // INPUT/MODE
philpem@74 56 // { SDLK_, 1, 0x2a }, // HELP
philpem@74 57 // Keycode 2B not used
philpem@74 58 { SDLK_COMMA, 0, 0x2c }, // , [Comma]
philpem@74 59 { SDLK_MINUS, 0, 0x2d }, // - [Dash]
philpem@74 60 { SDLK_PERIOD, 0, 0x2e }, // . [Period]
philpem@74 61 { SDLK_SLASH, 0, 0x2f }, // / [Forward-slash]
philpem@74 62 { SDLK_0, 0, 0x30 }, // 0
philpem@74 63 { SDLK_1, 0, 0x31 }, // 1
philpem@74 64 { SDLK_2, 0, 0x32 }, // 2
philpem@74 65 { SDLK_3, 0, 0x33 }, // 3
philpem@74 66 { SDLK_4, 0, 0x34 }, // 4
philpem@74 67 { SDLK_5, 0, 0x35 }, // 5
philpem@74 68 { SDLK_6, 0, 0x36 }, // 6
philpem@74 69 { SDLK_7, 0, 0x37 }, // 7
philpem@74 70 { SDLK_8, 0, 0x38 }, // 8
philpem@74 71 { SDLK_9, 0, 0x39 }, // 9
philpem@74 72 // Keycode 3A not used
philpem@74 73 { SDLK_SEMICOLON, 0, 0x3b }, // ; [Semicolon]
philpem@74 74 // Keycode 3C not used
philpem@74 75 { SDLK_EQUALS, 0, 0x3d }, // = [Equals]
philpem@74 76 // Keycodes 3E, 3F, 40 not used
philpem@74 77 // { SDLK_, 1, 0x41 }, // CMD
philpem@74 78 // { SDLK_, 1, 0x42 }, // CLOSE/OPEN
philpem@74 79 { SDLK_KP7, 0, 0x43 }, // PRINT
philpem@74 80 { SDLK_KP8, 0, 0x44 }, // CLEAR/RFRSH
philpem@74 81 { SDLK_CAPSLOCK, 0, 0x45 }, // Caps Lock
philpem@74 82 { SDLK_KP9, 0, 0x46 }, // PAGE
philpem@74 83 { SDLK_KP4, 0, 0x47 }, // BEG
philpem@74 84 { SDLK_LSHIFT, 0, 0x48 }, // Left Shift
philpem@74 85 { SDLK_RSHIFT, 0, 0x49 }, // Right Shift
philpem@74 86 { SDLK_HOME, 0, 0x4a }, // Home
philpem@74 87 { SDLK_KP5, 0, 0x4a }, // Home [Keypad 5]
philpem@74 88 { SDLK_END, 0, 0x4b }, // End
philpem@74 89 { SDLK_KP6, 0, 0x4b }, // End [Keypad 6]
philpem@74 90 { SDLK_LCTRL, 0, 0x4c }, // Left Ctrl? \___ not sure which is left and which is right...
philpem@74 91 { SDLK_RCTRL, 0, 0x4d }, // Right Ctrl? /
philpem@74 92 // Keycodes 4E thru 5A not used
philpem@74 93 { SDLK_LEFTBRACKET, 0, 0x5b }, // [
philpem@74 94 { SDLK_BACKSLASH, 0, 0x5c }, // \ (backslash)
philpem@74 95 { SDLK_RIGHTBRACKET, 0, 0x5d }, // ]
philpem@74 96 // Keycodes 5E, 5F not used
philpem@74 97 { SDLK_BACKQUOTE, 0, 0x60 }, // `
philpem@74 98 { SDLK_a, 0, 0x61 }, // A
philpem@74 99 { SDLK_b, 0, 0x62 }, // B
philpem@74 100 { SDLK_c, 0, 0x63 }, // C
philpem@74 101 { SDLK_d, 0, 0x64 }, // D
philpem@74 102 { SDLK_e, 0, 0x65 }, // E
philpem@74 103 { SDLK_f, 0, 0x66 }, // F
philpem@74 104 { SDLK_g, 0, 0x67 }, // G
philpem@74 105 { SDLK_h, 0, 0x68 }, // H
philpem@74 106 { SDLK_i, 0, 0x69 }, // I
philpem@74 107 { SDLK_j, 0, 0x6a }, // J
philpem@74 108 { SDLK_k, 0, 0x6b }, // K
philpem@74 109 { SDLK_l, 0, 0x6c }, // L
philpem@74 110 { SDLK_m, 0, 0x6d }, // M
philpem@74 111 { SDLK_n, 0, 0x6e }, // N
philpem@74 112 { SDLK_o, 0, 0x6f }, // O
philpem@74 113 { SDLK_p, 0, 0x70 }, // P
philpem@74 114 { SDLK_q, 0, 0x71 }, // Q
philpem@74 115 { SDLK_r, 0, 0x72 }, // R
philpem@74 116 { SDLK_s, 0, 0x73 }, // S
philpem@74 117 { SDLK_t, 0, 0x74 }, // T
philpem@74 118 { SDLK_u, 0, 0x75 }, // U
philpem@74 119 { SDLK_v, 0, 0x76 }, // V
philpem@74 120 { SDLK_w, 0, 0x77 }, // W
philpem@74 121 { SDLK_x, 0, 0x78 }, // X
philpem@74 122 { SDLK_y, 0, 0x79 }, // Y
philpem@74 123 { SDLK_z, 0, 0x7a }, // Z
philpem@74 124 // Keycodes 7B, 7C, 7D not used
philpem@74 125 { SDLK_NUMLOCK, 0, 0x7e } // Numlock
philpem@74 126 // { SDLK_, 1, 0x7f }, // Dlete
philpem@74 127 };
philpem@74 128
philpem@74 129 /**
philpem@74 130 * List of key states
philpem@74 131 */
philpem@74 132 int keystate[0x80];
philpem@74 133
philpem@74 134 void keyboard_init(void)
philpem@74 135 {
philpem@74 136 // Set all key states to "not pressed"
philpem@74 137 for (int i=0; i<(sizeof(keystate)/sizeof(keystate[0])); i++) {
philpem@74 138 keystate[i] = 0;
philpem@74 139 }
philpem@74 140 }
philpem@74 141
philpem@74 142 void keyboard_event(SDL_Event *ev)
philpem@74 143 {
philpem@74 144 }