Macos 模拟人鼠运动
嗨,我目前正在尝试创建一个程序,以一个平滑的随机运动将光标从一个给定点移动到另一个点。我目前已经使用CoreGraphics创建了以下内容,它可以工作,但是鼠标移动变得非常不稳定。有没有办法解决这个问题?非常感谢。在我的Mac OS X应用程序启动时,我在ApplicationIDFinishLaunching内调用以下命令:Macos 模拟人鼠运动,macos,swift,cocoa,Macos,Swift,Cocoa,嗨,我目前正在尝试创建一个程序,以一个平滑的随机运动将光标从一个给定点移动到另一个点。我目前已经使用CoreGraphics创建了以下内容,它可以工作,但是鼠标移动变得非常不稳定。有没有办法解决这个问题?非常感谢。在我的Mac OS X应用程序启动时,我在ApplicationIDFinishLaunching内调用以下命令: var pos = NSEvent.mouseLocation() pos.y = NSScreen.mainScreen()!.frame.height - pos.y
var pos = NSEvent.mouseLocation()
pos.y = NSScreen.mainScreen()!.frame.height - pos.y
moveMouse(CGPoint(x:200,y:200), from: pos)
这些是我创建的函数:
func transMouse(point:CGPoint) {
let move = CGEventCreateMouseEvent(nil, CGEventType.MouseMoved, point, CGMouseButton.Left)
CGEventPost(CGEventTapLocation.CGHIDEventTap, move)
}
func moveMouseOne(direction:Character, _ currentPos:CGPoint) -> CGPoint {
var newPos = currentPos
if direction == "r" {
newPos.x = currentPos.x + 1
} else if direction == "l" {
newPos.x = currentPos.x - 1
} else if direction == "u" {
newPos.y = currentPos.y - 1
} else if direction == "d" {
newPos.y = currentPos.y + 1
}
transMouse(newPos)
return newPos
}
func moveMouse(to:CGPoint, from:CGPoint) -> CGPoint {
let dx:Int = Int(to.x - from.x)
let dy:Int = Int(to.y - from.y)
var moves = Array<Character>()
if dx > 0 {
for _ in 0..<dx {
moves.append("r")
}
} else {
for _ in 0..<(-dx) {
moves.append("l")
}
}
if dy > 0 {
for _ in 0..<dy {
moves.append("d")
}
} else {
for _ in 0..<(-dy) {
moves.append("u")
}
}
var pos = from
let delay:Double = 0.0008
let startTime = DISPATCH_TIME_NOW
for var i = 0; i < moves.count; ++i {
let time = dispatch_time(startTime, Int64(delay * Double(i) * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue()) {
let count = moves.count
let random = Int(arc4random_uniform(UInt32(count)))
pos = self.moveMouseOne(moves[random], pos)
if random == count - 1 {
moves.popLast()
} else {
moves[random] = moves.popLast()!
}
}
}
return to
}
func传输(点:CGPoint){
让move=CGEventCreateMouseEvent(nil,CGEventType.MouseMoved,point,CGMouseButton.Left)
CGEventPost(CGEventTapLocation.cgidEventTap,移动)
}
func moveMouseOne(方向:字符,uCurrentPos:CGPoint)->CGPoint{
var newPos=currentPos
如果方向==“r”{
新位置x=当前位置x+1
}如果方向==“l”,则为else{
新位置x=当前位置x-1
}如果方向=“u”,则为else{
新位置y=当前位置y-1
}如果方向==“d”,则为else{
新位置y=当前位置y+1
}
transMouse(新位置)
返回新位置
}
func移动鼠标(到:CGPoint,从:CGPoint)->CGPoint{
设dx:Int=Int(to.x-from.x)
设dy:Int=Int(to.y-from.y)
var=Array()
如果dx>0{
对于uu in 0..我真的建议使用核心动画进行类似的操作,这将节省您大量的时间和精力
我不知道如何使用CoreAnimation操作鼠标,你能解释更多吗?也许我误解了-你想通过编程移动实际的鼠标光标吗?还是想移动其他东西?是的,我想移动实际的鼠标光标。但就我所知,CoreAnimation无法做到这一点?是的p-抱歉-我认为核心动画只能为视图设置动画