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

This commit is contained in:
Hajime Hoshi 2017-04-10 20:30:28 +09:00
parent 12f14e5140
commit d7ea02d8c1
4 changed files with 125 additions and 111 deletions

View File

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

View File

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

View File

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

View File

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