~eliasnaur/gio-patches

gio: app: send keypress events for modifier keys in macos v2 PROPOSED

Jeff Williams: 1
 gio: app: send keypress events for modifier keys in macos

 2 files changed, 54 insertions(+), 0 deletions(-)
Yes, that's much cleaner. I'll make that change too, and then send
another patch.

Thanks!
Next
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/55261/mbox | git am -3
Learn more about email & git

[PATCH v2] gio: app: send keypress events for modifier keys in macos Export this patch

This change generates keypress and release events for modifier keys in
MacOS. Specifically the Control, Alt, Shift and Command keys.
---
 app/os_macos.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++
 app/os_macos.m  |  4 ++++
 2 files changed, 54 insertions(+)

diff --git a/app/os_macos.go b/app/os_macos.go
index d62bb645..77d69e1b 100644
--- a/app/os_macos.go
+++ b/app/os_macos.go
@@ -561,6 +561,56 @@ func gio_onKeys(h C.uintptr_t, cstr C.CFTypeRef, ti C.double, mods C.NSUInteger,
	}
}

var lastModsOnFlagsChanged C.NSUInteger

type onFlagsChangedLogicTableEntry struct {
	modKeyMask C.NSUInteger
	modKeyName key.Name
}

var onFlagsChangedLogicTable = []onFlagsChangedLogicTableEntry{
	onFlagsChangedLogicTableEntry{
		C.NSControlKeyMask,
		key.NameCtrl,
	},
	onFlagsChangedLogicTableEntry{
		C.NSAlternateKeyMask,
		key.NameAlt,
	},
	onFlagsChangedLogicTableEntry{
		C.NSShiftKeyMask,
		key.NameShift,
	},
	onFlagsChangedLogicTableEntry{
		C.NSCommandKeyMask,
		key.NameCommand,
	},
}

//export gio_onFlagsChanged
func gio_onFlagsChanged(h C.uintptr_t, mods C.NSUInteger) {
	w := windowFor(h)
	for _, e := range onFlagsChangedLogicTable {
		if mods&e.modKeyMask != 0 {
			if lastModsOnFlagsChanged&e.modKeyMask == 0 {
				w.ProcessEvent(key.Event{Name: e.modKeyName,
					State: key.Press,
				})
			}

			continue
		}

		if lastModsOnFlagsChanged&e.modKeyMask != 0 {
			w.ProcessEvent(key.Event{Name: e.modKeyName,
				State: key.Release,
			})
		}
	}

	lastModsOnFlagsChanged = mods
}

//export gio_onText
func gio_onText(h C.uintptr_t, cstr C.CFTypeRef) {
	str := nsstringToString(cstr)
diff --git a/app/os_macos.m b/app/os_macos.m
index 9c895148..6e3438e3 100644
--- a/app/os_macos.m
+++ b/app/os_macos.m
@@ -136,6 +136,10 @@ static void handleMouse(GioView *view, NSEvent *event, int typ, CGFloat dx, CGFl
	NSString *keys = [event charactersIgnoringModifiers];
	gio_onKeys(self.handle, (__bridge CFTypeRef)keys, [event timestamp], [event modifierFlags], true);
}
- (void)flagsChanged:(NSEvent *)event {
	[self interpretKeyEvents:[NSArray arrayWithObject:event]];
	gio_onFlagsChanged(self.handle, [event modifierFlags]);
}
- (void)keyUp:(NSEvent *)event {
	NSString *keys = [event charactersIgnoringModifiers];
	gio_onKeys(self.handle, (__bridge CFTypeRef)keys, [event timestamp], [event modifierFlags], false);
-- 
2.30.2