internal/ui: remove the keypress event and the charCode preperty usages

These APIs are deprecated.

Closes #2199
This commit is contained in:
Hajime Hoshi 2022-07-12 23:09:34 +09:00
parent 55adc2113e
commit 2203c3c448
2 changed files with 41 additions and 33 deletions

View File

@ -21,7 +21,6 @@ import (
var ( var (
stringKeydown = js.ValueOf("keydown") stringKeydown = js.ValueOf("keydown")
stringKeypress = js.ValueOf("keypress")
stringKeyup = js.ValueOf("keyup") stringKeyup = js.ValueOf("keyup")
stringMousedown = js.ValueOf("mousedown") stringMousedown = js.ValueOf("mousedown")
stringMouseup = js.ValueOf("mouseup") stringMouseup = js.ValueOf("mouseup")
@ -198,41 +197,28 @@ func (i *Input) updateFromEvent(e js.Value) {
// overhead (#1437). // overhead (#1437).
switch t := e.Get("type"); { switch t := e.Get("type"); {
case t.Equal(stringKeydown): case t.Equal(stringKeydown):
c := e.Get("code") if str := e.Get("key").String(); isKeyString(str) {
if c.Type() != js.TypeString { for _, r := range str {
code := e.Get("keyCode").Int() if unicode.IsPrint(r) {
if edgeKeyCodeToUIKey[code] == KeyArrowUp ||
edgeKeyCodeToUIKey[code] == KeyArrowDown ||
edgeKeyCodeToUIKey[code] == KeyArrowLeft ||
edgeKeyCodeToUIKey[code] == KeyArrowRight ||
edgeKeyCodeToUIKey[code] == KeyBackspace ||
edgeKeyCodeToUIKey[code] == KeyTab {
e.Call("preventDefault")
}
i.keyDownEdge(code)
return
}
if c.Equal(uiKeyToJSKey[KeyArrowUp]) ||
c.Equal(uiKeyToJSKey[KeyArrowDown]) ||
c.Equal(uiKeyToJSKey[KeyArrowLeft]) ||
c.Equal(uiKeyToJSKey[KeyArrowRight]) ||
c.Equal(uiKeyToJSKey[KeyBackspace]) ||
c.Equal(uiKeyToJSKey[KeyTab]) {
e.Call("preventDefault")
}
i.keyDown(c)
case t.Equal(stringKeypress):
if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) {
i.runeBuffer = append(i.runeBuffer, r) i.runeBuffer = append(i.runeBuffer, r)
} }
case t.Equal(stringKeyup): }
if e.Get("code").Type() != js.TypeString { }
// Assume that UA is Edge.
code := e.Get("keyCode").Int() c := e.Get("code")
i.keyUpEdge(code) if c.Type() != js.TypeString {
i.keyDownEdge(e.Get("keyCode").Int())
return return
} }
i.keyUp(e.Get("code")) i.keyDown(c)
case t.Equal(stringKeyup):
c := e.Get("code")
if c.Type() != js.TypeString {
// Assume that UA is Edge.
i.keyUpEdge(e.Get("keyCode").Int())
return
}
i.keyUp(c)
case t.Equal(stringMousedown): case t.Equal(stringMousedown):
button := e.Get("button").Int() button := e.Get("button").Int()
i.mouseDown(button) i.mouseDown(button)
@ -313,3 +299,31 @@ func (i *Input) updateForGo2Cpp() {
} }
} }
} }
func isKeyString(str string) bool {
// From https://www.w3.org/TR/uievents-key/#keys-unicode,
//
// A key string is a string containing a 0 or 1 non-control characters
// ("base" characters) followed by 0 or more combining characters. The
// string MUST be in Normalized Form C (NFC) as described in
// [UnicodeNormalizationForms].
//
// A non-control character is any valid Unicode character except those
// that are part of the "Other, Control" ("Cc") General Category.
//
// A combining character is any valid Unicode character in the "Mark,
// Spacing Combining" ("Mc") General Category or with a non-zero
// Combining Class.
for i, r := range str {
if i == 0 {
if unicode.Is(unicode.Cc, r) {
return false
}
continue
}
if !unicode.Is(unicode.Mc, r) {
return false
}
}
return true
}

View File

@ -508,12 +508,6 @@ func setCanvasEventHandlers(v js.Value) {
// Focus the canvas explicitly to activate tha game (#961). // Focus the canvas explicitly to activate tha game (#961).
v.Call("focus") v.Call("focus")
e := args[0]
// Don't 'preventDefault' on keydown events or keypress events wouldn't work (#715).
theUI.input.updateFromEvent(e)
return nil
}))
v.Call("addEventListener", "keypress", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
e := args[0] e := args[0]
e.Call("preventDefault") e.Call("preventDefault")
theUI.input.updateFromEvent(e) theUI.input.updateFromEvent(e)