From 65bc7be2569b708f45335b9d7af500d32a8a3dd5 Mon Sep 17 00:00:00 2001 From: Larry Clapp Date: Sat, 23 Nov 2019 11:22:27 -0500 Subject: [PATCH] app/internal/window: allow punctuation as keycode events Allows things like "ctrl-{" and ".". All punctuation is returned as-is, e.g. "!" is "!", not "shift-1", and "{" is "{", not "shift-[". Also add the Enter key as a known key (fn-return on my Mac). Signed-off-by: Larry Clapp --- app/internal/window/os_macos.go | 18 ++++++++++-------- io/key/key.go | 10 +++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/internal/window/os_macos.go b/app/internal/window/os_macos.go index dc8ab558..42c23fa8 100644 --- a/app/internal/window/os_macos.go +++ b/app/internal/window/os_macos.go @@ -10,6 +10,7 @@ import ( "runtime" "sync" "time" + "unicode" "unsafe" "gioui.org/f32" @@ -118,13 +119,14 @@ func gio_onFrameCallback(view C.CFTypeRef) { //export gio_onKeys func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) { str := C.GoString(cstr) + kmods := convertMods(mods) viewDo(view, func(views viewMap, view C.CFTypeRef) { w := views[view] for _, k := range str { if n, ok := convertKey(k); ok { w.w.Event(key.Event{ Name: n, - Modifiers: convertMods(mods), + Modifiers: kmods, }) } } @@ -292,12 +294,6 @@ func Main() { } func convertKey(k rune) (string, bool) { - if '0' <= k && k <= '9' || 'A' <= k && k <= 'Z' { - return string(k), true - } - if 'a' <= k && k <= 'z' { - return string(k - 0x20), true - } var n string switch k { case 0x1b: @@ -312,6 +308,8 @@ func convertKey(k rune) (string, bool) { n = key.NameDownArrow case 0xd: n = key.NameReturn + case 0x3: + n = key.NameEnter case C.NSHomeFunctionKey: n = key.NameHome case C.NSEndFunctionKey: @@ -353,7 +351,11 @@ func convertKey(k rune) (string, bool) { case 0x20: n = "Space" default: - return "", false + k = unicode.ToUpper(k) + if !unicode.IsPrint(k) { + return "", false + } + n = string(k) } return n, true } diff --git a/io/key/key.go b/io/key/key.go index e90c5dff..faeaf8c7 100644 --- a/io/key/key.go +++ b/io/key/key.go @@ -39,12 +39,12 @@ type FocusEvent struct { // An Event is generated when a key is pressed. For text input // use EditEvent. type Event struct { - // Name of the key. For letters, the upper case form is used. - // Use the Name* constants for special keys suchs as the arrow - // keys. + // Name of the key. For letters, the upper case form is used, via + // unicode.ToUpper. The shift modifier is taken into account, all other + // modifiers are ignored. For example, the "shift-1" and "ctrl-shift-1" + // combinations both give the Name "!" with the US keyboard layout. Name string - // Modifiers is the set of active modifiers when - // the key was pressed. + // Modifiers is the set of active modifiers when the key was pressed. Modifiers Modifiers }