Skip to content

Instantly share code, notes, and snippets.

@jorgenpt
Last active December 20, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgenpt/6067362 to your computer and use it in GitHub Desktop.
Save jorgenpt/6067362 to your computer and use it in GitHub Desktop.
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (assign) CFMachPortRef tap;
@end
@implementation AppDelegate
static const CGEventMask movementMask =
CGEventMaskBit(kCGEventLeftMouseDragged)
| CGEventMaskBit(kCGEventRightMouseDragged)
| CGEventMaskBit(kCGEventMouseMoved);
static const CGEventMask mask =
CGEventMaskBit(kCGEventLeftMouseDown) | CGEventMaskBit(kCGEventLeftMouseUp)
| CGEventMaskBit(kCGEventRightMouseDown) | CGEventMaskBit(kCGEventRightMouseUp)
| CGEventMaskBit(kCGEventOtherMouseDown) | CGEventMaskBit(kCGEventOtherMouseUp)
| CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged)
| CGEventMaskBit(kCGEventMouseMoved);
CGEventRef tapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon);
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[NSThread detachNewThreadSelector:@selector(tapLoop)
toTarget:self
withObject:nil];
}
- (void)tapLoop
{
self.tap = CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
mask,
&tapCallback,
(__bridge void*)self);
CFRunLoopSourceRef theRunLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, self.tap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
theRunLoopSource,
kCFRunLoopCommonModes);
CFRunLoopRun();
CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
theRunLoopSource,
kCFRunLoopCommonModes);
CFRelease(theRunLoopSource);
CFRelease(self.tap);
self.tap = nil;
}
CGEventRef tapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *data)
{
NSWindow *window = [NSApp mainWindow];
switch (type)
{
case kCGEventTapDisabledByTimeout:
case kCGEventTapDisabledByUserInput:
{
CGEventTapEnable([(__bridge AppDelegate*)data tap], true);
return NULL;
}
default:
break;
}
if (![window isKeyWindow])
return event;
NSRect windowRect = [window frame];
CGPoint eventLocation = CGEventGetUnflippedLocation(event);
if (!NSPointInRect(NSPointFromCGPoint(eventLocation), windowRect)) {
CGPoint newLocation = CGEventGetLocation(event);
if (eventLocation.x < NSMinX(windowRect)) {
newLocation.x = NSMinX(windowRect);
} else if (eventLocation.x >= NSMaxX(windowRect)) {
newLocation.x = NSMaxX(windowRect) - 1.0;
}
if (eventLocation.y < NSMinY(windowRect)) {
newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1);
} else if (eventLocation.y >= NSMaxY(windowRect)) {
newLocation.y += (eventLocation.y - NSMaxY(windowRect) + 1);
}
/* For movement events, we generate new event that puts it back in
* bounds. This makes the cursor never appear to leave the window.
* Setting the location of the event before returning it does not
* update where the windowserver draws the cursor.
*/
CGSetLocalEventsSuppressionInterval(0);
CGWarpMouseCursorPosition(newLocation);
CGSetLocalEventsSuppressionInterval(0.25);
if ((CGEventMaskBit(type) & movementMask) == 0) {
/* For click events, we just constrain the event to the window, so
* no other app receives the click event.
*/
CGEventSetLocation(event, newLocation);
}
}
return event;
}
@end
The MIT License (MIT)
Copyright (c) 2013 Jørgen Tjernø
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment