mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
ui: Use code instead of keyCode on browsers (#339)
This commit is contained in:
parent
12f14e5140
commit
d7ea02d8c1
81
genkeys.go
81
genkeys.go
@ -31,44 +31,46 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal"
|
"github.com/hajimehoshi/ebiten/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
var keyCodeToName map[int]string
|
var keyCodeToName map[string]string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
keyCodeToName = map[int]string{
|
keyCodeToName = map[string]string{
|
||||||
0xBC: "Comma",
|
"Comma": "Comma",
|
||||||
0xBE: "Period",
|
"Period": "Period",
|
||||||
0x12: "Alt",
|
"AltLeft": "Alt",
|
||||||
0x14: "CapsLock",
|
"AltRight": "Alt",
|
||||||
0x11: "Control",
|
"CapsLock": "CapsLock",
|
||||||
0x10: "Shift",
|
"ControlLeft": "Control",
|
||||||
0x0D: "Enter",
|
"ControlRight": "Control",
|
||||||
0x20: "Space",
|
"ShiftLeft": "Shift",
|
||||||
0x09: "Tab",
|
"ShiftRight": "Shift",
|
||||||
0x2E: "Delete",
|
"Enter": "Enter",
|
||||||
0x23: "End",
|
"Space": "Space",
|
||||||
0x24: "Home",
|
"Tab": "Tab",
|
||||||
0x2D: "Insert",
|
"Delete": "Delete",
|
||||||
0x22: "PageDown",
|
"End": "End",
|
||||||
0x21: "PageUp",
|
"Home": "Home",
|
||||||
0x28: "Down",
|
"Insert": "Insert",
|
||||||
0x25: "Left",
|
"PageDown": "PageDown",
|
||||||
0x27: "Right",
|
"PageUp": "PageUp",
|
||||||
0x26: "Up",
|
"ArrowDown": "Down",
|
||||||
0x1B: "Escape",
|
"ArrowLeft": "Left",
|
||||||
// The keys not listed in the Mozilla site:
|
"ArrowRight": "Right",
|
||||||
0x08: "Backspace",
|
"ArrowUp": "Up",
|
||||||
|
"Escape": "Escape",
|
||||||
|
"Backspace": "Backspace",
|
||||||
}
|
}
|
||||||
// ASCII: 0 - 9
|
// ASCII: 0 - 9
|
||||||
for c := '0'; c <= '9'; c++ {
|
for c := '0'; c <= '9'; c++ {
|
||||||
keyCodeToName[int(c)] = string(c)
|
keyCodeToName["Digit"+string(c)] = string(c)
|
||||||
}
|
}
|
||||||
// ASCII: A - Z
|
// ASCII: A - Z
|
||||||
for c := 'A'; c <= 'Z'; c++ {
|
for c := 'A'; c <= 'Z'; c++ {
|
||||||
keyCodeToName[int(c)] = string(c)
|
keyCodeToName["Key"+string(c)] = string(c)
|
||||||
}
|
}
|
||||||
// Function keys
|
// Function keys
|
||||||
for i := 1; i <= 12; i++ {
|
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
|
package ui
|
||||||
|
|
||||||
var keyCodeToKey = map[int]Key{
|
var keyCodeToKey = map[string]Key{
|
||||||
{{range $code, $name := .KeyCodeToName}}{{$code}}: Key{{$name}},
|
{{range $code, $name := .KeyCodeToName}}"{{$code}}": Key{{$name}},
|
||||||
{{end}}
|
{{end}}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
@ -232,19 +234,28 @@ 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."
|
||||||
|
|
||||||
names := []string{}
|
namesSet := map[string]struct{}{}
|
||||||
namesWithoutMods := []string{}
|
namesWithoutModsSet := map[string]struct{}{}
|
||||||
codes := []int{}
|
codes := []string{}
|
||||||
for code, name := range keyCodeToName {
|
for code, name := range keyCodeToName {
|
||||||
names = append(names, name)
|
namesSet[name] = struct{}{}
|
||||||
codes = append(codes, code)
|
codes = append(codes, code)
|
||||||
if name != "Alt" && name != "Control" && name != "Shift" {
|
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(names))
|
||||||
sort.Sort(KeyNames(namesWithoutMods))
|
sort.Sort(KeyNames(namesWithoutMods))
|
||||||
sort.Ints(codes)
|
sort.Strings(codes)
|
||||||
|
|
||||||
for path, tmpl := range map[string]string{
|
for path, tmpl := range map[string]string{
|
||||||
"keys.go": ebitenKeysTmpl,
|
"keys.go": ebitenKeysTmpl,
|
||||||
|
@ -20,20 +20,20 @@ import (
|
|||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (i *input) keyDown(key int) {
|
func (i *input) keyDown(code string) {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
k, ok := keyCodeToKey[key]
|
k, ok := keyCodeToKey[code]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
i.keyPressed[k] = true
|
i.keyPressed[k] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *input) keyUp(key int) {
|
func (i *input) keyUp(code string) {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
k, ok := keyCodeToKey[key]
|
k, ok := keyCodeToKey[code]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -18,74 +18,77 @@
|
|||||||
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
var keyCodeToKey = map[int]Key{
|
var keyCodeToKey = map[string]Key{
|
||||||
8: KeyBackspace,
|
"AltLeft": KeyAlt,
|
||||||
9: KeyTab,
|
"AltRight": KeyAlt,
|
||||||
13: KeyEnter,
|
"ArrowDown": KeyDown,
|
||||||
16: KeyShift,
|
"ArrowLeft": KeyLeft,
|
||||||
17: KeyControl,
|
"ArrowRight": KeyRight,
|
||||||
18: KeyAlt,
|
"ArrowUp": KeyUp,
|
||||||
20: KeyCapsLock,
|
"Backspace": KeyBackspace,
|
||||||
27: KeyEscape,
|
"CapsLock": KeyCapsLock,
|
||||||
32: KeySpace,
|
"Comma": KeyComma,
|
||||||
33: KeyPageUp,
|
"ControlLeft": KeyControl,
|
||||||
34: KeyPageDown,
|
"ControlRight": KeyControl,
|
||||||
35: KeyEnd,
|
"Delete": KeyDelete,
|
||||||
36: KeyHome,
|
"Digit0": Key0,
|
||||||
37: KeyLeft,
|
"Digit1": Key1,
|
||||||
38: KeyUp,
|
"Digit2": Key2,
|
||||||
39: KeyRight,
|
"Digit3": Key3,
|
||||||
40: KeyDown,
|
"Digit4": Key4,
|
||||||
45: KeyInsert,
|
"Digit5": Key5,
|
||||||
46: KeyDelete,
|
"Digit6": Key6,
|
||||||
48: Key0,
|
"Digit7": Key7,
|
||||||
49: Key1,
|
"Digit8": Key8,
|
||||||
50: Key2,
|
"Digit9": Key9,
|
||||||
51: Key3,
|
"End": KeyEnd,
|
||||||
52: Key4,
|
"Enter": KeyEnter,
|
||||||
53: Key5,
|
"Escape": KeyEscape,
|
||||||
54: Key6,
|
"F1": KeyF1,
|
||||||
55: Key7,
|
"F10": KeyF10,
|
||||||
56: Key8,
|
"F11": KeyF11,
|
||||||
57: Key9,
|
"F12": KeyF12,
|
||||||
65: KeyA,
|
"F2": KeyF2,
|
||||||
66: KeyB,
|
"F3": KeyF3,
|
||||||
67: KeyC,
|
"F4": KeyF4,
|
||||||
68: KeyD,
|
"F5": KeyF5,
|
||||||
69: KeyE,
|
"F6": KeyF6,
|
||||||
70: KeyF,
|
"F7": KeyF7,
|
||||||
71: KeyG,
|
"F8": KeyF8,
|
||||||
72: KeyH,
|
"F9": KeyF9,
|
||||||
73: KeyI,
|
"Home": KeyHome,
|
||||||
74: KeyJ,
|
"Insert": KeyInsert,
|
||||||
75: KeyK,
|
"KeyA": KeyA,
|
||||||
76: KeyL,
|
"KeyB": KeyB,
|
||||||
77: KeyM,
|
"KeyC": KeyC,
|
||||||
78: KeyN,
|
"KeyD": KeyD,
|
||||||
79: KeyO,
|
"KeyE": KeyE,
|
||||||
80: KeyP,
|
"KeyF": KeyF,
|
||||||
81: KeyQ,
|
"KeyG": KeyG,
|
||||||
82: KeyR,
|
"KeyH": KeyH,
|
||||||
83: KeyS,
|
"KeyI": KeyI,
|
||||||
84: KeyT,
|
"KeyJ": KeyJ,
|
||||||
85: KeyU,
|
"KeyK": KeyK,
|
||||||
86: KeyV,
|
"KeyL": KeyL,
|
||||||
87: KeyW,
|
"KeyM": KeyM,
|
||||||
88: KeyX,
|
"KeyN": KeyN,
|
||||||
89: KeyY,
|
"KeyO": KeyO,
|
||||||
90: KeyZ,
|
"KeyP": KeyP,
|
||||||
112: KeyF1,
|
"KeyQ": KeyQ,
|
||||||
113: KeyF2,
|
"KeyR": KeyR,
|
||||||
114: KeyF3,
|
"KeyS": KeyS,
|
||||||
115: KeyF4,
|
"KeyT": KeyT,
|
||||||
116: KeyF5,
|
"KeyU": KeyU,
|
||||||
117: KeyF6,
|
"KeyV": KeyV,
|
||||||
118: KeyF7,
|
"KeyW": KeyW,
|
||||||
119: KeyF8,
|
"KeyX": KeyX,
|
||||||
120: KeyF9,
|
"KeyY": KeyY,
|
||||||
121: KeyF10,
|
"KeyZ": KeyZ,
|
||||||
122: KeyF11,
|
"PageDown": KeyPageDown,
|
||||||
123: KeyF12,
|
"PageUp": KeyPageUp,
|
||||||
188: KeyComma,
|
"Period": KeyPeriod,
|
||||||
190: KeyPeriod,
|
"ShiftLeft": KeyShift,
|
||||||
|
"ShiftRight": KeyShift,
|
||||||
|
"Space": KeySpace,
|
||||||
|
"Tab": KeyTab,
|
||||||
}
|
}
|
||||||
|
@ -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("keyCode").Int()
|
code := e.Get("code").String()
|
||||||
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("keyCode").Int()
|
code := e.Get("code").String()
|
||||||
currentInput.keyUp(code)
|
currentInput.keyUp(code)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user