Revert "ui: Use code instead of keyCode on browsers (#339)"

This reverts commit d7ea02d8c1.
This commit is contained in:
Hajime Hoshi 2017-04-10 20:38:23 +09:00
parent d7ea02d8c1
commit aa1e7e9cd6
4 changed files with 113 additions and 127 deletions

View File

@ -31,46 +31,44 @@ import (
"github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal"
) )
var keyCodeToName map[string]string var keyCodeToName map[int]string
func init() { func init() {
keyCodeToName = map[string]string{ keyCodeToName = map[int]string{
"Comma": "Comma", 0xBC: "Comma",
"Period": "Period", 0xBE: "Period",
"AltLeft": "Alt", 0x12: "Alt",
"AltRight": "Alt", 0x14: "CapsLock",
"CapsLock": "CapsLock", 0x11: "Control",
"ControlLeft": "Control", 0x10: "Shift",
"ControlRight": "Control", 0x0D: "Enter",
"ShiftLeft": "Shift", 0x20: "Space",
"ShiftRight": "Shift", 0x09: "Tab",
"Enter": "Enter", 0x2E: "Delete",
"Space": "Space", 0x23: "End",
"Tab": "Tab", 0x24: "Home",
"Delete": "Delete", 0x2D: "Insert",
"End": "End", 0x22: "PageDown",
"Home": "Home", 0x21: "PageUp",
"Insert": "Insert", 0x28: "Down",
"PageDown": "PageDown", 0x25: "Left",
"PageUp": "PageUp", 0x27: "Right",
"ArrowDown": "Down", 0x26: "Up",
"ArrowLeft": "Left", 0x1B: "Escape",
"ArrowRight": "Right", // The keys not listed in the Mozilla site:
"ArrowUp": "Up", 0x08: "Backspace",
"Escape": "Escape",
"Backspace": "Backspace",
} }
// ASCII: 0 - 9 // ASCII: 0 - 9
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
keyCodeToName["Digit"+string(c)] = string(c) keyCodeToName[int(c)] = string(c)
} }
// ASCII: A - Z // ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ { for c := 'A'; c <= 'Z'; c++ {
keyCodeToName["Key"+string(c)] = string(c) keyCodeToName[int(c)] = string(c)
} }
// Function keys // Function keys
for i := 1; i <= 12; i++ { 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 package ui
var keyCodeToKey = map[string]Key{ var keyCodeToKey = map[int]Key{
{{range $code, $name := .KeyCodeToName}}"{{$code}}": Key{{$name}}, {{range $code, $name := .KeyCodeToName}}{{$code}}: Key{{$name}},
{{end}} {{end}}
} }
` `
@ -234,28 +232,19 @@ func main() {
notice := "DO NOT EDIT: This file is auto-generated by genkeys.go." 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{} names := []string{}
namesWithoutMods := []string{} namesWithoutMods := []string{}
for n := range namesSet { codes := []int{}
names = append(names, n) 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(names))
sort.Sort(KeyNames(namesWithoutMods)) sort.Sort(KeyNames(namesWithoutMods))
sort.Strings(codes) sort.Ints(codes)
for path, tmpl := range map[string]string{ for path, tmpl := range map[string]string{
"keys.go": ebitenKeysTmpl, "keys.go": ebitenKeysTmpl,

View File

@ -20,20 +20,20 @@ import (
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
) )
func (i *input) keyDown(code string) { func (i *input) keyDown(key int) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
k, ok := keyCodeToKey[code] k, ok := keyCodeToKey[key]
if !ok { if !ok {
return return
} }
i.keyPressed[k] = true i.keyPressed[k] = true
} }
func (i *input) keyUp(code string) { func (i *input) keyUp(key int) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
k, ok := keyCodeToKey[code] k, ok := keyCodeToKey[key]
if !ok { if !ok {
return return
} }

View File

@ -18,77 +18,74 @@
package ui package ui
var keyCodeToKey = map[string]Key{ var keyCodeToKey = map[int]Key{
"AltLeft": KeyAlt, 8: KeyBackspace,
"AltRight": KeyAlt, 9: KeyTab,
"ArrowDown": KeyDown, 13: KeyEnter,
"ArrowLeft": KeyLeft, 16: KeyShift,
"ArrowRight": KeyRight, 17: KeyControl,
"ArrowUp": KeyUp, 18: KeyAlt,
"Backspace": KeyBackspace, 20: KeyCapsLock,
"CapsLock": KeyCapsLock, 27: KeyEscape,
"Comma": KeyComma, 32: KeySpace,
"ControlLeft": KeyControl, 33: KeyPageUp,
"ControlRight": KeyControl, 34: KeyPageDown,
"Delete": KeyDelete, 35: KeyEnd,
"Digit0": Key0, 36: KeyHome,
"Digit1": Key1, 37: KeyLeft,
"Digit2": Key2, 38: KeyUp,
"Digit3": Key3, 39: KeyRight,
"Digit4": Key4, 40: KeyDown,
"Digit5": Key5, 45: KeyInsert,
"Digit6": Key6, 46: KeyDelete,
"Digit7": Key7, 48: Key0,
"Digit8": Key8, 49: Key1,
"Digit9": Key9, 50: Key2,
"End": KeyEnd, 51: Key3,
"Enter": KeyEnter, 52: Key4,
"Escape": KeyEscape, 53: Key5,
"F1": KeyF1, 54: Key6,
"F10": KeyF10, 55: Key7,
"F11": KeyF11, 56: Key8,
"F12": KeyF12, 57: Key9,
"F2": KeyF2, 65: KeyA,
"F3": KeyF3, 66: KeyB,
"F4": KeyF4, 67: KeyC,
"F5": KeyF5, 68: KeyD,
"F6": KeyF6, 69: KeyE,
"F7": KeyF7, 70: KeyF,
"F8": KeyF8, 71: KeyG,
"F9": KeyF9, 72: KeyH,
"Home": KeyHome, 73: KeyI,
"Insert": KeyInsert, 74: KeyJ,
"KeyA": KeyA, 75: KeyK,
"KeyB": KeyB, 76: KeyL,
"KeyC": KeyC, 77: KeyM,
"KeyD": KeyD, 78: KeyN,
"KeyE": KeyE, 79: KeyO,
"KeyF": KeyF, 80: KeyP,
"KeyG": KeyG, 81: KeyQ,
"KeyH": KeyH, 82: KeyR,
"KeyI": KeyI, 83: KeyS,
"KeyJ": KeyJ, 84: KeyT,
"KeyK": KeyK, 85: KeyU,
"KeyL": KeyL, 86: KeyV,
"KeyM": KeyM, 87: KeyW,
"KeyN": KeyN, 88: KeyX,
"KeyO": KeyO, 89: KeyY,
"KeyP": KeyP, 90: KeyZ,
"KeyQ": KeyQ, 112: KeyF1,
"KeyR": KeyR, 113: KeyF2,
"KeyS": KeyS, 114: KeyF3,
"KeyT": KeyT, 115: KeyF4,
"KeyU": KeyU, 116: KeyF5,
"KeyV": KeyV, 117: KeyF6,
"KeyW": KeyW, 118: KeyF7,
"KeyX": KeyX, 119: KeyF8,
"KeyY": KeyY, 120: KeyF9,
"KeyZ": KeyZ, 121: KeyF10,
"PageDown": KeyPageDown, 122: KeyF11,
"PageUp": KeyPageUp, 123: KeyF12,
"Period": KeyPeriod, 188: KeyComma,
"ShiftLeft": KeyShift, 190: KeyPeriod,
"ShiftRight": KeyShift,
"Space": KeySpace,
"Tab": KeyTab,
} }

View File

@ -183,12 +183,12 @@ func initialize() error {
// Keyboard // Keyboard
canvas.Call("addEventListener", "keydown", func(e *js.Object) { canvas.Call("addEventListener", "keydown", func(e *js.Object) {
e.Call("preventDefault") e.Call("preventDefault")
code := e.Get("code").String() code := e.Get("keyCode").Int()
currentInput.keyDown(code) currentInput.keyDown(code)
}) })
canvas.Call("addEventListener", "keyup", func(e *js.Object) { canvas.Call("addEventListener", "keyup", func(e *js.Object) {
e.Call("preventDefault") e.Call("preventDefault")
code := e.Get("code").String() code := e.Get("keyCode").Int()
currentInput.keyUp(code) currentInput.keyUp(code)
}) })