Skip to content

Commit

Permalink
An modified version of #18, tackling CMD+SPACE issue in #1
Browse files Browse the repository at this point in the history
  • Loading branch information
aahung committed Nov 4, 2018
1 parent 298e9e1 commit daf8d1f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Unshaky/ShakyPressPreventer.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@implementation ShakyPressPreventer {
NSTimeInterval lastPressedTimestamps[128];
CGEventType lastPressedEventTypes[128];
CGEventFlags lastEventFlagsAboutModifierKeys[128];
BOOL dismissNextEvent[128];
int keyDelays[128];
BOOL ignoreExternalKeyboard;
Expand Down Expand Up @@ -67,13 +68,17 @@ - (CGEventRef)filterShakyPressEvent:(CGEventRef)event {
// The incoming keycode.
CGKeyCode keyCode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
CGEventType eventType = CGEventGetType(event);
CGEventFlags eventFlagsAboutModifierKeys = (kCGEventFlagMaskShift | kCGEventFlagMaskControl |
kCGEventFlagMaskAlternate | kCGEventFlagMaskCommand |
kCGEventFlagMaskSecondaryFn) & CGEventGetFlags(event);

// ignore unconfigured keys
if (keyDelays[keyCode] == 0) return event;

if (lastPressedTimestamps[keyCode] == 0.0) {
lastPressedTimestamps[keyCode] = [[NSDate date] timeIntervalSince1970];
lastPressedEventTypes[keyCode] = eventType;
lastEventFlagsAboutModifierKeys[keyCode] = eventFlagsAboutModifierKeys;
} else {
if (dismissNextEvent[keyCode]) {
// dismiss the corresponding keyup event
Expand All @@ -84,6 +89,11 @@ - (CGEventRef)filterShakyPressEvent:(CGEventRef)event {
}
if (eventType == kCGEventKeyDown
&& lastPressedEventTypes[keyCode] == kCGEventKeyUp
// Credit to @ghost711:
/** For some users, pressing button when holding CMD, will cause the event to be reported
twice in rapid succession (first without the flag, and then again with it). Unshaky should not
interfere in such case. So I add this following checking */
&& lastEventFlagsAboutModifierKeys[keyCode] == eventFlagsAboutModifierKeys
&& 1000 * ([[NSDate date] timeIntervalSince1970] - lastPressedTimestamps[keyCode]) < keyDelays[keyCode]) {
// dismiss the keydown event if it follows keyup event too soon
NSLog(@"DISMISSING KEYDOWN:%d", keyCode);
Expand All @@ -97,6 +107,7 @@ - (CGEventRef)filterShakyPressEvent:(CGEventRef)event {
}
lastPressedTimestamps[keyCode] = [[NSDate date] timeIntervalSince1970];
lastPressedEventTypes[keyCode] = eventType;
lastEventFlagsAboutModifierKeys[keyCode] = eventFlagsAboutModifierKeys;
}

if (_debugTextView != nil) [self appendToDebugTextView:[NSString stringWithFormat:@"%f\t Key(%d)\t Event(%d)\n", [[NSDate date] timeIntervalSince1970], keyCode, eventType]];
Expand Down

0 comments on commit daf8d1f

Please sign in to comment.