~eliasnaur/gio-patches

app/internal/window: allow punctuation as keycode events v2 PROPOSED

Larry Clapp: 1
 app/internal/window: allow punctuation as keycode events

 3 files changed, 20 insertions(+), 7 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/9086/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH v2] app/internal/window: allow punctuation as keycode events Export this patch

Allows things like Ctrl-{.

All punctuation is returned as-is, e.g. "!" is "!", not Shift-1, and "{"
is "{", not Shift-[.

Added Event.Code to give a numeric code for keys independent of modifier
keys.  So the values of Code for "1" and "shift-1" and "ctrl-1" are all
the same.

Signed-off-by: Larry Clapp <larry@theclapp.org>
---
 app/internal/window/gl_macos.m  |  3 ++-
 app/internal/window/os_macos.go | 12 +++++++++---
 io/key/key.go                   | 12 +++++++++---
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/app/internal/window/gl_macos.m b/app/internal/window/gl_macos.m
index fdadae2..f4212f6 100644
--- a/app/internal/window/gl_macos.m
+++ b/app/internal/window/gl_macos.m
@@ -104,7 +104,8 @@ CVDisplayLinkRef displayLink;
}
- (void)keyDown:(NSEvent *)event {
	NSString *keys = [event charactersIgnoringModifiers];
	gio_onKeys((__bridge CFTypeRef)self, (char *)[keys UTF8String], [event timestamp], [event modifierFlags]);
	gio_onKeys((__bridge CFTypeRef)self, (char *)[keys UTF8String], [event timestamp],
	    [event modifierFlags], [event keyCode]);
	[self interpretKeyEvents:[NSArray arrayWithObject:event]];
}
- (void)insertText:(id)string {
diff --git a/app/internal/window/os_macos.go b/app/internal/window/os_macos.go
index 018bf64..a2b6f7e 100644
--- a/app/internal/window/os_macos.go
+++ b/app/internal/window/os_macos.go
@@ -116,7 +116,7 @@ func gio_onFrameCallback(view C.CFTypeRef) {
}

//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, keyCode C.int) {
	str := C.GoString(cstr)
	var kmods key.Modifiers
	if mods&C.NSAlternateKeyMask != 0 {
@@ -135,7 +135,11 @@ func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger)
		w := views[view]
		for _, k := range str {
			if n, ok := convertKey(k); ok {
				w.w.Event(key.Event{Name: n, Modifiers: kmods})
				w.w.Event(key.Event{
					Name:      n,
					Code:      int(keyCode),
					Modifiers: kmods,
				})
			}
		}
	})
@@ -301,7 +305,7 @@ func Main() {
}

func convertKey(k rune) (string, bool) {
	if '0' <= k && k <= '9' || 'A' <= k && k <= 'Z' {
	if '!' <= k && k <= '`' || '{' <= k && k <= '~' {
		return string(k), true
	}
	if 'a' <= k && k <= 'z' {
@@ -321,6 +325,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:
diff --git a/io/key/key.go b/io/key/key.go
index e90c5df..adacc8d 100644
--- a/io/key/key.go
+++ b/io/key/key.go
@@ -39,10 +39,16 @@ 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.  For
	// punctuation, if shift-1 is "!", Name is "!" (not "1").
	// Use the Name* constants for special keys such as the arrow keys, e.g.
	// NameLeftArrow.
	Name string
	// Raw Code of the key.  Not ASCII.  Does not vary with modifier keys, e.g.
	// 1 and shift-1 and ctrl-1 are all the same Code.  On newer Macs, codes
	// are defined as an enum in (deep breath)
	// /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
	Code int
	// Modifiers is the set of active modifiers when
	// the key was pressed.
	Modifiers Modifiers
-- 
2.23.0