From 1a8963f464d7b8a391d8f42e94c486c0092358ea Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Fri, 23 May 2025 11:57:03 +0100 Subject: [PATCH] mouse tracking --- DockPhobia.xcodeproj/project.pbxproj | 2 + DockPhobia/AppDelegate.swift | 9 ++--- DockPhobia/MouseTracker.swift | 57 ++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/DockPhobia.xcodeproj/project.pbxproj b/DockPhobia.xcodeproj/project.pbxproj index 9161c47..198b1ba 100644 --- a/DockPhobia.xcodeproj/project.pbxproj +++ b/DockPhobia.xcodeproj/project.pbxproj @@ -292,6 +292,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); + MACOSX_DEPLOYMENT_TARGET = 10.10; MARKETING_VERSION = 1.0; PRODUCT_BUNDLE_IDENTIFIER = com.neon443.DockPhobia; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -320,6 +321,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); + MACOSX_DEPLOYMENT_TARGET = 10.10; MARKETING_VERSION = 1.0; PRODUCT_BUNDLE_IDENTIFIER = com.neon443.DockPhobia; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/DockPhobia/AppDelegate.swift b/DockPhobia/AppDelegate.swift index b51fdd0..f577618 100644 --- a/DockPhobia/AppDelegate.swift +++ b/DockPhobia/AppDelegate.swift @@ -6,19 +6,19 @@ // import Cocoa +import AppKit @main class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet var window: NSWindow! + var mouseTracker = MouseTracker() + func applicationDidFinishLaunching(_ aNotification: Notification) { // Insert code here to initialize your application - while true { - printMouse() - sleep(1) - } + mouseTracker.addMonitor() } func applicationWillTerminate(_ aNotification: Notification) { @@ -29,4 +29,3 @@ class AppDelegate: NSObject, NSApplicationDelegate { return true } } - diff --git a/DockPhobia/MouseTracker.swift b/DockPhobia/MouseTracker.swift index 9332382..f84d350 100644 --- a/DockPhobia/MouseTracker.swift +++ b/DockPhobia/MouseTracker.swift @@ -8,9 +8,58 @@ import Foundation import AppKit import Cocoa +import CoreGraphics -func printMouse() { - let mouseLoc: NSPoint - mouseLoc = NSEvent.mouseLocation - print(mouseLoc) +struct Screen { + var width: CGFloat + var height: CGFloat } + +class MouseTracker { + var screen: Screen + + var monitor: NSEvent? + + init() { + if let screen = NSScreen.main { + let rect = screen.frame + self.screen = Screen( + width: rect.width, + height: rect.height + ) + print(self.screen) + } else { + fatalError("no screen wtf???") + } + addMonitor() + } + + func checkMouse(_ event: NSEvent) { + var location = event.locationInWindow + print(location) + } + + func addMonitor() { + self.monitor = NSEvent.addGlobalMonitorForEvents(matching: .mouseMoved, handler: checkMouse) as? NSEvent + } + + func removeMonitor() { + NSEvent.removeMonitor(monitor as Any) + } +} + + +/*- (void) startEventTap { + //eventTap is an ivar on this class of type CFMachPortRef + eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly, kCGEventMaskForAllEvents, myCGEventCallback, NULL); + CGEventTapEnable(eventTap, true); +} + +CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { + if (type == kCGEventMouseMoved) { + NSLog(@"%@", NSStringFromPoint([NSEvent mouseLocation])); + } + + return event; +} +*/