2015-08-11 18 views
7

Người mới sử dụng Swift tại đây.Bắt tọa độ chuột trong Swift

Tôi đã gặp sự cố với một tác vụ không đáng kể. Tất cả những gì tôi muốn làm là lấy tọa độ x, y của con trỏ chuột theo yêu cầu. Tôi thích không phải để chờ sự kiện di chuyển chuột kích hoạt trước khi tôi có thể lấy coords của con trỏ.

Sẽ đánh giá cao bất kỳ trợ giúp nào!

Trả lời

15

Bạn nên xem xét phương pháp NSEvent mouseLocation

chỉnh sửa/cập nhật: Xcode 8.2.1 • Swift 3.0.2

Nếu bạn muốn theo dõi các sự kiện trên bất kỳ cửa sổ khi ứng dụng của bạn hoạt động, bạn có thể thêm một LocalMonitorForEvents phù hợp với mouseMoved mặt nạ và nếu nó không hoạt động một GlobalMonitorForEvents:

class ViewController: NSViewController { 
    lazy var window: NSWindow = self.view.window! 
    var mouseLocation: NSPoint { 
     return NSEvent.mouseLocation 
    } 
    var location: NSPoint { 
     return window.mouseLocationOutsideOfEventStream 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) { 
      print("mouseLocation:", String(format: "%.1f, %.1f", self.mouseLocation.x, self.mouseLocation.y)) 
      print("windowLocation:", String(format: "%.1f, %.1f", self.location.x, self.location.y)) 
      return $0 
     } 
     NSEvent.addGlobalMonitorForEvents(matching: [.mouseMoved]) { _ in 
      self.mouseLocation = NSEvent.mouseLocation() 
      print(String(format: "%.0f, %.0f", self.mouseLocation.x, self.mouseLocation.y)) 
     } 
    } 
} 

Swift 2

import Cocoa 
@NSApplicationMain 

class AppDelegate: NSObject, NSApplicationDelegate { 

    @IBOutlet weak var window: NSWindow! 

    var mouseLocation: NSPoint { 
     return NSEvent.mouseLocation() 
    } 

    func applicationDidFinishLaunching(aNotification: NSNotification) { 
     // Insert code here to initialize your application 
     println("Mouse Location X,Y = \(mouseLocation)") 
     println("Mouse Location X = \(mouseLocation.x)") 
     println("Mouse Location Y = \(mouseLocation.y)") 
    } 

    func applicationWillTerminate(aNotification: NSNotification) { 
     // Insert code here to tear down your application 
    } 
} 

Nếu bạn muốn theo dõi vị trí của chuột bạn sẽ cần phải tạo ra một cửa sổ tùy chỉnh và ghi đè sự kiện mouseMoved như sau:

class CustomWindow: NSWindow { 
    var mouseLocation: NSPoint { 
     return NSEvent.mouseLocation() 
    } 
    override func mouseMoved(theEvent: NSEvent) { 
     println("Mouse Location X,Y = \(mouseLocation)") 
     println("Mouse Location X = \(mouseLocation.x)") 
     println("Mouse Location Y = \(mouseLocation.y)") 
    } 
} 

lưu ý: bạn cần thiết lập để bạn window property acceptMouseMovedEvents thành true.

func applicationDidFinishLaunching(aNotification: NSNotification) { 
    window.acceptsMouseMovedEvents = true 
} 
+2

Cảm ơn câu trả lời tuyệt vời. Hoạt động hoàn hảo. NSEvent là một mỏ vàng. :) – MattY

+0

@ MattY bạn được chào đón –

+1

Giải pháp tuyệt vời @Leo! Tuy nhiên, tôi không thể tìm hiểu cách nhận 'mouseLocation' nếu vị trí của nó nằm ngoài cửa sổ tùy chỉnh của tôi. Tôi muốn đạt được một số loại bộ chọn màu và muốn vẽ chế độ xem tùy chỉnh bên cạnh con trỏ. Sẽ tuyệt vời nếu bạn có thể dẫn tôi đi đúng hướng! – ixany

Các vấn đề liên quan