From 8c356fa5de2b32f43c18588599b261d7abc19d4b Mon Sep 17 00:00:00 2001 From: _sam <3804518+Aahung@users.noreply.github.com> Date: Fri, 10 Aug 2018 02:44:25 -0700 Subject: [PATCH] Handle modifier keys (ctrl, opt, cmd, shift and fn) #8 --- Unshaky/Info.plist | 4 ++-- Unshaky/PreferenceViewController.swift | 1 + Unshaky/ShakyPressPreventer.m | 29 +++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Unshaky/Info.plist b/Unshaky/Info.plist index a6cb920..7dc2947 100644 --- a/Unshaky/Info.plist +++ b/Unshaky/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.2.2 + 0.2.3 CFBundleVersion - 7 + 8 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) LSUIElement diff --git a/Unshaky/PreferenceViewController.swift b/Unshaky/PreferenceViewController.swift index 7d4e43e..87e6fdd 100644 --- a/Unshaky/PreferenceViewController.swift +++ b/Unshaky/PreferenceViewController.swift @@ -132,6 +132,7 @@ class PreferenceViewController: NSViewController, 117: "ForwardDelete", 52: "Linefeed", 53: "Escape", + 54: "RightCommand", 55: "Command", 56: "Shift", 57: "CapsLock", diff --git a/Unshaky/ShakyPressPreventer.m b/Unshaky/ShakyPressPreventer.m index 156aeff..cc1a3f1 100644 --- a/Unshaky/ShakyPressPreventer.m +++ b/Unshaky/ShakyPressPreventer.m @@ -58,6 +58,33 @@ - (CGEventRef)filterShakyPressEvent:(CGEventRef)event { // ignore unconfigured keys if (keyDelays[keyCode] == 0) return event; + { + // handle control, command, option and shift keys + CGEventFlags flags; + BOOL isModifierPressed; + switch (keyCode) { + case 54: // RightCommand + case 55: // Command + case 56: // Shift + case 58: // Option + case 59: // Control + case 60: // RightShift + case 61: // RightOption + case 62: // RightControl (this key does not exist on MacBook Pro's keyboard) + case 63: // Function + flags = CGEventGetFlags(event); + isModifierPressed = (flags & (kCGEventFlagMaskAlternate | kCGEventFlagMaskCommand | kCGEventFlagMaskControl | kCGEventFlagMaskShift | kCGEventFlagMaskSecondaryFn)) > 0; + if (isModifierPressed) + eventType = kCGEventKeyDown; + else + eventType = kCGEventKeyUp; + break; + + default: + break; + } + } + if (lastPressedTimestamps[keyCode] == 0.0) { lastPressedTimestamps[keyCode] = [[NSDate date] timeIntervalSince1970]; lastPressedEventTypes[keyCode] = eventType; @@ -92,7 +119,7 @@ - (CGEventRef)filterShakyPressEvent:(CGEventRef)event { - (BOOL)setupInputDeviceListener { - CGEventMask eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp)); + CGEventMask eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp) | (1 << kCGEventFlagsChanged)); CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, myCGEventCallback, (__bridge void *)(self)); if (!eventTap) {