以编程方式更改 Mac 光标速度
Change Mac cursor speed programmatically
如果用户按住特定键,我需要减慢光标速度以精确移动鼠标。这样做有 API 吗?我试图获取鼠标位置并将其位置设置为该位置的一半,但它不起作用。
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)
如何改变光标速度?
我看过 Mac Mouse/Trackpad Speed Programmatically 但该函数已弃用。
您可能需要取消光标位置与鼠标的关联,处理事件,并以较慢的速度自行移动光标。
CGDisplayHideCursor (kCGNullDirectDisplay);
CGAssociateMouseAndMouseCursorPosition (false);
// ... handle events, move cursor by some scalar of the mouse movement
// ... using CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, ...);
CGAssociateMouseAndMouseCursorPosition (true);
CGDisplayShowCursor (kCGNullDirectDisplay);
感谢 seths 这是基本的工作代码:
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
}
如果用户按住特定键,我需要减慢光标速度以精确移动鼠标。这样做有 API 吗?我试图获取鼠标位置并将其位置设置为该位置的一半,但它不起作用。
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)
如何改变光标速度?
我看过 Mac Mouse/Trackpad Speed Programmatically 但该函数已弃用。
您可能需要取消光标位置与鼠标的关联,处理事件,并以较慢的速度自行移动光标。
CGDisplayHideCursor (kCGNullDirectDisplay);
CGAssociateMouseAndMouseCursorPosition (false);
// ... handle events, move cursor by some scalar of the mouse movement
// ... using CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, ...);
CGAssociateMouseAndMouseCursorPosition (true);
CGDisplayShowCursor (kCGNullDirectDisplay);
感谢 seths
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
}