I am learning swift and I am trying to make a simple app where I move an object (The default ship) with the keyboard. I am designing an event to represent the left keypress. Since I do not need the mouse to be at a specific place, NSLocation is of no real use to me. The official documentation tells me to return the variable locationInWindow, which is my NSPoint for NSLocation, as undefined.
I am not sure to grasp what they mean by this. I tried to use the _undefined keyword, but then this comes up :
Cannot convert value of type '(@autoclosure () -> String, StaticString, UInt) -> _' to specified type 'NSPoint' (aka 'CGPoint')
Here is my code
var locationInWindow : NSPoint{
return _undefined
}
let movingLeftEvent = NSEvent.keyEvent(with:NSEvent.EventType.keyDown, location: nil, modifierFlags:[], timestamp: [], windowNumber: 0, context: nil , characters: <#String#>, charactersIgnoringModifiers: <#String#>, isARepeat: false, keyCode: <#UInt16#>)
You just need to
addLocalMonitorForEvents
to your view controllerand implement your custom keyDown method:
Sample