forked from joejulian/gio
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:
@@ -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
|
||||
}
|
||||
|
||||
+5
-5
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user