Getting MouseMoved event in Swift OSX Application

Hey,


I'm trying to get the mouseMoved event fired when I am moving the mouse over my window. But if I override the mouseMoved event to print a debug message nothing is happening. The mouseDown event does work. I read, that I have to set the "acceptsMouseMovedEvents" properties on my window, I did this in viewDidLoad of my ViewController (self.view.window?.acceptsMouseMovedEvents = true) but I dont get the mouseMoved event.


Could someone upload an working example of a window which only displays the mouse coordinates in two labels which updates their values when I move the mouse?


Thanks!

J0nas

Two things:


1. At "viewDidLoad" time, you cannot expect that the view has been added to the window's view hierarchy yet, so "self.view.window" is presumably nil, and your optionally chained assignment will do nothing.


2. The reason the sending of mouseMoved events defaults to false is that it's regarded as inefficient, or at least wasteful, to turn this on permanently. It floods the event queue with events, many of which you will choose to ignore because they're outside the window.

The superior approach is to define a NSTrackingArea (which you can do for the root content view's bounds if you wish). In that case, you get mouseMoved events only when the mouse moves inside the window. Plus, you get mouseEntered and mouseExited events, if those are useful to you.

If you follow this approach, use the "use visible rect" option on the tracking area to avoid having to redefine the tracking area whenever the window size changes.

Hey,


thank you for your answer. Could you show me a code example on how to set my swift application up to get the mouseMoved event fired? I dont know how to use a initialized NSTrackingArea the right way.


Thanks!

J0nas

Hey,


thank you, it worked for me. Could you also explain, how to get the mouse coordinates of the absolute position on the window, please? E. g. my window has a size of 500px x 500px and my mouse cursor is moving somewhere in the middle, then I should get coordinates around X = 250 and Y = 250.


Thanks!

J0nas

If you have the mouseEvent, call

let mousePoint = theEvent.locationInWindow

Hello J0nas. I have the same question. Coming from Java and C#, I am new to Swift. And I am finding it challenging. I learned how to do this in Java, HTML5, JavaScript, Visual Basic and C# in a matter of minutes for each language. There were several examples of entire projects on the web that worked for the current versions. I found some Xcode examples on the web but they were for much earlier versions of Swift. I could not get any of those examples to work. The Apple documention is excellent but I am not yet proficient enough to read it and implement it in working code. The anwers you have got are great. Do you have code that works in Swift 4? If so, could you please share it? For me, so far, graphics in Swift has been anything but swift. I would buy an up to date book that covered these basic topics for the most recent version with complete working sample code projects but I cannot find anything on the web yet. The books for beginners do not cover these kinds of things, and the forums and answers available on the web appear to require a fairly deep understanding of how to read the specifications and adapt code written for Ojective C or older versions of Swift to the current version. Any advice would be appreciated. Thank you.

Getting MouseMoved event in Swift OSX Application
 
 
Q