input: Refactoring: nameToJSKeyCodes -> nameToJSKeyCode

This commit is contained in:
Hajime Hoshi 2019-09-02 00:25:39 +09:00
parent 83b2d4b112
commit 05f4ca2a4d
3 changed files with 168 additions and 378 deletions

View File

@ -34,7 +34,7 @@ import (
var ( var (
nameToGLFWKeys map[string]glfw.Key nameToGLFWKeys map[string]glfw.Key
nameToJSKeyCodes map[string][]string nameToJSKeyCode map[string]string
keyCodeToNameEdge map[int]string keyCodeToNameEdge map[int]string
) )
@ -85,68 +85,68 @@ func init() {
"Menu": glfw.KeyMenu, "Menu": glfw.KeyMenu,
"Last": glfw.KeyLast, "Last": glfw.KeyLast,
} }
nameToJSKeyCodes = map[string][]string{ nameToJSKeyCode = map[string]string{
"Comma": {"Comma"}, "Comma": "Comma",
"Period": {"Period"}, "Period": "Period",
"LeftAlt": {"AltLeft"}, "LeftAlt": "AltLeft",
"RightAlt": {"AltRight"}, "RightAlt": "AltRight",
"CapsLock": {"CapsLock"}, "CapsLock": "CapsLock",
"LeftControl": {"ControlLeft"}, "LeftControl": "ControlLeft",
"RightControl": {"ControlRight"}, "RightControl": "ControlRight",
"LeftShift": {"ShiftLeft"}, "LeftShift": "ShiftLeft",
"RightShift": {"ShiftRight"}, "RightShift": "ShiftRight",
"Enter": {"Enter"}, "Enter": "Enter",
"Space": {"Space"}, "Space": "Space",
"Tab": {"Tab"}, "Tab": "Tab",
"Delete": {"Delete"}, "Delete": "Delete",
"End": {"End"}, "End": "End",
"Home": {"Home"}, "Home": "Home",
"Insert": {"Insert"}, "Insert": "Insert",
"PageDown": {"PageDown"}, "PageDown": "PageDown",
"PageUp": {"PageUp"}, "PageUp": "PageUp",
"Down": {"ArrowDown"}, "Down": "ArrowDown",
"Left": {"ArrowLeft"}, "Left": "ArrowLeft",
"Right": {"ArrowRight"}, "Right": "ArrowRight",
"Up": {"ArrowUp"}, "Up": "ArrowUp",
"Escape": {"Escape"}, "Escape": "Escape",
"Backspace": {"Backspace"}, "Backspace": "Backspace",
"Apostrophe": {"Quote"}, "Apostrophe": "Quote",
"Minus": {"Minus"}, "Minus": "Minus",
"Slash": {"Slash"}, "Slash": "Slash",
"Semicolon": {"Semicolon"}, "Semicolon": "Semicolon",
"Equal": {"Equal"}, "Equal": "Equal",
"LeftBracket": {"BracketLeft"}, "LeftBracket": "BracketLeft",
"Backslash": {"Backslash"}, "Backslash": "Backslash",
"RightBracket": {"BracketRight"}, "RightBracket": "BracketRight",
"GraveAccent": {"Backquote"}, "GraveAccent": "Backquote",
"NumLock": {"NumLock"}, "NumLock": "NumLock",
"Pause": {"Pause"}, "Pause": "Pause",
"PrintScreen": {"PrintScreen"}, "PrintScreen": "PrintScreen",
"ScrollLock": {"ScrollLock"}, "ScrollLock": "ScrollLock",
"Menu": {"ContextMenu"}, "Menu": "ContextMenu",
} }
// ASCII: 0 - 9 // ASCII: 0 - 9
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
nameToGLFWKeys[string(c)] = glfw.Key0 + glfw.Key(c) - '0' nameToGLFWKeys[string(c)] = glfw.Key0 + glfw.Key(c) - '0'
nameToJSKeyCodes[string(c)] = []string{"Digit" + string(c)} nameToJSKeyCode[string(c)] = "Digit" + string(c)
} }
// ASCII: A - Z // ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ { for c := 'A'; c <= 'Z'; c++ {
nameToGLFWKeys[string(c)] = glfw.KeyA + glfw.Key(c) - 'A' nameToGLFWKeys[string(c)] = glfw.KeyA + glfw.Key(c) - 'A'
nameToJSKeyCodes[string(c)] = []string{"Key" + string(c)} nameToJSKeyCode[string(c)] = "Key" + string(c)
} }
// Function keys // Function keys
for i := 1; i <= 12; i++ { for i := 1; i <= 12; i++ {
name := "F" + strconv.Itoa(i) name := "F" + strconv.Itoa(i)
nameToGLFWKeys[name] = glfw.KeyF1 + glfw.Key(i) - 1 nameToGLFWKeys[name] = glfw.KeyF1 + glfw.Key(i) - 1
nameToJSKeyCodes[name] = []string{name} nameToJSKeyCode[name] = name
} }
// Numpad // Numpad
// https://www.w3.org/TR/uievents-code/#key-numpad-section // https://www.w3.org/TR/uievents-code/#key-numpad-section
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
name := "KP" + string(c) name := "KP" + string(c)
nameToGLFWKeys[name] = glfw.KeyKP0 + glfw.Key(c) - '0' nameToGLFWKeys[name] = glfw.KeyKP0 + glfw.Key(c) - '0'
nameToJSKeyCodes[name] = []string{"Numpad" + string(c)} nameToJSKeyCode[name] = "Numpad" + string(c)
} }
nameToGLFWKeys["KPDecimal"] = glfw.KeyKPDecimal nameToGLFWKeys["KPDecimal"] = glfw.KeyKPDecimal
@ -157,13 +157,13 @@ func init() {
nameToGLFWKeys["KPEnter"] = glfw.KeyKPEnter nameToGLFWKeys["KPEnter"] = glfw.KeyKPEnter
nameToGLFWKeys["KPEqual"] = glfw.KeyKPEqual nameToGLFWKeys["KPEqual"] = glfw.KeyKPEqual
nameToJSKeyCodes["KPDecimal"] = []string{"NumpadDecimal"} nameToJSKeyCode["KPDecimal"] = "NumpadDecimal"
nameToJSKeyCodes["KPDivide"] = []string{"NumpadDivide"} nameToJSKeyCode["KPDivide"] = "NumpadDivide"
nameToJSKeyCodes["KPMultiply"] = []string{"NumpadMultiply"} nameToJSKeyCode["KPMultiply"] = "NumpadMultiply"
nameToJSKeyCodes["KPSubtract"] = []string{"NumpadSubtract"} nameToJSKeyCode["KPSubtract"] = "NumpadSubtract"
nameToJSKeyCodes["KPAdd"] = []string{"NumpadAdd"} nameToJSKeyCode["KPAdd"] = "NumpadAdd"
nameToJSKeyCodes["KPEnter"] = []string{"NumpadEnter"} nameToJSKeyCode["KPEnter"] = "NumpadEnter"
nameToJSKeyCodes["KPEqual"] = []string{"NumpadEqual"} nameToJSKeyCode["KPEqual"] = "NumpadEqual"
} }
func init() { func init() {
@ -337,10 +337,8 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
var keyToCodes = map[driver.Key][]string{ var keyToCode = map[driver.Key]string{
{{range $name, $codes := .NameToJSKeyCodes}}driver.Key{{$name}}: []string{ {{range $name, $code := .NameToJSKeyCode}}driver.Key{{$name}}: {{$code | printf "%q"}},
{{range $code := $codes}}"{{$code}}",{{end}}
},
{{end}} {{end}}
} }
@ -456,7 +454,7 @@ func main() {
ebitenKeyNames := []string{} ebitenKeyNames := []string{}
ebitenKeyNamesWithoutMods := []string{} ebitenKeyNamesWithoutMods := []string{}
driverKeyNames := []string{} driverKeyNames := []string{}
for name := range nameToJSKeyCodes { for name := range nameToJSKeyCode {
driverKeyNames = append(driverKeyNames, name) driverKeyNames = append(driverKeyNames, name)
if !strings.HasSuffix(name, "Alt") && !strings.HasSuffix(name, "Control") && !strings.HasSuffix(name, "Shift") { if !strings.HasSuffix(name, "Alt") && !strings.HasSuffix(name, "Control") && !strings.HasSuffix(name, "Shift") {
ebitenKeyNames = append(ebitenKeyNames, name) ebitenKeyNames = append(ebitenKeyNames, name)
@ -519,7 +517,7 @@ func main() {
License string License string
DoNotEdit string DoNotEdit string
BuildTag string BuildTag string
NameToJSKeyCodes map[string][]string NameToJSKeyCode map[string]string
KeyCodeToNameEdge map[int]string KeyCodeToNameEdge map[int]string
EbitenKeyNames []string EbitenKeyNames []string
EbitenKeyNamesWithoutMods []string EbitenKeyNamesWithoutMods []string
@ -529,7 +527,7 @@ func main() {
License: license, License: license,
DoNotEdit: doNotEdit, DoNotEdit: doNotEdit,
BuildTag: buildTag, BuildTag: buildTag,
NameToJSKeyCodes: nameToJSKeyCodes, NameToJSKeyCode: nameToJSKeyCode,
KeyCodeToNameEdge: keyCodeToNameEdge, KeyCodeToNameEdge: keyCodeToNameEdge,
EbitenKeyNames: ebitenKeyNames, EbitenKeyNames: ebitenKeyNames,
EbitenKeyNamesWithoutMods: ebitenKeyNamesWithoutMods, EbitenKeyNamesWithoutMods: ebitenKeyNamesWithoutMods,

View File

@ -128,12 +128,10 @@ func (i *Input) ResetForFrame() {
func (i *Input) IsKeyPressed(key driver.Key) bool { func (i *Input) IsKeyPressed(key driver.Key) bool {
if i.keyPressed != nil { if i.keyPressed != nil {
for _, c := range keyToCodes[key] { if i.keyPressed[keyToCode[key]] {
if i.keyPressed[c] {
return true return true
} }
} }
}
if i.keyPressedEdge != nil { if i.keyPressedEdge != nil {
for c, k := range keyCodeToKeyEdge { for c, k := range keyCodeToKeyEdge {
if k != key { if k != key {
@ -275,12 +273,12 @@ func (i *Input) Update(e js.Value) {
return return
} }
cs := c.String() cs := c.String()
if cs == keyToCodes[driver.KeyUp][0] || if cs == keyToCode[driver.KeyUp] ||
cs == keyToCodes[driver.KeyDown][0] || cs == keyToCode[driver.KeyDown] ||
cs == keyToCodes[driver.KeyLeft][0] || cs == keyToCode[driver.KeyLeft] ||
cs == keyToCodes[driver.KeyRight][0] || cs == keyToCode[driver.KeyRight] ||
cs == keyToCodes[driver.KeyBackspace][0] || cs == keyToCode[driver.KeyBackspace] ||
cs == keyToCodes[driver.KeyTab][0] { cs == keyToCode[driver.KeyTab] {
e.Call("preventDefault") e.Call("preventDefault")
} }
i.keyDown(cs) i.keyDown(cs)

View File

@ -22,316 +22,110 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
var keyToCodes = map[driver.Key][]string{ var keyToCode = map[driver.Key]string{
driver.Key0: { driver.Key0: "Digit0",
"Digit0", driver.Key1: "Digit1",
}, driver.Key2: "Digit2",
driver.Key1: { driver.Key3: "Digit3",
"Digit1", driver.Key4: "Digit4",
}, driver.Key5: "Digit5",
driver.Key2: { driver.Key6: "Digit6",
"Digit2", driver.Key7: "Digit7",
}, driver.Key8: "Digit8",
driver.Key3: { driver.Key9: "Digit9",
"Digit3", driver.KeyA: "KeyA",
}, driver.KeyApostrophe: "Quote",
driver.Key4: { driver.KeyB: "KeyB",
"Digit4", driver.KeyBackslash: "Backslash",
}, driver.KeyBackspace: "Backspace",
driver.Key5: { driver.KeyC: "KeyC",
"Digit5", driver.KeyCapsLock: "CapsLock",
}, driver.KeyComma: "Comma",
driver.Key6: { driver.KeyD: "KeyD",
"Digit6", driver.KeyDelete: "Delete",
}, driver.KeyDown: "ArrowDown",
driver.Key7: { driver.KeyE: "KeyE",
"Digit7", driver.KeyEnd: "End",
}, driver.KeyEnter: "Enter",
driver.Key8: { driver.KeyEqual: "Equal",
"Digit8", driver.KeyEscape: "Escape",
}, driver.KeyF: "KeyF",
driver.Key9: { driver.KeyF1: "F1",
"Digit9", driver.KeyF10: "F10",
}, driver.KeyF11: "F11",
driver.KeyA: { driver.KeyF12: "F12",
"KeyA", driver.KeyF2: "F2",
}, driver.KeyF3: "F3",
driver.KeyApostrophe: { driver.KeyF4: "F4",
"Quote", driver.KeyF5: "F5",
}, driver.KeyF6: "F6",
driver.KeyB: { driver.KeyF7: "F7",
"KeyB", driver.KeyF8: "F8",
}, driver.KeyF9: "F9",
driver.KeyBackslash: { driver.KeyG: "KeyG",
"Backslash", driver.KeyGraveAccent: "Backquote",
}, driver.KeyH: "KeyH",
driver.KeyBackspace: { driver.KeyHome: "Home",
"Backspace", driver.KeyI: "KeyI",
}, driver.KeyInsert: "Insert",
driver.KeyC: { driver.KeyJ: "KeyJ",
"KeyC", driver.KeyK: "KeyK",
}, driver.KeyKP0: "Numpad0",
driver.KeyCapsLock: { driver.KeyKP1: "Numpad1",
"CapsLock", driver.KeyKP2: "Numpad2",
}, driver.KeyKP3: "Numpad3",
driver.KeyComma: { driver.KeyKP4: "Numpad4",
"Comma", driver.KeyKP5: "Numpad5",
}, driver.KeyKP6: "Numpad6",
driver.KeyD: { driver.KeyKP7: "Numpad7",
"KeyD", driver.KeyKP8: "Numpad8",
}, driver.KeyKP9: "Numpad9",
driver.KeyDelete: { driver.KeyKPAdd: "NumpadAdd",
"Delete", driver.KeyKPDecimal: "NumpadDecimal",
}, driver.KeyKPDivide: "NumpadDivide",
driver.KeyDown: { driver.KeyKPEnter: "NumpadEnter",
"ArrowDown", driver.KeyKPEqual: "NumpadEqual",
}, driver.KeyKPMultiply: "NumpadMultiply",
driver.KeyE: { driver.KeyKPSubtract: "NumpadSubtract",
"KeyE", driver.KeyL: "KeyL",
}, driver.KeyLeft: "ArrowLeft",
driver.KeyEnd: { driver.KeyLeftAlt: "AltLeft",
"End", driver.KeyLeftBracket: "BracketLeft",
}, driver.KeyLeftControl: "ControlLeft",
driver.KeyEnter: { driver.KeyLeftShift: "ShiftLeft",
"Enter", driver.KeyM: "KeyM",
}, driver.KeyMenu: "ContextMenu",
driver.KeyEqual: { driver.KeyMinus: "Minus",
"Equal", driver.KeyN: "KeyN",
}, driver.KeyNumLock: "NumLock",
driver.KeyEscape: { driver.KeyO: "KeyO",
"Escape", driver.KeyP: "KeyP",
}, driver.KeyPageDown: "PageDown",
driver.KeyF: { driver.KeyPageUp: "PageUp",
"KeyF", driver.KeyPause: "Pause",
}, driver.KeyPeriod: "Period",
driver.KeyF1: { driver.KeyPrintScreen: "PrintScreen",
"F1", driver.KeyQ: "KeyQ",
}, driver.KeyR: "KeyR",
driver.KeyF10: { driver.KeyRight: "ArrowRight",
"F10", driver.KeyRightAlt: "AltRight",
}, driver.KeyRightBracket: "BracketRight",
driver.KeyF11: { driver.KeyRightControl: "ControlRight",
"F11", driver.KeyRightShift: "ShiftRight",
}, driver.KeyS: "KeyS",
driver.KeyF12: { driver.KeyScrollLock: "ScrollLock",
"F12", driver.KeySemicolon: "Semicolon",
}, driver.KeySlash: "Slash",
driver.KeyF2: { driver.KeySpace: "Space",
"F2", driver.KeyT: "KeyT",
}, driver.KeyTab: "Tab",
driver.KeyF3: { driver.KeyU: "KeyU",
"F3", driver.KeyUp: "ArrowUp",
}, driver.KeyV: "KeyV",
driver.KeyF4: { driver.KeyW: "KeyW",
"F4", driver.KeyX: "KeyX",
}, driver.KeyY: "KeyY",
driver.KeyF5: { driver.KeyZ: "KeyZ",
"F5",
},
driver.KeyF6: {
"F6",
},
driver.KeyF7: {
"F7",
},
driver.KeyF8: {
"F8",
},
driver.KeyF9: {
"F9",
},
driver.KeyG: {
"KeyG",
},
driver.KeyGraveAccent: {
"Backquote",
},
driver.KeyH: {
"KeyH",
},
driver.KeyHome: {
"Home",
},
driver.KeyI: {
"KeyI",
},
driver.KeyInsert: {
"Insert",
},
driver.KeyJ: {
"KeyJ",
},
driver.KeyK: {
"KeyK",
},
driver.KeyKP0: {
"Numpad0",
},
driver.KeyKP1: {
"Numpad1",
},
driver.KeyKP2: {
"Numpad2",
},
driver.KeyKP3: {
"Numpad3",
},
driver.KeyKP4: {
"Numpad4",
},
driver.KeyKP5: {
"Numpad5",
},
driver.KeyKP6: {
"Numpad6",
},
driver.KeyKP7: {
"Numpad7",
},
driver.KeyKP8: {
"Numpad8",
},
driver.KeyKP9: {
"Numpad9",
},
driver.KeyKPAdd: {
"NumpadAdd",
},
driver.KeyKPDecimal: {
"NumpadDecimal",
},
driver.KeyKPDivide: {
"NumpadDivide",
},
driver.KeyKPEnter: {
"NumpadEnter",
},
driver.KeyKPEqual: {
"NumpadEqual",
},
driver.KeyKPMultiply: {
"NumpadMultiply",
},
driver.KeyKPSubtract: {
"NumpadSubtract",
},
driver.KeyL: {
"KeyL",
},
driver.KeyLeft: {
"ArrowLeft",
},
driver.KeyLeftAlt: {
"AltLeft",
},
driver.KeyLeftBracket: {
"BracketLeft",
},
driver.KeyLeftControl: {
"ControlLeft",
},
driver.KeyLeftShift: {
"ShiftLeft",
},
driver.KeyM: {
"KeyM",
},
driver.KeyMenu: {
"ContextMenu",
},
driver.KeyMinus: {
"Minus",
},
driver.KeyN: {
"KeyN",
},
driver.KeyNumLock: {
"NumLock",
},
driver.KeyO: {
"KeyO",
},
driver.KeyP: {
"KeyP",
},
driver.KeyPageDown: {
"PageDown",
},
driver.KeyPageUp: {
"PageUp",
},
driver.KeyPause: {
"Pause",
},
driver.KeyPeriod: {
"Period",
},
driver.KeyPrintScreen: {
"PrintScreen",
},
driver.KeyQ: {
"KeyQ",
},
driver.KeyR: {
"KeyR",
},
driver.KeyRight: {
"ArrowRight",
},
driver.KeyRightAlt: {
"AltRight",
},
driver.KeyRightBracket: {
"BracketRight",
},
driver.KeyRightControl: {
"ControlRight",
},
driver.KeyRightShift: {
"ShiftRight",
},
driver.KeyS: {
"KeyS",
},
driver.KeyScrollLock: {
"ScrollLock",
},
driver.KeySemicolon: {
"Semicolon",
},
driver.KeySlash: {
"Slash",
},
driver.KeySpace: {
"Space",
},
driver.KeyT: {
"KeyT",
},
driver.KeyTab: {
"Tab",
},
driver.KeyU: {
"KeyU",
},
driver.KeyUp: {
"ArrowUp",
},
driver.KeyV: {
"KeyV",
},
driver.KeyW: {
"KeyW",
},
driver.KeyX: {
"KeyX",
},
driver.KeyY: {
"KeyY",
},
driver.KeyZ: {
"KeyZ",
},
} }
var keyCodeToKeyEdge = map[int]driver.Key{ var keyCodeToKeyEdge = map[int]driver.Key{