ui: Speed optimization for browser keyboard inputs

This commit is contained in:
Hajime Hoshi 2017-04-27 11:40:21 +09:00
parent bdb96c2a5c
commit 0dafbfe99d
3 changed files with 278 additions and 129 deletions

View File

@ -32,57 +32,54 @@ import (
) )
var ( var (
codeToName map[string]string nameToCodes map[string][]string
keyCodeToNameSafari map[int]string keyCodeToNameSafari map[int]string
) )
func init() { func init() {
codeToName = map[string]string{ nameToCodes = map[string][]string{
"Comma": "Comma", "Comma": {"Comma"},
"Period": "Period", "Period": {"Period"},
"AltLeft": "Alt", "Alt": {"AltLeft", "AltRight"},
"AltRight": "Alt", "CapsLock": {"CapsLock"},
"CapsLock": "CapsLock", "Control": {"ControlLeft", "ControlRight"},
"ControlLeft": "Control", "Shift": {"ShiftLeft", "ShiftRight"},
"ControlRight": "Control", "Enter": {"Enter"},
"ShiftLeft": "Shift", "Space": {"Space"},
"ShiftRight": "Shift", "Tab": {"Tab"},
"Enter": "Enter", "Delete": {"Delete"},
"Space": "Space", "End": {"End"},
"Tab": "Tab", "Home": {"Home"},
"Delete": "Delete", "Insert": {"Insert"},
"End": "End", "PageDown": {"PageDown"},
"Home": "Home", "PageUp": {"PageUp"},
"Insert": "Insert", "Down": {"ArrowDown"},
"PageDown": "PageDown", "Left": {"ArrowLeft"},
"PageUp": "PageUp", "Right": {"ArrowRight"},
"ArrowDown": "Down", "Up": {"ArrowUp"},
"ArrowLeft": "Left", "Escape": {"Escape"},
"ArrowRight": "Right", "Backspace": {"Backspace"},
"ArrowUp": "Up", "Apostrophe": {"Quote"},
"Escape": "Escape", "Minus": {"Minus"},
"Backspace": "Backspace", "Slash": {"Slash"},
"Quote": "Apostrophe", "Semicolon": {"Semicolon"},
"Minus": "Minus", "Equal": {"Equal"},
"Slash": "Slash", "LeftBracket": {"BracketLeft"},
"Semicolon": "Semicolon", "Backslash": {"Backslash"},
"Equal": "Equal", "RightBracket": {"BracketRight"},
"BracketLeft": "LeftBracket", "GraveAccent": {"Backquote"},
"Backslash": "Backslash",
"BracketRight": "RightBracket",
"Backquote": "GraveAccent",
} }
// ASCII: 0 - 9 // ASCII: 0 - 9
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
codeToName["Digit"+string(c)] = string(c) nameToCodes[string(c)] = []string{"Digit" + string(c)}
} }
// ASCII: A - Z // ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ { for c := 'A'; c <= 'Z'; c++ {
codeToName["Key"+string(c)] = string(c) nameToCodes[string(c)] = []string{"Key" + string(c)}
} }
// Function keys // Function keys
for i := 1; i <= 12; i++ { for i := 1; i <= 12; i++ {
codeToName["F"+strconv.Itoa(i)] = "F" + strconv.Itoa(i) nameToCodes["F"+strconv.Itoa(i)] = []string{"F" + strconv.Itoa(i)}
} }
} }
@ -201,8 +198,10 @@ const uiKeysJSTmpl = `{{.License}}
package ui package ui
var codeToKey = map[string]Key{ var keyToCodes = map[Key][]string{
{{range $code, $name := .CodeToName}}"{{$code}}": Key{{$name}}, {{range $name, $codes := .NameToCodes}}Key{{$name}}: []string{
{{range $code := $codes}}"{{$code}}",{{end}}
},
{{end}} {{end}}
} }
@ -301,9 +300,9 @@ func main() {
namesSet := map[string]struct{}{} namesSet := map[string]struct{}{}
namesWithoutModsSet := map[string]struct{}{} namesWithoutModsSet := map[string]struct{}{}
codes := []string{} codes := []string{}
for code, name := range codeToName { for name, cs := range nameToCodes {
namesSet[name] = struct{}{} namesSet[name] = struct{}{}
codes = append(codes, code) codes = append(codes, cs...)
if name != "Alt" && name != "Control" && name != "Shift" { if name != "Alt" && name != "Control" && name != "Shift" {
namesWithoutModsSet[name] = struct{}{} namesWithoutModsSet[name] = struct{}{}
} }
@ -353,7 +352,7 @@ func main() {
"License": license, "License": license,
"Notice": notice, "Notice": notice,
"BuildTag": buildTag, "BuildTag": buildTag,
"CodeToName": codeToName, "NameToCodes": nameToCodes,
"KeyCodeToNameSafari": keyCodeToNameSafari, "KeyCodeToNameSafari": keyCodeToNameSafari,
"Codes": codes, "Codes": codes,
"KeyNames": names, "KeyNames": names,

View File

@ -37,10 +37,7 @@ func (i *input) IsKeyPressed(key Key) bool {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if i.keyPressed != nil { if i.keyPressed != nil {
for c, k := range codeToKey { for _, c := range keyToCodes[key] {
if k != key {
continue
}
if i.keyPressed[c] { if i.keyPressed[c] {
return true return true
} }

View File

@ -18,88 +18,241 @@
package ui package ui
var codeToKey = map[string]Key{ var keyToCodes = map[Key][]string{
"AltLeft": KeyAlt, Key0: {
"AltRight": KeyAlt, "Digit0",
"ArrowDown": KeyDown, },
"ArrowLeft": KeyLeft, Key1: {
"ArrowRight": KeyRight, "Digit1",
"ArrowUp": KeyUp, },
"Backquote": KeyGraveAccent, Key2: {
"Backslash": KeyBackslash, "Digit2",
"Backspace": KeyBackspace, },
"BracketLeft": KeyLeftBracket, Key3: {
"BracketRight": KeyRightBracket, "Digit3",
"CapsLock": KeyCapsLock, },
"Comma": KeyComma, Key4: {
"ControlLeft": KeyControl, "Digit4",
"ControlRight": KeyControl, },
"Delete": KeyDelete, Key5: {
"Digit0": Key0, "Digit5",
"Digit1": Key1, },
"Digit2": Key2, Key6: {
"Digit3": Key3, "Digit6",
"Digit4": Key4, },
"Digit5": Key5, Key7: {
"Digit6": Key6, "Digit7",
"Digit7": Key7, },
"Digit8": Key8, Key8: {
"Digit9": Key9, "Digit8",
"End": KeyEnd, },
"Enter": KeyEnter, Key9: {
"Equal": KeyEqual, "Digit9",
"Escape": KeyEscape, },
"F1": KeyF1, KeyA: {
"F10": KeyF10, "KeyA",
"F11": KeyF11, },
"F12": KeyF12, KeyAlt: {
"F2": KeyF2, "AltLeft", "AltRight",
"F3": KeyF3, },
"F4": KeyF4, KeyApostrophe: {
"F5": KeyF5, "Quote",
"F6": KeyF6, },
"F7": KeyF7, KeyB: {
"F8": KeyF8, "KeyB",
"F9": KeyF9, },
"Home": KeyHome, KeyBackslash: {
"Insert": KeyInsert, "Backslash",
"KeyA": KeyA, },
"KeyB": KeyB, KeyBackspace: {
"KeyC": KeyC, "Backspace",
"KeyD": KeyD, },
"KeyE": KeyE, KeyC: {
"KeyF": KeyF, "KeyC",
"KeyG": KeyG, },
"KeyH": KeyH, KeyCapsLock: {
"KeyI": KeyI, "CapsLock",
"KeyJ": KeyJ, },
"KeyK": KeyK, KeyComma: {
"KeyL": KeyL, "Comma",
"KeyM": KeyM, },
"KeyN": KeyN, KeyControl: {
"KeyO": KeyO, "ControlLeft", "ControlRight",
"KeyP": KeyP, },
"KeyQ": KeyQ, KeyD: {
"KeyR": KeyR, "KeyD",
"KeyS": KeyS, },
"KeyT": KeyT, KeyDelete: {
"KeyU": KeyU, "Delete",
"KeyV": KeyV, },
"KeyW": KeyW, KeyDown: {
"KeyX": KeyX, "ArrowDown",
"KeyY": KeyY, },
"KeyZ": KeyZ, KeyE: {
"Minus": KeyMinus, "KeyE",
"PageDown": KeyPageDown, },
"PageUp": KeyPageUp, KeyEnd: {
"Period": KeyPeriod, "End",
"Quote": KeyApostrophe, },
"Semicolon": KeySemicolon, KeyEnter: {
"ShiftLeft": KeyShift, "Enter",
"ShiftRight": KeyShift, },
"Slash": KeySlash, KeyEqual: {
"Space": KeySpace, "Equal",
"Tab": KeyTab, },
KeyEscape: {
"Escape",
},
KeyF: {
"KeyF",
},
KeyF1: {
"F1",
},
KeyF10: {
"F10",
},
KeyF11: {
"F11",
},
KeyF12: {
"F12",
},
KeyF2: {
"F2",
},
KeyF3: {
"F3",
},
KeyF4: {
"F4",
},
KeyF5: {
"F5",
},
KeyF6: {
"F6",
},
KeyF7: {
"F7",
},
KeyF8: {
"F8",
},
KeyF9: {
"F9",
},
KeyG: {
"KeyG",
},
KeyGraveAccent: {
"Backquote",
},
KeyH: {
"KeyH",
},
KeyHome: {
"Home",
},
KeyI: {
"KeyI",
},
KeyInsert: {
"Insert",
},
KeyJ: {
"KeyJ",
},
KeyK: {
"KeyK",
},
KeyL: {
"KeyL",
},
KeyLeft: {
"ArrowLeft",
},
KeyLeftBracket: {
"BracketLeft",
},
KeyM: {
"KeyM",
},
KeyMinus: {
"Minus",
},
KeyN: {
"KeyN",
},
KeyO: {
"KeyO",
},
KeyP: {
"KeyP",
},
KeyPageDown: {
"PageDown",
},
KeyPageUp: {
"PageUp",
},
KeyPeriod: {
"Period",
},
KeyQ: {
"KeyQ",
},
KeyR: {
"KeyR",
},
KeyRight: {
"ArrowRight",
},
KeyRightBracket: {
"BracketRight",
},
KeyS: {
"KeyS",
},
KeySemicolon: {
"Semicolon",
},
KeyShift: {
"ShiftLeft", "ShiftRight",
},
KeySlash: {
"Slash",
},
KeySpace: {
"Space",
},
KeyT: {
"KeyT",
},
KeyTab: {
"Tab",
},
KeyU: {
"KeyU",
},
KeyUp: {
"ArrowUp",
},
KeyV: {
"KeyV",
},
KeyW: {
"KeyW",
},
KeyX: {
"KeyX",
},
KeyY: {
"KeyY",
},
KeyZ: {
"KeyZ",
},
} }
var keyCodeToKeySafari = map[int]Key{ var keyCodeToKeySafari = map[int]Key{