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

View File

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

View File

@ -22,316 +22,110 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver"
)
var keyToCodes = map[driver.Key][]string{
driver.Key0: {
"Digit0",
},
driver.Key1: {
"Digit1",
},
driver.Key2: {
"Digit2",
},
driver.Key3: {
"Digit3",
},
driver.Key4: {
"Digit4",
},
driver.Key5: {
"Digit5",
},
driver.Key6: {
"Digit6",
},
driver.Key7: {
"Digit7",
},
driver.Key8: {
"Digit8",
},
driver.Key9: {
"Digit9",
},
driver.KeyA: {
"KeyA",
},
driver.KeyApostrophe: {
"Quote",
},
driver.KeyB: {
"KeyB",
},
driver.KeyBackslash: {
"Backslash",
},
driver.KeyBackspace: {
"Backspace",
},
driver.KeyC: {
"KeyC",
},
driver.KeyCapsLock: {
"CapsLock",
},
driver.KeyComma: {
"Comma",
},
driver.KeyD: {
"KeyD",
},
driver.KeyDelete: {
"Delete",
},
driver.KeyDown: {
"ArrowDown",
},
driver.KeyE: {
"KeyE",
},
driver.KeyEnd: {
"End",
},
driver.KeyEnter: {
"Enter",
},
driver.KeyEqual: {
"Equal",
},
driver.KeyEscape: {
"Escape",
},
driver.KeyF: {
"KeyF",
},
driver.KeyF1: {
"F1",
},
driver.KeyF10: {
"F10",
},
driver.KeyF11: {
"F11",
},
driver.KeyF12: {
"F12",
},
driver.KeyF2: {
"F2",
},
driver.KeyF3: {
"F3",
},
driver.KeyF4: {
"F4",
},
driver.KeyF5: {
"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 keyToCode = map[driver.Key]string{
driver.Key0: "Digit0",
driver.Key1: "Digit1",
driver.Key2: "Digit2",
driver.Key3: "Digit3",
driver.Key4: "Digit4",
driver.Key5: "Digit5",
driver.Key6: "Digit6",
driver.Key7: "Digit7",
driver.Key8: "Digit8",
driver.Key9: "Digit9",
driver.KeyA: "KeyA",
driver.KeyApostrophe: "Quote",
driver.KeyB: "KeyB",
driver.KeyBackslash: "Backslash",
driver.KeyBackspace: "Backspace",
driver.KeyC: "KeyC",
driver.KeyCapsLock: "CapsLock",
driver.KeyComma: "Comma",
driver.KeyD: "KeyD",
driver.KeyDelete: "Delete",
driver.KeyDown: "ArrowDown",
driver.KeyE: "KeyE",
driver.KeyEnd: "End",
driver.KeyEnter: "Enter",
driver.KeyEqual: "Equal",
driver.KeyEscape: "Escape",
driver.KeyF: "KeyF",
driver.KeyF1: "F1",
driver.KeyF10: "F10",
driver.KeyF11: "F11",
driver.KeyF12: "F12",
driver.KeyF2: "F2",
driver.KeyF3: "F3",
driver.KeyF4: "F4",
driver.KeyF5: "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{