Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AppDelegate.m
- // MouseTracker
- //
- #import <Cocoa/Cocoa.h>
- #import <AppKit/NSAccessibility.h>
- #import "AppDelegate.h"
- #import "UIElementUtilities.h"
- // these are private
- @interface AppDelegate () {
- }
- @property (weak) IBOutlet NSWindow *window;
- @end
- @implementation AppDelegate
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
- {
- if (AXIsProcessTrustedWithOptions(NULL)) {
- // NSLog(@"Trusted I guess");
- }
- if (!AXAPIEnabled()) {
- NSLog(@"Accessibility API is not enabled, go to System Preferences");
- }
- NSLog(@"Creating system wide accessibility object");
- _systemWideAccessibilityObject = AXUIElementCreateSystemWide();
- NSLog(@"%@", CFCopyDescription(_systemWideAccessibilityObject));
- _currentUIElement = NULL;
- // if (timer == nil) ...
- timer = [NSTimer scheduledTimerWithTimeInterval:0.5
- target:self
- selector:@selector(checkMouse:)
- userInfo:nil
- repeats:YES];
- }
- - (void)dealloc {
- [timer invalidate];
- timer = nil;
- if (_systemWideAccessibilityObject)
- CFRelease(_systemWideAccessibilityObject);
- if (_currentUIElement)
- CFRelease (_currentUIElement);
- [super dealloc];
- }
- - (void)checkMouse:(NSTimer *)theTimer
- {
- mouseLocation = [NSEvent mouseLocation];
- // check to see if the mouse even moved
- if (!NSEqualPoints(mouseLocation, lastMouseLocation)) {
- _mouseX.stringValue = [NSString stringWithFormat:@"%.2f", mouseLocation.x];
- _mouseY.stringValue = [NSString stringWithFormat:@"%.2f", mouseLocation.y];
- CGPoint carbonPoint = [UIElementUtilities carbonScreenPointFromCocoaScreenPoint:mouseLocation];
- AXUIElementRef elementUnderCursor = NULL;
- // grab what is under the current mouse location
- accessibilityErrorCode = AXUIElementCopyElementAtPosition(_systemWideAccessibilityObject, carbonPoint.x, carbonPoint.y, &elementUnderCursor);
- NSArray *array = nil; // = [NSArray array];
- AXUIElementCopyAttributeNames(elementUnderCursor, (CFArrayRef *)&array);
- if (accessibilityErrorCode == kAXErrorSuccess) {
- NSLog(@"Accessibility Code: %d", accessibilityErrorCode);
- NSLog(@"%@", CFCopyDescription(elementUnderCursor));
- // print something about the UI element
- } else {
- // we borked
- NSLog(@"Accessibility Error Code: %d", accessibilityErrorCode);
- NSLog(@"Description: %@", CFCopyDescription(elementUnderCursor));
- }
- lastMouseLocation = mouseLocation;
- }
- }
- - (void)applicationWillTerminate:(NSNotification *)aNotification {
- // Insert code here to tear down your application
- }
- /*
- kAXErrorSuccess = 0,
- kAXErrorFailure = -25200,
- kAXErrorIllegalArgument = -25201,
- kAXErrorInvalidUIElement = -25202,
- kAXErrorInvalidUIElementObserver = -25203,
- kAXErrorCannotComplete = -25204,
- kAXErrorAttributeUnsupported = -25205,
- kAXErrorActionUnsupported = -25206,
- kAXErrorNotificationUnsupported = -25207,
- kAXErrorNotImplemented = -25208,
- kAXErrorNotificationAlreadyRegistered = -25209,
- kAXErrorNotificationNotRegistered = -25210,
- kAXErrorAPIDisabled = -25211,
- kAXErrorNoValue = -25212,
- kAXErrorParameterizedAttributeUnsupported = -25213,
- kAXErrorNotEnoughPrecision = -25214
- AXError AXUIElementCopyAttributeNames (AXUIElementRef element, CFArrayRef *names)
- Returns a list of all the attributes supported by element
- */
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement