2017-11-12 1 views
1

Ich muss die Cursorgeschwindigkeit für Präzisionsbewegung der Maus verlangsamen, wenn der Benutzer eine bestimmte Taste gedrückt hält. Gibt es dafür eine API? Ich habe versucht, die Mausposition zu bekommen und seine Position auf die Hälfte zu setzen, aber es funktioniert nicht.Mac-Cursor-Geschwindigkeit programmgesteuert ändern

let mouseLoc = NSEvent.mouseLocation 

// get the delta 
let deltaX = mouseLoc.x - lastX 
let deltaY = mouseLoc.y - lastY 

lastX = mouseLoc.x; lastY = mouseLoc.y 

// add to the current position by half of the real mouse position 
var x = currentMousePos.x + (deltaX/2) 
var y = currentMousePos.y + (deltaY/2) 

// invert the y and set the mouse pos 
CGDisplayMoveCursorToPoint(CGMainDisplayID(), carbonPoint(from: currentMousePos)) 
currentMousePos = NSPoint(x: x, y: y) 

Wie ändern Sie die Cursorgeschwindigkeit?

Ich habe mir Mac Mouse/Trackpad Speed Programmatically angesehen, aber die Funktion ist veraltet.

+0

Ich bin mir bewusst von diesem Post, aber sie verwenden eine veraltete API: [Mac Maus/Trackpad Geschwindigkeit Programmatisch] (https://stackoverflow.com/questions/10448843/mac-mouse-trackpad-speed-programmatically) – fuzzyCap

Antwort

1

Möglicherweise müssen Sie die Cursorposition mit der Maus lösen, Ereignisse behandeln und den Cursor langsamer bewegen.

https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/QuartzDisplayServicesConceptual/Articles/MouseCursor.html

CGDisplayHideCursor (kCGNullDirectDisplay); 
CGAssociateMouseAndMouseCursorPosition (false); 
// ... handle events, move cursor by some scalar of the mouse movement 
// ... using CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, ...); 
CGAssociateMouseAndMouseCursorPosition (true); 
CGDisplayShowCursor (kCGNullDirectDisplay); 
0

Dank Seths answer hier der grundlegende Arbeitscode:

var shiftPressed = false { 
    didSet { 
     if oldValue != shiftPressed { 
      // disassociate the mouse position if the user is holding shift and associate it if not 
      CGAssociateMouseAndMouseCursorPosition(boolean_t(truncating: !shiftPressed as NSNumber)); 
     } 
    } 
} 

// listen for when the mouse moves 
localMouseMonitor = NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) { (event: NSEvent) in 
    self.updateMouse(eventDeltaX: event.deltaX, eventDeltaY: event.deltaY) 
    return event 
} 

func updateMouse(eventDeltaX: CGFloat, eventDeltaY: CGFloat) { 
    let mouseLoc = NSEvent.mouseLocation 
    var x = mouseLoc.x, y = mouseLoc.y 

    // slow the mouse speed when shift is pressed 
    if shiftPressed { 
     let speed: CGFloat = 0.1 
     // set the x and y based off a percentage of the mouse delta 
     x = lastX + (eventDeltaX * speed) 
     y = lastY - (eventDeltaY * speed) 

     // move the mouse to the new position 
     CGDisplayMoveCursorToPoint(CGMainDisplayID(), carbonPoint(from: NSPoint(x: x, y: y))); 
    } 

    lastX = x 
    lastY = y 
}