diff --git a/genkeys.go b/genkeys.go index 1141caed9..16da57d00 100644 --- a/genkeys.go +++ b/genkeys.go @@ -503,11 +503,13 @@ const uidriverJsKeysTmpl = `{{.License}} package js import ( + "syscall/js" + "github.com/hajimehoshi/ebiten/v2/internal/driver" ) -var driverKeyToJSKey = map[driver.Key]string{ -{{range $name, $code := .DriverKeyNameToJSKey}}driver.Key{{$name}}: {{$code | printf "%q"}}, +var driverKeyToJSKey = map[driver.Key]js.Value{ +{{range $name, $code := .DriverKeyNameToJSKey}}driver.Key{{$name}}: js.ValueOf({{$code | printf "%q"}}), {{end}} } diff --git a/internal/uidriver/js/input_js.go b/internal/uidriver/js/input_js.go index 49bc9d401..7b54bc374 100644 --- a/internal/uidriver/js/input_js.go +++ b/internal/uidriver/js/input_js.go @@ -168,7 +168,7 @@ func (i *Input) resetForFrame() { func (i *Input) IsKeyPressed(key driver.Key) bool { if i.keyPressed != nil { - if i.keyPressed[driverKeyToJSKey[key]] { + if i.keyPressed[driverKeyToJSKey[key].String()] { return true } } @@ -210,18 +210,18 @@ func (i *Input) Wheel() (xoff, yoff float64) { return i.wheelX, i.wheelY } -func (i *Input) keyDown(code string) { +func (i *Input) keyDown(code js.Value) { if i.keyPressed == nil { 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 { i.keyPressed = map[string]bool{} } - i.keyPressed[code] = false + i.keyPressed[code.String()] = false } func (i *Input) keyDownEdge(code int) { @@ -318,16 +318,15 @@ func (i *Input) Update(e js.Value) { i.keyDownEdge(code) return } - cs := c.String() - if cs == driverKeyToJSKey[driver.KeyUp] || - cs == driverKeyToJSKey[driver.KeyDown] || - cs == driverKeyToJSKey[driver.KeyLeft] || - cs == driverKeyToJSKey[driver.KeyRight] || - cs == driverKeyToJSKey[driver.KeyBackspace] || - cs == driverKeyToJSKey[driver.KeyTab] { + if jsutil.Equal(c, driverKeyToJSKey[driver.KeyUp]) || + jsutil.Equal(c, driverKeyToJSKey[driver.KeyDown]) || + jsutil.Equal(c, driverKeyToJSKey[driver.KeyLeft]) || + jsutil.Equal(c, driverKeyToJSKey[driver.KeyRight]) || + jsutil.Equal(c, driverKeyToJSKey[driver.KeyBackspace]) || + jsutil.Equal(c, driverKeyToJSKey[driver.KeyTab]) { e.Call("preventDefault") } - i.keyDown(cs) + i.keyDown(c) case jsutil.Equal(t, stringKeypress): if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) { i.runeBuffer = append(i.runeBuffer, r) @@ -339,8 +338,7 @@ func (i *Input) Update(e js.Value) { i.keyUpEdge(code) return } - code := e.Get("code").String() - i.keyUp(code) + i.keyUp(e.Get("code")) case jsutil.Equal(t, stringMousedown): button := e.Get("button").Int() i.mouseDown(button) diff --git a/internal/uidriver/js/keys_js.go b/internal/uidriver/js/keys_js.go index 4ec659750..95172c917 100644 --- a/internal/uidriver/js/keys_js.go +++ b/internal/uidriver/js/keys_js.go @@ -17,115 +17,117 @@ package js import ( + "syscall/js" + "github.com/hajimehoshi/ebiten/v2/internal/driver" ) -var driverKeyToJSKey = map[driver.Key]string{ - driver.Key0: "Digit0", - driver.Key1: "Digit1", - driver.Key2: "Digit2", - driver.Key3: "Digit3", - driver.Key4: "Digit4", - driver.Key5: "Digit5", - driver.Key6: "Digit6", - driver.Key7: "Digit7", - driver.Key8: "Digit8", - driver.Key9: "Digit9", - driver.KeyA: "KeyA", - driver.KeyApostrophe: "Quote", - driver.KeyB: "KeyB", - driver.KeyBackslash: "Backslash", - driver.KeyBackspace: "Backspace", - driver.KeyC: "KeyC", - driver.KeyCapsLock: "CapsLock", - driver.KeyComma: "Comma", - driver.KeyD: "KeyD", - driver.KeyDelete: "Delete", - driver.KeyDown: "ArrowDown", - driver.KeyE: "KeyE", - driver.KeyEnd: "End", - driver.KeyEnter: "Enter", - driver.KeyEqual: "Equal", - driver.KeyEscape: "Escape", - driver.KeyF: "KeyF", - driver.KeyF1: "F1", - driver.KeyF10: "F10", - driver.KeyF11: "F11", - driver.KeyF12: "F12", - driver.KeyF2: "F2", - driver.KeyF3: "F3", - driver.KeyF4: "F4", - driver.KeyF5: "F5", - driver.KeyF6: "F6", - driver.KeyF7: "F7", - driver.KeyF8: "F8", - driver.KeyF9: "F9", - driver.KeyG: "KeyG", - driver.KeyGraveAccent: "Backquote", - driver.KeyH: "KeyH", - driver.KeyHome: "Home", - driver.KeyI: "KeyI", - driver.KeyInsert: "Insert", - driver.KeyJ: "KeyJ", - driver.KeyK: "KeyK", - driver.KeyKP0: "Numpad0", - driver.KeyKP1: "Numpad1", - driver.KeyKP2: "Numpad2", - driver.KeyKP3: "Numpad3", - driver.KeyKP4: "Numpad4", - driver.KeyKP5: "Numpad5", - driver.KeyKP6: "Numpad6", - driver.KeyKP7: "Numpad7", - driver.KeyKP8: "Numpad8", - driver.KeyKP9: "Numpad9", - driver.KeyKPAdd: "NumpadAdd", - driver.KeyKPDecimal: "NumpadDecimal", - driver.KeyKPDivide: "NumpadDivide", - driver.KeyKPEnter: "NumpadEnter", - driver.KeyKPEqual: "NumpadEqual", - driver.KeyKPMultiply: "NumpadMultiply", - driver.KeyKPSubtract: "NumpadSubtract", - driver.KeyL: "KeyL", - driver.KeyLeft: "ArrowLeft", - driver.KeyLeftAlt: "AltLeft", - driver.KeyLeftBracket: "BracketLeft", - driver.KeyLeftControl: "ControlLeft", - driver.KeyLeftShift: "ShiftLeft", - driver.KeyLeftSuper: "MetaLeft", - driver.KeyM: "KeyM", - driver.KeyMenu: "ContextMenu", - driver.KeyMinus: "Minus", - driver.KeyN: "KeyN", - driver.KeyNumLock: "NumLock", - driver.KeyO: "KeyO", - driver.KeyP: "KeyP", - driver.KeyPageDown: "PageDown", - driver.KeyPageUp: "PageUp", - driver.KeyPause: "Pause", - driver.KeyPeriod: "Period", - driver.KeyPrintScreen: "PrintScreen", - driver.KeyQ: "KeyQ", - driver.KeyR: "KeyR", - driver.KeyRight: "ArrowRight", - driver.KeyRightAlt: "AltRight", - driver.KeyRightBracket: "BracketRight", - driver.KeyRightControl: "ControlRight", - driver.KeyRightShift: "ShiftRight", - driver.KeyRightSuper: "MetaRight", - driver.KeyS: "KeyS", - driver.KeyScrollLock: "ScrollLock", - driver.KeySemicolon: "Semicolon", - driver.KeySlash: "Slash", - driver.KeySpace: "Space", - driver.KeyT: "KeyT", - driver.KeyTab: "Tab", - driver.KeyU: "KeyU", - driver.KeyUp: "ArrowUp", - driver.KeyV: "KeyV", - driver.KeyW: "KeyW", - driver.KeyX: "KeyX", - driver.KeyY: "KeyY", - driver.KeyZ: "KeyZ", +var driverKeyToJSKey = map[driver.Key]js.Value{ + driver.Key0: js.ValueOf("Digit0"), + driver.Key1: js.ValueOf("Digit1"), + driver.Key2: js.ValueOf("Digit2"), + driver.Key3: js.ValueOf("Digit3"), + driver.Key4: js.ValueOf("Digit4"), + driver.Key5: js.ValueOf("Digit5"), + driver.Key6: js.ValueOf("Digit6"), + driver.Key7: js.ValueOf("Digit7"), + driver.Key8: js.ValueOf("Digit8"), + driver.Key9: js.ValueOf("Digit9"), + driver.KeyA: js.ValueOf("KeyA"), + driver.KeyApostrophe: js.ValueOf("Quote"), + driver.KeyB: js.ValueOf("KeyB"), + driver.KeyBackslash: js.ValueOf("Backslash"), + driver.KeyBackspace: js.ValueOf("Backspace"), + driver.KeyC: js.ValueOf("KeyC"), + driver.KeyCapsLock: js.ValueOf("CapsLock"), + driver.KeyComma: js.ValueOf("Comma"), + driver.KeyD: js.ValueOf("KeyD"), + driver.KeyDelete: js.ValueOf("Delete"), + driver.KeyDown: js.ValueOf("ArrowDown"), + driver.KeyE: js.ValueOf("KeyE"), + driver.KeyEnd: js.ValueOf("End"), + driver.KeyEnter: js.ValueOf("Enter"), + driver.KeyEqual: js.ValueOf("Equal"), + driver.KeyEscape: js.ValueOf("Escape"), + driver.KeyF: js.ValueOf("KeyF"), + driver.KeyF1: js.ValueOf("F1"), + driver.KeyF10: js.ValueOf("F10"), + driver.KeyF11: js.ValueOf("F11"), + driver.KeyF12: js.ValueOf("F12"), + driver.KeyF2: js.ValueOf("F2"), + driver.KeyF3: js.ValueOf("F3"), + driver.KeyF4: js.ValueOf("F4"), + driver.KeyF5: js.ValueOf("F5"), + driver.KeyF6: js.ValueOf("F6"), + driver.KeyF7: js.ValueOf("F7"), + driver.KeyF8: js.ValueOf("F8"), + driver.KeyF9: js.ValueOf("F9"), + driver.KeyG: js.ValueOf("KeyG"), + driver.KeyGraveAccent: js.ValueOf("Backquote"), + driver.KeyH: js.ValueOf("KeyH"), + driver.KeyHome: js.ValueOf("Home"), + driver.KeyI: js.ValueOf("KeyI"), + driver.KeyInsert: js.ValueOf("Insert"), + driver.KeyJ: js.ValueOf("KeyJ"), + driver.KeyK: js.ValueOf("KeyK"), + driver.KeyKP0: js.ValueOf("Numpad0"), + driver.KeyKP1: js.ValueOf("Numpad1"), + driver.KeyKP2: js.ValueOf("Numpad2"), + driver.KeyKP3: js.ValueOf("Numpad3"), + driver.KeyKP4: js.ValueOf("Numpad4"), + driver.KeyKP5: js.ValueOf("Numpad5"), + driver.KeyKP6: js.ValueOf("Numpad6"), + driver.KeyKP7: js.ValueOf("Numpad7"), + driver.KeyKP8: js.ValueOf("Numpad8"), + driver.KeyKP9: js.ValueOf("Numpad9"), + driver.KeyKPAdd: js.ValueOf("NumpadAdd"), + driver.KeyKPDecimal: js.ValueOf("NumpadDecimal"), + driver.KeyKPDivide: js.ValueOf("NumpadDivide"), + driver.KeyKPEnter: js.ValueOf("NumpadEnter"), + driver.KeyKPEqual: js.ValueOf("NumpadEqual"), + driver.KeyKPMultiply: js.ValueOf("NumpadMultiply"), + driver.KeyKPSubtract: js.ValueOf("NumpadSubtract"), + driver.KeyL: js.ValueOf("KeyL"), + driver.KeyLeft: js.ValueOf("ArrowLeft"), + driver.KeyLeftAlt: js.ValueOf("AltLeft"), + driver.KeyLeftBracket: js.ValueOf("BracketLeft"), + driver.KeyLeftControl: js.ValueOf("ControlLeft"), + driver.KeyLeftShift: js.ValueOf("ShiftLeft"), + driver.KeyLeftSuper: js.ValueOf("MetaLeft"), + driver.KeyM: js.ValueOf("KeyM"), + driver.KeyMenu: js.ValueOf("ContextMenu"), + driver.KeyMinus: js.ValueOf("Minus"), + driver.KeyN: js.ValueOf("KeyN"), + driver.KeyNumLock: js.ValueOf("NumLock"), + driver.KeyO: js.ValueOf("KeyO"), + driver.KeyP: js.ValueOf("KeyP"), + driver.KeyPageDown: js.ValueOf("PageDown"), + driver.KeyPageUp: js.ValueOf("PageUp"), + driver.KeyPause: js.ValueOf("Pause"), + driver.KeyPeriod: js.ValueOf("Period"), + driver.KeyPrintScreen: js.ValueOf("PrintScreen"), + driver.KeyQ: js.ValueOf("KeyQ"), + driver.KeyR: js.ValueOf("KeyR"), + driver.KeyRight: js.ValueOf("ArrowRight"), + driver.KeyRightAlt: js.ValueOf("AltRight"), + driver.KeyRightBracket: js.ValueOf("BracketRight"), + driver.KeyRightControl: js.ValueOf("ControlRight"), + driver.KeyRightShift: js.ValueOf("ShiftRight"), + driver.KeyRightSuper: js.ValueOf("MetaRight"), + driver.KeyS: js.ValueOf("KeyS"), + driver.KeyScrollLock: js.ValueOf("ScrollLock"), + driver.KeySemicolon: js.ValueOf("Semicolon"), + driver.KeySlash: js.ValueOf("Slash"), + driver.KeySpace: js.ValueOf("Space"), + driver.KeyT: js.ValueOf("KeyT"), + driver.KeyTab: js.ValueOf("Tab"), + driver.KeyU: js.ValueOf("KeyU"), + driver.KeyUp: js.ValueOf("ArrowUp"), + driver.KeyV: js.ValueOf("KeyV"), + driver.KeyW: js.ValueOf("KeyW"), + driver.KeyX: js.ValueOf("KeyX"), + driver.KeyY: js.ValueOf("KeyY"), + driver.KeyZ: js.ValueOf("KeyZ"), } var edgeKeyCodeToDriverKey = map[int]driver.Key{