|
| 1 | +// |
| 2 | +// Keyboard.swift |
| 3 | +// SDL3 |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 7/7/26. |
| 6 | +// |
| 7 | + |
| 8 | +import CSDL3 |
| 9 | + |
| 10 | +public extension SDL { |
| 11 | + |
| 12 | + /// A snapshot of the current state of the keyboard, indexed by `Scancode.rawValue`. |
| 13 | + static var keyboardState: [Bool] { |
| 14 | + |
| 15 | + var count: Int32 = 0 |
| 16 | + guard let pointer = SDL_GetKeyboardState(&count) else { return [] } |
| 17 | + return (0 ..< Int(count)).map { pointer[$0] } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +public extension Scancode { |
| 22 | + |
| 23 | + /// Get a human-readable name for the scancode. |
| 24 | + /// |
| 25 | + /// - Returns: `nil` if the scancode doesn't have a name. |
| 26 | + var name: String? { |
| 27 | + |
| 28 | + let name = String(cString: SDL_GetScancodeName(SDL_Scancode(rawValue: rawValue))) |
| 29 | + return name.isEmpty ? nil : name |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// MARK: - Named Scancodes |
| 34 | + |
| 35 | +public extension Scancode { |
| 36 | + |
| 37 | + static var a: Scancode { Scancode(rawValue: SDL_SCANCODE_A.rawValue) } |
| 38 | + static var b: Scancode { Scancode(rawValue: SDL_SCANCODE_B.rawValue) } |
| 39 | + static var c: Scancode { Scancode(rawValue: SDL_SCANCODE_C.rawValue) } |
| 40 | + static var d: Scancode { Scancode(rawValue: SDL_SCANCODE_D.rawValue) } |
| 41 | + static var e: Scancode { Scancode(rawValue: SDL_SCANCODE_E.rawValue) } |
| 42 | + static var f: Scancode { Scancode(rawValue: SDL_SCANCODE_F.rawValue) } |
| 43 | + static var g: Scancode { Scancode(rawValue: SDL_SCANCODE_G.rawValue) } |
| 44 | + static var h: Scancode { Scancode(rawValue: SDL_SCANCODE_H.rawValue) } |
| 45 | + static var i: Scancode { Scancode(rawValue: SDL_SCANCODE_I.rawValue) } |
| 46 | + static var j: Scancode { Scancode(rawValue: SDL_SCANCODE_J.rawValue) } |
| 47 | + static var k: Scancode { Scancode(rawValue: SDL_SCANCODE_K.rawValue) } |
| 48 | + static var l: Scancode { Scancode(rawValue: SDL_SCANCODE_L.rawValue) } |
| 49 | + static var m: Scancode { Scancode(rawValue: SDL_SCANCODE_M.rawValue) } |
| 50 | + static var n: Scancode { Scancode(rawValue: SDL_SCANCODE_N.rawValue) } |
| 51 | + static var o: Scancode { Scancode(rawValue: SDL_SCANCODE_O.rawValue) } |
| 52 | + static var p: Scancode { Scancode(rawValue: SDL_SCANCODE_P.rawValue) } |
| 53 | + static var q: Scancode { Scancode(rawValue: SDL_SCANCODE_Q.rawValue) } |
| 54 | + static var r: Scancode { Scancode(rawValue: SDL_SCANCODE_R.rawValue) } |
| 55 | + static var s: Scancode { Scancode(rawValue: SDL_SCANCODE_S.rawValue) } |
| 56 | + static var t: Scancode { Scancode(rawValue: SDL_SCANCODE_T.rawValue) } |
| 57 | + static var u: Scancode { Scancode(rawValue: SDL_SCANCODE_U.rawValue) } |
| 58 | + static var v: Scancode { Scancode(rawValue: SDL_SCANCODE_V.rawValue) } |
| 59 | + static var w: Scancode { Scancode(rawValue: SDL_SCANCODE_W.rawValue) } |
| 60 | + static var x: Scancode { Scancode(rawValue: SDL_SCANCODE_X.rawValue) } |
| 61 | + static var y: Scancode { Scancode(rawValue: SDL_SCANCODE_Y.rawValue) } |
| 62 | + static var z: Scancode { Scancode(rawValue: SDL_SCANCODE_Z.rawValue) } |
| 63 | + |
| 64 | + static var escape: Scancode { Scancode(rawValue: SDL_SCANCODE_ESCAPE.rawValue) } |
| 65 | + static var `return`: Scancode { Scancode(rawValue: SDL_SCANCODE_RETURN.rawValue) } |
| 66 | + static var space: Scancode { Scancode(rawValue: SDL_SCANCODE_SPACE.rawValue) } |
| 67 | + static var tab: Scancode { Scancode(rawValue: SDL_SCANCODE_TAB.rawValue) } |
| 68 | + |
| 69 | + static var up: Scancode { Scancode(rawValue: SDL_SCANCODE_UP.rawValue) } |
| 70 | + static var down: Scancode { Scancode(rawValue: SDL_SCANCODE_DOWN.rawValue) } |
| 71 | + static var left: Scancode { Scancode(rawValue: SDL_SCANCODE_LEFT.rawValue) } |
| 72 | + static var right: Scancode { Scancode(rawValue: SDL_SCANCODE_RIGHT.rawValue) } |
| 73 | + |
| 74 | + static var f8: Scancode { Scancode(rawValue: SDL_SCANCODE_F8.rawValue) } |
| 75 | + static var f9: Scancode { Scancode(rawValue: SDL_SCANCODE_F9.rawValue) } |
| 76 | + static var f10: Scancode { Scancode(rawValue: SDL_SCANCODE_F10.rawValue) } |
| 77 | + |
| 78 | + static var minus: Scancode { Scancode(rawValue: SDL_SCANCODE_MINUS.rawValue) } |
| 79 | + static var equals: Scancode { Scancode(rawValue: SDL_SCANCODE_EQUALS.rawValue) } |
| 80 | + static var leftBracket: Scancode { Scancode(rawValue: SDL_SCANCODE_LEFTBRACKET.rawValue) } |
| 81 | + static var rightBracket: Scancode { Scancode(rawValue: SDL_SCANCODE_RIGHTBRACKET.rawValue) } |
| 82 | + static var backslash: Scancode { Scancode(rawValue: SDL_SCANCODE_BACKSLASH.rawValue) } |
| 83 | + static var semicolon: Scancode { Scancode(rawValue: SDL_SCANCODE_SEMICOLON.rawValue) } |
| 84 | + static var apostrophe: Scancode { Scancode(rawValue: SDL_SCANCODE_APOSTROPHE.rawValue) } |
| 85 | + static var grave: Scancode { Scancode(rawValue: SDL_SCANCODE_GRAVE.rawValue) } |
| 86 | + |
| 87 | + static var keypadDivide: Scancode { Scancode(rawValue: SDL_SCANCODE_KP_DIVIDE.rawValue) } |
| 88 | + static var keypadMultiply: Scancode { Scancode(rawValue: SDL_SCANCODE_KP_MULTIPLY.rawValue) } |
| 89 | + static var keypadMinus: Scancode { Scancode(rawValue: SDL_SCANCODE_KP_MINUS.rawValue) } |
| 90 | + static var keypadPlus: Scancode { Scancode(rawValue: SDL_SCANCODE_KP_PLUS.rawValue) } |
| 91 | + static var keypadEnter: Scancode { Scancode(rawValue: SDL_SCANCODE_KP_ENTER.rawValue) } |
| 92 | + static var keypadPeriod: Scancode { Scancode(rawValue: SDL_SCANCODE_KP_PERIOD.rawValue) } |
| 93 | + |
| 94 | + static var leftAlt: Scancode { Scancode(rawValue: SDL_SCANCODE_LALT.rawValue) } |
| 95 | + static var rightAlt: Scancode { Scancode(rawValue: SDL_SCANCODE_RALT.rawValue) } |
| 96 | + static var leftGUI: Scancode { Scancode(rawValue: SDL_SCANCODE_LGUI.rawValue) } |
| 97 | + static var rightGUI: Scancode { Scancode(rawValue: SDL_SCANCODE_RGUI.rawValue) } |
| 98 | + static var leftControl: Scancode { Scancode(rawValue: SDL_SCANCODE_LCTRL.rawValue) } |
| 99 | + static var rightControl: Scancode { Scancode(rawValue: SDL_SCANCODE_RCTRL.rawValue) } |
| 100 | + static var leftShift: Scancode { Scancode(rawValue: SDL_SCANCODE_LSHIFT.rawValue) } |
| 101 | + static var rightShift: Scancode { Scancode(rawValue: SDL_SCANCODE_RSHIFT.rawValue) } |
| 102 | +} |
0 commit comments