Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Unshaky/ShakyPressPreventer.m #18

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions Unshaky/ShakyPressPreventer.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,69 @@ - (void)loadIgnoreExternalKeyboard {

- (CGEventRef)filterShakyPressEvent:(CGEventRef)event {

// The incoming keycode.
CGKeyCode keyCode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);

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

// keyboard type, dismiss if it is not built-in keyboard
if (ignoreExternalKeyboard) {
int64_t type = CGEventGetIntegerValueField(event, kCGKeyboardEventKeyboardType);
if (type != 58) return event;
}

// The incoming keycode.
CGKeyCode keyCode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
double stamp = [[NSDate date] timeIntervalSince1970];
double lastStamp = lastPressedTimestamps[keyCode];
CGEventType eventType = CGEventGetType(event);

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

if (lastPressedTimestamps[keyCode] == 0.0) {
lastPressedTimestamps[keyCode] = [[NSDate date] timeIntervalSince1970];
lastPressedEventTypes[keyCode] = eventType;
} else {
if (lastStamp != 0.0) {
if (dismissNextEvent[keyCode]) {
// dismiss the corresponding keyup event
NSLog(@"DISMISSING KEYUP:%d", keyCode);
if (_debugTextView != nil) [self appendToDebugTextView:[NSString stringWithFormat:@"%f\t Key(%d)\t Event(%d) DISMISSED\n", [[NSDate date] timeIntervalSince1970], keyCode, eventType]];
if (_debugTextView != nil) [self appendToDebugTextView:[NSString stringWithFormat:@"%f\t Key(%d)\t Event(%d) DISMISSED\n", stamp, keyCode, eventType]];
dismissNextEvent[keyCode] = NO;
return nil;
}
if (eventType == kCGEventKeyDown
&& lastPressedEventTypes[keyCode] == kCGEventKeyUp
&& 1000 * ([[NSDate date] timeIntervalSince1970] - lastPressedTimestamps[keyCode]) < keyDelays[keyCode]) {
if (eventType == kCGEventKeyDown && lastPressedEventTypes[keyCode] == kCGEventKeyUp ) {
// dismiss the keydown event if it follows keyup event too soon
NSLog(@"DISMISSING KEYDOWN:%d", keyCode);
if (_debugTextView != nil) [self appendToDebugTextView:[NSString stringWithFormat:@"%f\t Key(%d)\t Event(%d) DISMISSED\n", [[NSDate date] timeIntervalSince1970], keyCode, eventType]];

if (shakyPressDismissedHandler != nil) {
shakyPressDismissedHandler();
if( (keyCode == 49) && (CGEventGetFlags(event) & kCGEventFlagMaskCommand) ) {
/** CMD+Space was pressed, which causes a duplicate pair of down/up
keyEvents to occur 1-5 msecs after the "real" pair of events.
- If the CMD key is released first, it will look like:
CMD+Space Down
Space Up
CMD+Space Down
CMD+Space Up
- Whereas if the space bar is released first, it will be:
CMD+Space Down
CMD+Space Up
CMD+Space Down
CMD+Space Up
- The issue only appears to happen with CMD+Space,
not CMD+<any other key>, or <any other modifier key>+Space.
- So it looks like the simplest thing is to just ignore if both
CMD and space are pressed at the same time. */
NSLog(@"%f\t Key(%d)\t Event(%d) NOT DISMISSING DOWN (CMD Pressed)\n",
stamp, keyCode, eventType);

} else if( (int)(1000.0 * (stamp - lastStamp)) < keyDelays[keyCode] ) {
NSLog(@"DISMISSING KEYDOWN:%d", keyCode);
if (_debugTextView != nil) [self appendToDebugTextView:[NSString stringWithFormat:@"%f\t Key(%d)\t Event(%d) DISMISSED\n", stamp, keyCode, eventType]];

if (shakyPressDismissedHandler != nil) {
shakyPressDismissedHandler();
}
dismissNextEvent[keyCode] = YES;
return nil;
}
dismissNextEvent[keyCode] = YES;
return nil;
}
lastPressedTimestamps[keyCode] = [[NSDate date] timeIntervalSince1970];
lastPressedEventTypes[keyCode] = eventType;
}
lastPressedTimestamps[keyCode] = stamp;
lastPressedEventTypes[keyCode] = eventType;

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

Expand Down