Quartz Event Taps and Touch Bar
Quartz Event Taps and Touch Bar
- Subject: Quartz Event Taps and Touch Bar
- From: Bill Cheeseman <email@hidden>
- Date: Fri, 21 Apr 2017 09:56:59 -0400
Here's the second half of the AppDelegate.m file referenced in my previous two messages:
- (void)logEvent:(NSEvent *)event {
// Log interesting information about the event. Called by the myCGEventCallback() function.
// Get the description and target PID of the Touch Bar event.
int64_t targetPID = CGEventGetIntegerValueField([event CGEvent], kCGEventTargetUnixProcessID);
NSString *targetAppName = [[NSRunningApplication runningApplicationWithProcessIdentifier:(pid_t)targetPID] localizedName];
int64_t userData = CGEventGetIntegerValueField([event CGEvent], kCGEventSourceUserData); // just to see if it's of interest
// Log the event information of interest.
NSLog(@" -- \nevent type: %lu\nevent: %@\ntargetPID: %lld (%@)\nuser data: %lld", (unsigned long)[event type], event, targetPID, targetAppName, userData);
// Get the touches associated with the Touch Bar event and relevant information about them. From Apple's technical reference document: "An instance of the NSTouch class is a snapshot of a particular touch at an instant in time."
NSSet *touches = [event allTouches];
NSLog(@"touches: %@", touches);
for (NSTouch *thisTouch in touches) {
// Identity is useful only to compare touches; use -isEqual: to test whether different phases are in the same touch event.
NSLog(@"This touch: %@", thisTouch);
id touchIdentity = [thisTouch identity];
NSLog(@"identity: %@", touchIdentity);
NSLog(@"identity class: %@", NSStringFromClass([touchIdentity class]));
// Phase.
NSTouchPhase touchPhase = [thisTouch phase];
NSLog(@"phase: %lu", (unsigned long)touchPhase);
// Normalized position. From Apple's macOS Sierra 10.12.2 AppKit Release Notes: "Direct touches do not have a normalizedPosition in the digitizer space. Instead their position is in view coordinates.... AppKit will throw an exception if you attempt to acquire the wrong type of position for the type of touch."
// NSPoint touchPoint = [thisTouch normalizedPosition]; // this will crash if uncommented
// NSLog(@"normalized position: %@", NSStringFromPoint(touchPoint));
// Location. From the NSTouch.h header file: "These two methods are only valid for Direct touches. A nil view means the touch location in the root container of touch." Experimentation shows that the "root container of Touch" means the Touch Bar device. The y-coordinate is not meaningful; use the x-coordinate to locate the distance of the touch (at 72 dpi) from the left end of the Touch Bar. The width of the deviceSize value is the maximum horizontal size of the Touch Bar.
NSPoint touchLocation = [thisTouch locationInView:nil];
NSPoint touchPreviousLocation = [thisTouch previousLocationInView:nil];
NSLog(@"location: %@; previous location: %@", NSStringFromPoint(touchLocation), NSStringFromPoint(touchPreviousLocation));
// Is resting.
NSString *touchResting = [thisTouch isResting] ? @"YES" : @"NO";
NSLog(@"is resting: %@", touchResting);
// Device.
id touchDevice = [thisTouch device];
NSLog(@"device: %@", touchDevice);
// Device size. This is reported as {1085, 30} on the 15-inch MacBook Pro (Lated 2016) with Touch Bar. I do not know whether the 13-inch MacBook Pro's Touch Bar is shorter.
NSSize touchDeviceSize = [thisTouch deviceSize];
NSLog(@"device size: %@", NSStringFromSize(touchDeviceSize));
// Type
NSTouchType touchType = [thisTouch type]; // 0 is direct (Touch Bar), 1 is indirect (trackpad)
NSLog(@"type: %ld", (long)touchType);
}
}
@end
--
Bill Cheeseman - email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden