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 (
codeToName map[string]string
nameToCodes map[string][]string
keyCodeToNameSafari map[int]string
)
func init() {
codeToName = 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",
"Quote": "Apostrophe",
"Minus": "Minus",
"Slash": "Slash",
"Semicolon": "Semicolon",
"Equal": "Equal",
"BracketLeft": "LeftBracket",
"Backslash": "Backslash",
"BracketRight": "RightBracket",
"Backquote": "GraveAccent",
nameToCodes = map[string][]string{
"Comma": {"Comma"},
"Period": {"Period"},
"Alt": {"AltLeft", "AltRight"},
"CapsLock": {"CapsLock"},
"Control": {"ControlLeft", "ControlRight"},
"Shift": {"ShiftLeft", "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"},
}
// ASCII: 0 - 9
for c := '0'; c <= '9'; c++ {
codeToName["Digit"+string(c)] = string(c)
nameToCodes[string(c)] = []string{"Digit" + string(c)}
}
// ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ {
codeToName["Key"+string(c)] = string(c)
nameToCodes[string(c)] = []string{"Key" + string(c)}
}
// Function keys
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
var codeToKey = map[string]Key{
{{range $code, $name := .CodeToName}}"{{$code}}": Key{{$name}},
var keyToCodes = map[Key][]string{
{{range $name, $codes := .NameToCodes}}Key{{$name}}: []string{
{{range $code := $codes}}"{{$code}}",{{end}}
},
{{end}}
}
@ -301,9 +300,9 @@ func main() {
namesSet := map[string]struct{}{}
namesWithoutModsSet := map[string]struct{}{}
codes := []string{}
for code, name := range codeToName {
for name, cs := range nameToCodes {
namesSet[name] = struct{}{}
codes = append(codes, code)
codes = append(codes, cs...)
if name != "Alt" && name != "Control" && name != "Shift" {
namesWithoutModsSet[name] = struct{}{}
}
@ -353,7 +352,7 @@ func main() {
"License": license,
"Notice": notice,
"BuildTag": buildTag,
"CodeToName": codeToName,
"NameToCodes": nameToCodes,
"KeyCodeToNameSafari": keyCodeToNameSafari,
"Codes": codes,
"KeyNames": names,

View File

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

View File

@ -18,88 +18,241 @@
package ui
var codeToKey = map[string]Key{
"AltLeft": KeyAlt,
"AltRight": KeyAlt,
"ArrowDown": KeyDown,
"ArrowLeft": KeyLeft,
"ArrowRight": KeyRight,
"ArrowUp": KeyUp,
"Backquote": KeyGraveAccent,
"Backslash": KeyBackslash,
"Backspace": KeyBackspace,
"BracketLeft": KeyLeftBracket,
"BracketRight": KeyRightBracket,
"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,
"Equal": KeyEqual,
"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,
"Minus": KeyMinus,
"PageDown": KeyPageDown,
"PageUp": KeyPageUp,
"Period": KeyPeriod,
"Quote": KeyApostrophe,
"Semicolon": KeySemicolon,
"ShiftLeft": KeyShift,
"ShiftRight": KeyShift,
"Slash": KeySlash,
"Space": KeySpace,
"Tab": KeyTab,
var keyToCodes = map[Key][]string{
Key0: {
"Digit0",
},
Key1: {
"Digit1",
},
Key2: {
"Digit2",
},
Key3: {
"Digit3",
},
Key4: {
"Digit4",
},
Key5: {
"Digit5",
},
Key6: {
"Digit6",
},
Key7: {
"Digit7",
},
Key8: {
"Digit8",
},
Key9: {
"Digit9",
},
KeyA: {
"KeyA",
},
KeyAlt: {
"AltLeft", "AltRight",
},
KeyApostrophe: {
"Quote",
},
KeyB: {
"KeyB",
},
KeyBackslash: {
"Backslash",
},
KeyBackspace: {
"Backspace",
},
KeyC: {
"KeyC",
},
KeyCapsLock: {
"CapsLock",
},
KeyComma: {
"Comma",
},
KeyControl: {
"ControlLeft", "ControlRight",
},
KeyD: {
"KeyD",
},
KeyDelete: {
"Delete",
},
KeyDown: {
"ArrowDown",
},
KeyE: {
"KeyE",
},
KeyEnd: {
"End",
},
KeyEnter: {
"Enter",
},
KeyEqual: {
"Equal",
},
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{