Skip to content

Commit

Permalink
Handle modifier keys (ctrl, opt, cmd, shift and fn) #8
Browse files Browse the repository at this point in the history
  • Loading branch information
aahung committed Aug 10, 2018
1 parent 89e5824 commit 8c356fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Unshaky/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.2.2</string>
<string>0.2.3</string>
<key>CFBundleVersion</key>
<string>7</string>
<string>8</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
1 change: 1 addition & 0 deletions Unshaky/PreferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class PreferenceViewController: NSViewController,
117: "ForwardDelete",
52: "Linefeed",
53: "Escape",
54: "RightCommand",
55: "Command",
56: "Shift",
57: "CapsLock",
Expand Down
29 changes: 28 additions & 1 deletion Unshaky/ShakyPressPreventer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8c356fa

Please sign in to comment.