askrobots / network-computer-ios
Public
iPhone client for network-computer: see and drive a remote Linux/Mac desktop over WebRTC, phone as trackpad and keyboard, desktop on a TV via AirPlay.
KeyMap.swift · 2.9 KB · 54 lines
| 1 | import UIKit |
| 2 | |
| 3 | /// Maps what iOS gives us to KeyboardEvent.code names, which is what the host expects. |
| 4 | enum KeyMap { |
| 5 | /// Software keyboard: a typed character -> (code, needsShift). |
| 6 | static func code(for ch: Character) -> (String, Bool)? { |
| 7 | if let a = ch.asciiValue { |
| 8 | switch a { |
| 9 | case 97...122: return ("Key" + String(ch).uppercased(), false) // a-z |
| 10 | case 65...90: return ("Key" + String(ch), true) // A-Z |
| 11 | case 48...57: return ("Digit" + String(ch), false) // 0-9 |
| 12 | case 10, 13: return ("Enter", false) |
| 13 | case 9: return ("Tab", false) |
| 14 | case 32: return ("Space", false) |
| 15 | default: break |
| 16 | } |
| 17 | } |
| 18 | if let c = punctuation[ch] { return c } |
| 19 | return nil |
| 20 | } |
| 21 | |
| 22 | private static let punctuation: [Character: (String, Bool)] = [ |
| 23 | "-": ("Minus", false), "_": ("Minus", true), "=": ("Equal", false), "+": ("Equal", true), |
| 24 | "[": ("BracketLeft", false), "{": ("BracketLeft", true), "]": ("BracketRight", false), "}": ("BracketRight", true), |
| 25 | "\\": ("Backslash", false), "|": ("Backslash", true), ";": ("Semicolon", false), ":": ("Semicolon", true), |
| 26 | "'": ("Quote", false), "\"": ("Quote", true), "`": ("Backquote", false), "~": ("Backquote", true), |
| 27 | ",": ("Comma", false), "<": ("Comma", true), ".": ("Period", false), ">": ("Period", true), |
| 28 | "/": ("Slash", false), "?": ("Slash", true), |
| 29 | "!": ("Digit1", true), "@": ("Digit2", true), "#": ("Digit3", true), "$": ("Digit4", true), "%": ("Digit5", true), |
| 30 | "^": ("Digit6", true), "&": ("Digit7", true), "*": ("Digit8", true), "(": ("Digit9", true), ")": ("Digit0", true), |
| 31 | ] |
| 32 | |
| 33 | /// Hardware keyboard: USB HID usage -> code. Keyed by raw usage id so the |
| 34 | /// table does not depend on UIKeyboardHIDUsage case names. |
| 35 | static func code(forHID usage: Int) -> String? { |
| 36 | switch usage { |
| 37 | case 0x04...0x1d: return "Key" + String(UnicodeScalar(UInt8(65 + usage - 0x04))) |
| 38 | case 0x1e...0x26: return "Digit\(usage - 0x1e + 1)" |
| 39 | case 0x27: return "Digit0" |
| 40 | case 0x3a...0x45: return "F\(usage - 0x3a + 1)" |
| 41 | default: return hid[usage] |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private static let hid: [Int: String] = [ |
| 46 | 0x28: "Enter", 0x29: "Escape", 0x2a: "Backspace", 0x2b: "Tab", 0x2c: "Space", 0x2d: "Minus", 0x2e: "Equal", |
| 47 | 0x2f: "BracketLeft", 0x30: "BracketRight", 0x31: "Backslash", 0x33: "Semicolon", 0x34: "Quote", |
| 48 | 0x35: "Backquote", 0x36: "Comma", 0x37: "Period", 0x38: "Slash", 0x39: "CapsLock", |
| 49 | 0x49: "Insert", 0x4a: "Home", 0x4b: "PageUp", 0x4c: "Delete", 0x4d: "End", 0x4e: "PageDown", |
| 50 | 0x4f: "ArrowRight", 0x50: "ArrowLeft", 0x51: "ArrowDown", 0x52: "ArrowUp", |
| 51 | 0xe0: "ControlLeft", 0xe1: "ShiftLeft", 0xe2: "AltLeft", 0xe3: "MetaLeft", |
| 52 | 0xe4: "ControlRight", 0xe5: "ShiftRight", 0xe6: "AltRight", 0xe7: "MetaRight", |
| 53 | ] |
| 54 | } |