From aa1e7e9cd619ed587d52c6510367d403860c3f9c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 10 Apr 2017 20:38:23 +0900 Subject: [PATCH] Revert "ui: Use code instead of keyCode on browsers (#339)" This reverts commit d7ea02d8c13cabd16a67d6cb2e7d7dd0aa433bfd. --- genkeys.go | 85 +++++++++++------------- internal/ui/input_js.go | 8 +-- internal/ui/keys_js.go | 143 ++++++++++++++++++++-------------------- internal/ui/ui_js.go | 4 +- 4 files changed, 113 insertions(+), 127 deletions(-) diff --git a/genkeys.go b/genkeys.go index 801c67657..93ac8f015 100644 --- a/genkeys.go +++ b/genkeys.go @@ -31,46 +31,44 @@ import ( "github.com/hajimehoshi/ebiten/internal" ) -var keyCodeToName map[string]string +var keyCodeToName map[int]string func init() { - keyCodeToName = map[string]string{ - "Comma": "Comma", - "Period": "Period", - "AltLeft": "Alt", - "AltRight": "Alt", - "CapsLock": "CapsLock", - "ControlLeft": "Control", - "ControlRight": "Control", - "ShiftLeft": "Shift", - "ShiftRight": "Shift", - "Enter": "Enter", - "Space": "Space", - "Tab": "Tab", - "Delete": "Delete", - "End": "End", - "Home": "Home", - "Insert": "Insert", - "PageDown": "PageDown", - "PageUp": "PageUp", - "ArrowDown": "Down", - "ArrowLeft": "Left", - "ArrowRight": "Right", - "ArrowUp": "Up", - "Escape": "Escape", - "Backspace": "Backspace", + keyCodeToName = map[int]string{ + 0xBC: "Comma", + 0xBE: "Period", + 0x12: "Alt", + 0x14: "CapsLock", + 0x11: "Control", + 0x10: "Shift", + 0x0D: "Enter", + 0x20: "Space", + 0x09: "Tab", + 0x2E: "Delete", + 0x23: "End", + 0x24: "Home", + 0x2D: "Insert", + 0x22: "PageDown", + 0x21: "PageUp", + 0x28: "Down", + 0x25: "Left", + 0x27: "Right", + 0x26: "Up", + 0x1B: "Escape", + // The keys not listed in the Mozilla site: + 0x08: "Backspace", } // ASCII: 0 - 9 for c := '0'; c <= '9'; c++ { - keyCodeToName["Digit"+string(c)] = string(c) + keyCodeToName[int(c)] = string(c) } // ASCII: A - Z for c := 'A'; c <= 'Z'; c++ { - keyCodeToName["Key"+string(c)] = string(c) + keyCodeToName[int(c)] = string(c) } // Function keys for i := 1; i <= 12; i++ { - keyCodeToName["F"+strconv.Itoa(i)] = "F" + strconv.Itoa(i) + keyCodeToName[0x70+i-1] = "F" + strconv.Itoa(i) } } @@ -142,8 +140,8 @@ const uiKeysJSTmpl = `{{.License}} package ui -var keyCodeToKey = map[string]Key{ -{{range $code, $name := .KeyCodeToName}}"{{$code}}": Key{{$name}}, +var keyCodeToKey = map[int]Key{ +{{range $code, $name := .KeyCodeToName}}{{$code}}: Key{{$name}}, {{end}} } ` @@ -234,28 +232,19 @@ func main() { notice := "DO NOT EDIT: This file is auto-generated by genkeys.go." - namesSet := map[string]struct{}{} - namesWithoutModsSet := map[string]struct{}{} - codes := []string{} - for code, name := range keyCodeToName { - namesSet[name] = struct{}{} - codes = append(codes, code) - if name != "Alt" && name != "Control" && name != "Shift" { - namesWithoutModsSet[name] = struct{}{} - } - } names := []string{} namesWithoutMods := []string{} - for n := range namesSet { - names = append(names, n) + codes := []int{} + for code, name := range keyCodeToName { + names = append(names, name) + codes = append(codes, code) + if name != "Alt" && name != "Control" && name != "Shift" { + namesWithoutMods = append(namesWithoutMods, name) + } } - for n := range namesWithoutModsSet { - namesWithoutMods = append(namesWithoutMods, n) - } - sort.Sort(KeyNames(names)) sort.Sort(KeyNames(namesWithoutMods)) - sort.Strings(codes) + sort.Ints(codes) for path, tmpl := range map[string]string{ "keys.go": ebitenKeysTmpl, diff --git a/internal/ui/input_js.go b/internal/ui/input_js.go index 5a8a04ca9..5dcd21da8 100644 --- a/internal/ui/input_js.go +++ b/internal/ui/input_js.go @@ -20,20 +20,20 @@ import ( "github.com/gopherjs/gopherjs/js" ) -func (i *input) keyDown(code string) { +func (i *input) keyDown(key int) { i.m.Lock() defer i.m.Unlock() - k, ok := keyCodeToKey[code] + k, ok := keyCodeToKey[key] if !ok { return } i.keyPressed[k] = true } -func (i *input) keyUp(code string) { +func (i *input) keyUp(key int) { i.m.Lock() defer i.m.Unlock() - k, ok := keyCodeToKey[code] + k, ok := keyCodeToKey[key] if !ok { return } diff --git a/internal/ui/keys_js.go b/internal/ui/keys_js.go index 882bfb120..bbe735a38 100644 --- a/internal/ui/keys_js.go +++ b/internal/ui/keys_js.go @@ -18,77 +18,74 @@ package ui -var keyCodeToKey = map[string]Key{ - "AltLeft": KeyAlt, - "AltRight": KeyAlt, - "ArrowDown": KeyDown, - "ArrowLeft": KeyLeft, - "ArrowRight": KeyRight, - "ArrowUp": KeyUp, - "Backspace": KeyBackspace, - "CapsLock": KeyCapsLock, - "Comma": KeyComma, - "ControlLeft": KeyControl, - "ControlRight": KeyControl, - "Delete": KeyDelete, - "Digit0": Key0, - "Digit1": Key1, - "Digit2": Key2, - "Digit3": Key3, - "Digit4": Key4, - "Digit5": Key5, - "Digit6": Key6, - "Digit7": Key7, - "Digit8": Key8, - "Digit9": Key9, - "End": KeyEnd, - "Enter": KeyEnter, - "Escape": KeyEscape, - "F1": KeyF1, - "F10": KeyF10, - "F11": KeyF11, - "F12": KeyF12, - "F2": KeyF2, - "F3": KeyF3, - "F4": KeyF4, - "F5": KeyF5, - "F6": KeyF6, - "F7": KeyF7, - "F8": KeyF8, - "F9": KeyF9, - "Home": KeyHome, - "Insert": KeyInsert, - "KeyA": KeyA, - "KeyB": KeyB, - "KeyC": KeyC, - "KeyD": KeyD, - "KeyE": KeyE, - "KeyF": KeyF, - "KeyG": KeyG, - "KeyH": KeyH, - "KeyI": KeyI, - "KeyJ": KeyJ, - "KeyK": KeyK, - "KeyL": KeyL, - "KeyM": KeyM, - "KeyN": KeyN, - "KeyO": KeyO, - "KeyP": KeyP, - "KeyQ": KeyQ, - "KeyR": KeyR, - "KeyS": KeyS, - "KeyT": KeyT, - "KeyU": KeyU, - "KeyV": KeyV, - "KeyW": KeyW, - "KeyX": KeyX, - "KeyY": KeyY, - "KeyZ": KeyZ, - "PageDown": KeyPageDown, - "PageUp": KeyPageUp, - "Period": KeyPeriod, - "ShiftLeft": KeyShift, - "ShiftRight": KeyShift, - "Space": KeySpace, - "Tab": KeyTab, +var keyCodeToKey = map[int]Key{ + 8: KeyBackspace, + 9: KeyTab, + 13: KeyEnter, + 16: KeyShift, + 17: KeyControl, + 18: KeyAlt, + 20: KeyCapsLock, + 27: KeyEscape, + 32: KeySpace, + 33: KeyPageUp, + 34: KeyPageDown, + 35: KeyEnd, + 36: KeyHome, + 37: KeyLeft, + 38: KeyUp, + 39: KeyRight, + 40: KeyDown, + 45: KeyInsert, + 46: KeyDelete, + 48: Key0, + 49: Key1, + 50: Key2, + 51: Key3, + 52: Key4, + 53: Key5, + 54: Key6, + 55: Key7, + 56: Key8, + 57: Key9, + 65: KeyA, + 66: KeyB, + 67: KeyC, + 68: KeyD, + 69: KeyE, + 70: KeyF, + 71: KeyG, + 72: KeyH, + 73: KeyI, + 74: KeyJ, + 75: KeyK, + 76: KeyL, + 77: KeyM, + 78: KeyN, + 79: KeyO, + 80: KeyP, + 81: KeyQ, + 82: KeyR, + 83: KeyS, + 84: KeyT, + 85: KeyU, + 86: KeyV, + 87: KeyW, + 88: KeyX, + 89: KeyY, + 90: KeyZ, + 112: KeyF1, + 113: KeyF2, + 114: KeyF3, + 115: KeyF4, + 116: KeyF5, + 117: KeyF6, + 118: KeyF7, + 119: KeyF8, + 120: KeyF9, + 121: KeyF10, + 122: KeyF11, + 123: KeyF12, + 188: KeyComma, + 190: KeyPeriod, } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 385d325a4..137b5e202 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -183,12 +183,12 @@ func initialize() error { // Keyboard canvas.Call("addEventListener", "keydown", func(e *js.Object) { e.Call("preventDefault") - code := e.Get("code").String() + code := e.Get("keyCode").Int() currentInput.keyDown(code) }) canvas.Call("addEventListener", "keyup", func(e *js.Object) { e.Call("preventDefault") - code := e.Get("code").String() + code := e.Get("keyCode").Int() currentInput.keyUp(code) })