uidriver/js: Use js.Value as key strings

Updates #1437
This commit is contained in:
Hajime Hoshi 2020-12-16 12:51:52 +09:00
parent 442be20f4d
commit 69b1d2e820
3 changed files with 125 additions and 123 deletions

View File

@ -503,11 +503,13 @@ const uidriverJsKeysTmpl = `{{.License}}
package js package js
import ( import (
"syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
) )
var driverKeyToJSKey = map[driver.Key]string{ var driverKeyToJSKey = map[driver.Key]js.Value{
{{range $name, $code := .DriverKeyNameToJSKey}}driver.Key{{$name}}: {{$code | printf "%q"}}, {{range $name, $code := .DriverKeyNameToJSKey}}driver.Key{{$name}}: js.ValueOf({{$code | printf "%q"}}),
{{end}} {{end}}
} }

View File

@ -168,7 +168,7 @@ func (i *Input) resetForFrame() {
func (i *Input) IsKeyPressed(key driver.Key) bool { func (i *Input) IsKeyPressed(key driver.Key) bool {
if i.keyPressed != nil { if i.keyPressed != nil {
if i.keyPressed[driverKeyToJSKey[key]] { if i.keyPressed[driverKeyToJSKey[key].String()] {
return true return true
} }
} }
@ -210,18 +210,18 @@ func (i *Input) Wheel() (xoff, yoff float64) {
return i.wheelX, i.wheelY return i.wheelX, i.wheelY
} }
func (i *Input) keyDown(code string) { func (i *Input) keyDown(code js.Value) {
if i.keyPressed == nil { if i.keyPressed == nil {
i.keyPressed = map[string]bool{} i.keyPressed = map[string]bool{}
} }
i.keyPressed[code] = true i.keyPressed[code.String()] = true
} }
func (i *Input) keyUp(code string) { func (i *Input) keyUp(code js.Value) {
if i.keyPressed == nil { if i.keyPressed == nil {
i.keyPressed = map[string]bool{} i.keyPressed = map[string]bool{}
} }
i.keyPressed[code] = false i.keyPressed[code.String()] = false
} }
func (i *Input) keyDownEdge(code int) { func (i *Input) keyDownEdge(code int) {
@ -318,16 +318,15 @@ func (i *Input) Update(e js.Value) {
i.keyDownEdge(code) i.keyDownEdge(code)
return return
} }
cs := c.String() if jsutil.Equal(c, driverKeyToJSKey[driver.KeyUp]) ||
if cs == driverKeyToJSKey[driver.KeyUp] || jsutil.Equal(c, driverKeyToJSKey[driver.KeyDown]) ||
cs == driverKeyToJSKey[driver.KeyDown] || jsutil.Equal(c, driverKeyToJSKey[driver.KeyLeft]) ||
cs == driverKeyToJSKey[driver.KeyLeft] || jsutil.Equal(c, driverKeyToJSKey[driver.KeyRight]) ||
cs == driverKeyToJSKey[driver.KeyRight] || jsutil.Equal(c, driverKeyToJSKey[driver.KeyBackspace]) ||
cs == driverKeyToJSKey[driver.KeyBackspace] || jsutil.Equal(c, driverKeyToJSKey[driver.KeyTab]) {
cs == driverKeyToJSKey[driver.KeyTab] {
e.Call("preventDefault") e.Call("preventDefault")
} }
i.keyDown(cs) i.keyDown(c)
case jsutil.Equal(t, stringKeypress): case jsutil.Equal(t, stringKeypress):
if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) { if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) {
i.runeBuffer = append(i.runeBuffer, r) i.runeBuffer = append(i.runeBuffer, r)
@ -339,8 +338,7 @@ func (i *Input) Update(e js.Value) {
i.keyUpEdge(code) i.keyUpEdge(code)
return return
} }
code := e.Get("code").String() i.keyUp(e.Get("code"))
i.keyUp(code)
case jsutil.Equal(t, stringMousedown): case jsutil.Equal(t, stringMousedown):
button := e.Get("button").Int() button := e.Get("button").Int()
i.mouseDown(button) i.mouseDown(button)

View File

@ -17,115 +17,117 @@
package js package js
import ( import (
"syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
) )
var driverKeyToJSKey = map[driver.Key]string{ var driverKeyToJSKey = map[driver.Key]js.Value{
driver.Key0: "Digit0", driver.Key0: js.ValueOf("Digit0"),
driver.Key1: "Digit1", driver.Key1: js.ValueOf("Digit1"),
driver.Key2: "Digit2", driver.Key2: js.ValueOf("Digit2"),
driver.Key3: "Digit3", driver.Key3: js.ValueOf("Digit3"),
driver.Key4: "Digit4", driver.Key4: js.ValueOf("Digit4"),
driver.Key5: "Digit5", driver.Key5: js.ValueOf("Digit5"),
driver.Key6: "Digit6", driver.Key6: js.ValueOf("Digit6"),
driver.Key7: "Digit7", driver.Key7: js.ValueOf("Digit7"),
driver.Key8: "Digit8", driver.Key8: js.ValueOf("Digit8"),
driver.Key9: "Digit9", driver.Key9: js.ValueOf("Digit9"),
driver.KeyA: "KeyA", driver.KeyA: js.ValueOf("KeyA"),
driver.KeyApostrophe: "Quote", driver.KeyApostrophe: js.ValueOf("Quote"),
driver.KeyB: "KeyB", driver.KeyB: js.ValueOf("KeyB"),
driver.KeyBackslash: "Backslash", driver.KeyBackslash: js.ValueOf("Backslash"),
driver.KeyBackspace: "Backspace", driver.KeyBackspace: js.ValueOf("Backspace"),
driver.KeyC: "KeyC", driver.KeyC: js.ValueOf("KeyC"),
driver.KeyCapsLock: "CapsLock", driver.KeyCapsLock: js.ValueOf("CapsLock"),
driver.KeyComma: "Comma", driver.KeyComma: js.ValueOf("Comma"),
driver.KeyD: "KeyD", driver.KeyD: js.ValueOf("KeyD"),
driver.KeyDelete: "Delete", driver.KeyDelete: js.ValueOf("Delete"),
driver.KeyDown: "ArrowDown", driver.KeyDown: js.ValueOf("ArrowDown"),
driver.KeyE: "KeyE", driver.KeyE: js.ValueOf("KeyE"),
driver.KeyEnd: "End", driver.KeyEnd: js.ValueOf("End"),
driver.KeyEnter: "Enter", driver.KeyEnter: js.ValueOf("Enter"),
driver.KeyEqual: "Equal", driver.KeyEqual: js.ValueOf("Equal"),
driver.KeyEscape: "Escape", driver.KeyEscape: js.ValueOf("Escape"),
driver.KeyF: "KeyF", driver.KeyF: js.ValueOf("KeyF"),
driver.KeyF1: "F1", driver.KeyF1: js.ValueOf("F1"),
driver.KeyF10: "F10", driver.KeyF10: js.ValueOf("F10"),
driver.KeyF11: "F11", driver.KeyF11: js.ValueOf("F11"),
driver.KeyF12: "F12", driver.KeyF12: js.ValueOf("F12"),
driver.KeyF2: "F2", driver.KeyF2: js.ValueOf("F2"),
driver.KeyF3: "F3", driver.KeyF3: js.ValueOf("F3"),
driver.KeyF4: "F4", driver.KeyF4: js.ValueOf("F4"),
driver.KeyF5: "F5", driver.KeyF5: js.ValueOf("F5"),
driver.KeyF6: "F6", driver.KeyF6: js.ValueOf("F6"),
driver.KeyF7: "F7", driver.KeyF7: js.ValueOf("F7"),
driver.KeyF8: "F8", driver.KeyF8: js.ValueOf("F8"),
driver.KeyF9: "F9", driver.KeyF9: js.ValueOf("F9"),
driver.KeyG: "KeyG", driver.KeyG: js.ValueOf("KeyG"),
driver.KeyGraveAccent: "Backquote", driver.KeyGraveAccent: js.ValueOf("Backquote"),
driver.KeyH: "KeyH", driver.KeyH: js.ValueOf("KeyH"),
driver.KeyHome: "Home", driver.KeyHome: js.ValueOf("Home"),
driver.KeyI: "KeyI", driver.KeyI: js.ValueOf("KeyI"),
driver.KeyInsert: "Insert", driver.KeyInsert: js.ValueOf("Insert"),
driver.KeyJ: "KeyJ", driver.KeyJ: js.ValueOf("KeyJ"),
driver.KeyK: "KeyK", driver.KeyK: js.ValueOf("KeyK"),
driver.KeyKP0: "Numpad0", driver.KeyKP0: js.ValueOf("Numpad0"),
driver.KeyKP1: "Numpad1", driver.KeyKP1: js.ValueOf("Numpad1"),
driver.KeyKP2: "Numpad2", driver.KeyKP2: js.ValueOf("Numpad2"),
driver.KeyKP3: "Numpad3", driver.KeyKP3: js.ValueOf("Numpad3"),
driver.KeyKP4: "Numpad4", driver.KeyKP4: js.ValueOf("Numpad4"),
driver.KeyKP5: "Numpad5", driver.KeyKP5: js.ValueOf("Numpad5"),
driver.KeyKP6: "Numpad6", driver.KeyKP6: js.ValueOf("Numpad6"),
driver.KeyKP7: "Numpad7", driver.KeyKP7: js.ValueOf("Numpad7"),
driver.KeyKP8: "Numpad8", driver.KeyKP8: js.ValueOf("Numpad8"),
driver.KeyKP9: "Numpad9", driver.KeyKP9: js.ValueOf("Numpad9"),
driver.KeyKPAdd: "NumpadAdd", driver.KeyKPAdd: js.ValueOf("NumpadAdd"),
driver.KeyKPDecimal: "NumpadDecimal", driver.KeyKPDecimal: js.ValueOf("NumpadDecimal"),
driver.KeyKPDivide: "NumpadDivide", driver.KeyKPDivide: js.ValueOf("NumpadDivide"),
driver.KeyKPEnter: "NumpadEnter", driver.KeyKPEnter: js.ValueOf("NumpadEnter"),
driver.KeyKPEqual: "NumpadEqual", driver.KeyKPEqual: js.ValueOf("NumpadEqual"),
driver.KeyKPMultiply: "NumpadMultiply", driver.KeyKPMultiply: js.ValueOf("NumpadMultiply"),
driver.KeyKPSubtract: "NumpadSubtract", driver.KeyKPSubtract: js.ValueOf("NumpadSubtract"),
driver.KeyL: "KeyL", driver.KeyL: js.ValueOf("KeyL"),
driver.KeyLeft: "ArrowLeft", driver.KeyLeft: js.ValueOf("ArrowLeft"),
driver.KeyLeftAlt: "AltLeft", driver.KeyLeftAlt: js.ValueOf("AltLeft"),
driver.KeyLeftBracket: "BracketLeft", driver.KeyLeftBracket: js.ValueOf("BracketLeft"),
driver.KeyLeftControl: "ControlLeft", driver.KeyLeftControl: js.ValueOf("ControlLeft"),
driver.KeyLeftShift: "ShiftLeft", driver.KeyLeftShift: js.ValueOf("ShiftLeft"),
driver.KeyLeftSuper: "MetaLeft", driver.KeyLeftSuper: js.ValueOf("MetaLeft"),
driver.KeyM: "KeyM", driver.KeyM: js.ValueOf("KeyM"),
driver.KeyMenu: "ContextMenu", driver.KeyMenu: js.ValueOf("ContextMenu"),
driver.KeyMinus: "Minus", driver.KeyMinus: js.ValueOf("Minus"),
driver.KeyN: "KeyN", driver.KeyN: js.ValueOf("KeyN"),
driver.KeyNumLock: "NumLock", driver.KeyNumLock: js.ValueOf("NumLock"),
driver.KeyO: "KeyO", driver.KeyO: js.ValueOf("KeyO"),
driver.KeyP: "KeyP", driver.KeyP: js.ValueOf("KeyP"),
driver.KeyPageDown: "PageDown", driver.KeyPageDown: js.ValueOf("PageDown"),
driver.KeyPageUp: "PageUp", driver.KeyPageUp: js.ValueOf("PageUp"),
driver.KeyPause: "Pause", driver.KeyPause: js.ValueOf("Pause"),
driver.KeyPeriod: "Period", driver.KeyPeriod: js.ValueOf("Period"),
driver.KeyPrintScreen: "PrintScreen", driver.KeyPrintScreen: js.ValueOf("PrintScreen"),
driver.KeyQ: "KeyQ", driver.KeyQ: js.ValueOf("KeyQ"),
driver.KeyR: "KeyR", driver.KeyR: js.ValueOf("KeyR"),
driver.KeyRight: "ArrowRight", driver.KeyRight: js.ValueOf("ArrowRight"),
driver.KeyRightAlt: "AltRight", driver.KeyRightAlt: js.ValueOf("AltRight"),
driver.KeyRightBracket: "BracketRight", driver.KeyRightBracket: js.ValueOf("BracketRight"),
driver.KeyRightControl: "ControlRight", driver.KeyRightControl: js.ValueOf("ControlRight"),
driver.KeyRightShift: "ShiftRight", driver.KeyRightShift: js.ValueOf("ShiftRight"),
driver.KeyRightSuper: "MetaRight", driver.KeyRightSuper: js.ValueOf("MetaRight"),
driver.KeyS: "KeyS", driver.KeyS: js.ValueOf("KeyS"),
driver.KeyScrollLock: "ScrollLock", driver.KeyScrollLock: js.ValueOf("ScrollLock"),
driver.KeySemicolon: "Semicolon", driver.KeySemicolon: js.ValueOf("Semicolon"),
driver.KeySlash: "Slash", driver.KeySlash: js.ValueOf("Slash"),
driver.KeySpace: "Space", driver.KeySpace: js.ValueOf("Space"),
driver.KeyT: "KeyT", driver.KeyT: js.ValueOf("KeyT"),
driver.KeyTab: "Tab", driver.KeyTab: js.ValueOf("Tab"),
driver.KeyU: "KeyU", driver.KeyU: js.ValueOf("KeyU"),
driver.KeyUp: "ArrowUp", driver.KeyUp: js.ValueOf("ArrowUp"),
driver.KeyV: "KeyV", driver.KeyV: js.ValueOf("KeyV"),
driver.KeyW: "KeyW", driver.KeyW: js.ValueOf("KeyW"),
driver.KeyX: "KeyX", driver.KeyX: js.ValueOf("KeyX"),
driver.KeyY: "KeyY", driver.KeyY: js.ValueOf("KeyY"),
driver.KeyZ: "KeyZ", driver.KeyZ: js.ValueOf("KeyZ"),
} }
var edgeKeyCodeToDriverKey = map[int]driver.Key{ var edgeKeyCodeToDriverKey = map[int]driver.Key{