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 <larry@theclapp.org>
This commit is contained in:
Larry Clapp
2019-11-23 11:22:27 -05:00
committed by Elias Naur
parent d5424ef7fc
commit 65bc7be256
2 changed files with 15 additions and 13 deletions
+10 -8
View File
@@ -10,6 +10,7 @@ import (
"runtime" "runtime"
"sync" "sync"
"time" "time"
"unicode"
"unsafe" "unsafe"
"gioui.org/f32" "gioui.org/f32"
@@ -118,13 +119,14 @@ func gio_onFrameCallback(view C.CFTypeRef) {
//export gio_onKeys //export gio_onKeys
func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) { func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) {
str := C.GoString(cstr) str := C.GoString(cstr)
kmods := convertMods(mods)
viewDo(view, func(views viewMap, view C.CFTypeRef) { viewDo(view, func(views viewMap, view C.CFTypeRef) {
w := views[view] w := views[view]
for _, k := range str { for _, k := range str {
if n, ok := convertKey(k); ok { if n, ok := convertKey(k); ok {
w.w.Event(key.Event{ w.w.Event(key.Event{
Name: n, Name: n,
Modifiers: convertMods(mods), Modifiers: kmods,
}) })
} }
} }
@@ -292,12 +294,6 @@ func Main() {
} }
func convertKey(k rune) (string, bool) { 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 var n string
switch k { switch k {
case 0x1b: case 0x1b:
@@ -312,6 +308,8 @@ func convertKey(k rune) (string, bool) {
n = key.NameDownArrow n = key.NameDownArrow
case 0xd: case 0xd:
n = key.NameReturn n = key.NameReturn
case 0x3:
n = key.NameEnter
case C.NSHomeFunctionKey: case C.NSHomeFunctionKey:
n = key.NameHome n = key.NameHome
case C.NSEndFunctionKey: case C.NSEndFunctionKey:
@@ -353,7 +351,11 @@ func convertKey(k rune) (string, bool) {
case 0x20: case 0x20:
n = "Space" n = "Space"
default: default:
return "", false k = unicode.ToUpper(k)
if !unicode.IsPrint(k) {
return "", false
}
n = string(k)
} }
return n, true return n, true
} }
+5 -5
View File
@@ -39,12 +39,12 @@ type FocusEvent struct {
// An Event is generated when a key is pressed. For text input // An Event is generated when a key is pressed. For text input
// use EditEvent. // use EditEvent.
type Event struct { type Event struct {
// Name of the key. For letters, the upper case form is used. // Name of the key. For letters, the upper case form is used, via
// Use the Name* constants for special keys suchs as the arrow // unicode.ToUpper. The shift modifier is taken into account, all other
// keys. // modifiers are ignored. For example, the "shift-1" and "ctrl-shift-1"
// combinations both give the Name "!" with the US keyboard layout.
Name string Name string
// Modifiers is the set of active modifiers when // Modifiers is the set of active modifiers when the key was pressed.
// the key was pressed.
Modifiers Modifiers Modifiers Modifiers
} }