Skip to content

Commit

Permalink
feat: add support for mouse drag with modifier keys
Browse files Browse the repository at this point in the history
  • Loading branch information
socsieng committed Jan 1, 2021
1 parent 7f4c891 commit 32964a7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Sources/SendKeysLib/Commands/CommandExecutor.swift
Expand Up @@ -88,12 +88,14 @@ public class CommandExecutor: CommandExecutorProtocol {
let y2 = Double(command.arguments[3]!)!
let duration: TimeInterval = Double(command.arguments[4]!)!
let button = command.arguments[5]!
let modifiers = command.arguments[6]

try! mouseController.drag(
start: CGPoint(x: x1, y: y1),
end: CGPoint(x: x2, y: y2),
duration: duration,
button: getMouseButton(button: button)
button: getMouseButton(button: button),
flags: modifiers != nil ? try! KeyPresser.getModifierFlags(modifiers!.components(separatedBy: ",")) : []
)
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/SendKeysLib/Commands/MouseDragCommandMatcher.swift
Expand Up @@ -2,7 +2,7 @@ import Foundation

public class MouseDragCommandMatcher: CommandMatcher {
public init() {
super.init(try! NSRegularExpression(pattern: "\\<d:((\\d+),(\\d+),)?(\\d+),(\\d+)(:([\\d.]+))?(:([a-z]+))?\\>"))
super.init(try! NSRegularExpression(pattern: "\\<d:((\\d+),(\\d+),)?(\\d+),(\\d+)(:([\\d.]+))?(:([a-z]+))?(:([a-z,]+))?\\>"))
}

override public func createCommand(_ arguments: [String?]) -> Command {
Expand All @@ -12,14 +12,16 @@ public class MouseDragCommandMatcher: CommandMatcher {
let y2 = arguments[5]
let duration = arguments[7]
let button = arguments[9]
let modifiers = arguments[11]

return Command(.mouseDrag, [
x1 ?? "-1",
y1 ?? "-1",
x2!,
y2!,
duration ?? "0",
button ?? "left"
button ?? "left",
modifiers
])
}
}
4 changes: 2 additions & 2 deletions Sources/SendKeysLib/MouseController.swift
Expand Up @@ -41,14 +41,14 @@ class MouseController {
upEvent?.post(tap: CGEventTapLocation.cghidEventTap)
}

func drag(start: CGPoint, end: CGPoint, duration: TimeInterval, button: CGMouseButton) {
func drag(start: CGPoint, end: CGPoint, duration: TimeInterval, button: CGMouseButton, flags: CGEventFlags) {
let resolvedStart = resolveLocation(start)
let animator = Animator(duration, animationRefreshInterval, { progress in
let location = CGPoint(
x: (Double(end.x - resolvedStart.x) * progress) + Double(resolvedStart.x),
y: (Double(end.y - resolvedStart.y) * progress) + Double(resolvedStart.y)
)
self.setLocation(location)
self.setLocation(location, button: button, flags: flags)
})

var downMouseType = CGEventType.leftMouseDown
Expand Down
32 changes: 25 additions & 7 deletions Tests/SendKeysTests/CommandIteratorTests.swift
Expand Up @@ -195,7 +195,7 @@ final class CommandIteratorTests: XCTestCase {
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0", "left"])
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0", "left", nil])
])
}

Expand All @@ -204,7 +204,16 @@ final class CommandIteratorTests: XCTestCase {
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0", "right"])
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0", "right", nil])
])
}

func testParsesMouseDragWithButtonAndModifiers() throws {
let commands = getCommands(CommandsIterator("<d:1,2,3,4:right:command,shift>"))
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0", "right", "command,shift"])
])
}

Expand All @@ -213,7 +222,7 @@ final class CommandIteratorTests: XCTestCase {
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0.1", "left"])
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0.1", "left", nil])
])
}

Expand All @@ -222,7 +231,16 @@ final class CommandIteratorTests: XCTestCase {
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0.1", "right"])
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0.1", "right", nil])
])
}

func testParsesMouseDragWithDurationAndButtonAndModifier() throws {
let commands = getCommands(CommandsIterator("<d:1,2,3,4:0.1:right:command>"))
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["1", "2", "3", "4", "0.1", "right", "command"])
])
}

Expand All @@ -231,7 +249,7 @@ final class CommandIteratorTests: XCTestCase {
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["-1", "-1", "3", "4", "0", "left"])
Command(CommandType.mouseDrag, ["-1", "-1", "3", "4", "0", "left", nil])
])
}

Expand All @@ -240,7 +258,7 @@ final class CommandIteratorTests: XCTestCase {
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["-1", "-1", "3", "4", "2", "left"])
Command(CommandType.mouseDrag, ["-1", "-1", "3", "4", "2", "left", nil])
])
}

Expand All @@ -249,7 +267,7 @@ final class CommandIteratorTests: XCTestCase {
XCTAssertEqual(
commands,
[
Command(CommandType.mouseDrag, ["-1", "-1", "3", "4", "2", "center"])
Command(CommandType.mouseDrag, ["-1", "-1", "3", "4", "2", "center", nil])
])
}

Expand Down

0 comments on commit 32964a7

Please sign in to comment.