2017-11-12 16 views
1

ユーザーが特定のキーを押したままにすると、マウスの正確な動きのためにカーソルの速度を遅くする必要があります。これを行うためのAPIはありますか?私はマウスの位置を取得しようとし、その半分に位置を設定しましたが、動作しません。プログラムでMacカーソルの速度を変更する

どのようにカーソルの速度を変更しますか?

私はMac Mouse/Trackpad Speed Programmaticallyを見ましたが、この関数は推奨されていません。

+0

私はこの記事の承知しているが、彼らは推奨されないAPI使用している:[Macのマウス/トラックパッドの速度をプログラムで](https://stackoverflow.com/questions/10448843/mac-mouse-trackpad-speed-programmatically) – fuzzyCap

答えて

1

カーソルの位置をマウスで切り離したり、イベントを処理したり、遅い速度でカーソルを移動する必要が生じる場合があります。

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

ここseths answerのおかげで、基本的な作業コードです:

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 
} 
関連する問題