exp/textinput: bug fix: pressing enter key was often ignored on iOS Safari

Closes #3015
This commit is contained in:
Hajime Hoshi 2024-06-12 02:44:48 +09:00
parent 546c47878d
commit 96e0fd7a50
2 changed files with 19 additions and 14 deletions

View File

@ -16,7 +16,6 @@ package textinput
import (
"fmt"
"strings"
"syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/ui"
@ -119,7 +118,7 @@ func (t *textInput) init() {
return nil
}
// Though `isComposing` is false, send the text as being not committed for text completion with a virtual keyboard.
if isVirtualKeyboard() {
if ui.IsVirtualKeyboard() {
t.trySend(false)
return nil
}
@ -236,15 +235,3 @@ func (t *textInput) trySend(committed bool) {
t.textareaElement.Set("value", "")
}
}
func isVirtualKeyboard() bool {
// Detect a virtual keyboard by the user agent.
// Note that this is not a correct way to detect a virtual keyboard.
// In the future, we should use the `navigator.virtualKeyboard` API.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/virtualKeyboard
ua := js.Global().Get("navigator").Get("userAgent").String()
if strings.Contains(ua, "Android") || strings.Contains(ua, "iPhone") || strings.Contains(ua, "iPad") || strings.Contains(ua, "iPod") {
return true
}
return false
}

View File

@ -16,6 +16,7 @@ package ui
import (
"math"
"strings"
"syscall/js"
"unicode"
)
@ -64,6 +65,11 @@ var codeToMouseButton = map[int]MouseButton{
func eventToKeys(e js.Value) (key0, key1 Key, fromKeyProperty bool) {
id := jsCodeToID(e.Get("code"))
// On mobile browsers, treat enter key as if this is from a `key` property.
if IsVirtualKeyboard() && id == KeyEnter {
return KeyEnter, -1, true
}
if id >= 0 {
return id, -1, false
}
@ -426,3 +432,15 @@ func (i *InputState) resetForBlur() {
}
i.Touches = i.Touches[:0]
}
func IsVirtualKeyboard() bool {
// Detect a virtual keyboard by the user agent.
// Note that this is not a correct way to detect a virtual keyboard.
// In the future, we should use the `navigator.virtualKeyboard` API.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/virtualKeyboard
ua := js.Global().Get("navigator").Get("userAgent").String()
if strings.Contains(ua, "Android") || strings.Contains(ua, "iPhone") || strings.Contains(ua, "iPad") || strings.Contains(ua, "iPod") {
return true
}
return false
}