Skip to content

Commit

Permalink
fix: support negative values for mouse click and drag events
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
socsieng committed Jan 6, 2021
1 parent 3893313 commit f50209a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/Commands/MouseDragCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public class MouseDragCommand: MouseMoveCommand {
public override class var commandType: CommandType { return .mouseDrag }

private static let _expression = try! NSRegularExpression(pattern: "\\<d:((\\d+),(\\d+),)?(\\d+),(\\d+)(:([\\d.]+))?(:([a-z]+))?(:([a-z,]+))?\\>")
private static let _expression = try! NSRegularExpression(pattern: "\\<d:((-?\\d+),(-?\\d+),)?(-?\\d+),(-?\\d+)(:([\\d.]+))?(:([a-z]+))?(:([a-z,]+))?\\>")
public override class var expression: NSRegularExpression { return _expression }

public init(x1: Int?, y1: Int?, x2: Int, y2: Int, duration: TimeInterval, button: String?, modifiers: [String]) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/Commands/MouseMoveCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public class MouseMoveCommand: MouseClickCommand {
public override class var commandType: CommandType { return .mouseMove }

private static let _expression = try! NSRegularExpression(pattern: "\\<m:((\\d+),(\\d+),)?(\\d+),(\\d+)(:([\\d.]+))?(:([a-z,]+))?\\>")
private static let _expression = try! NSRegularExpression(pattern: "\\<m:((-?\\d+),(-?\\d+),)?(-?\\d+),(-?\\d+)(:([\\d.]+))?(:([a-z,]+))?\\>")
public override class var expression: NSRegularExpression { return _expression }

var x1: Int?
Expand Down
18 changes: 18 additions & 0 deletions Tests/SendKeysTests/CommandIteratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ final class CommandIteratorTests: XCTestCase {
])
}

func testParsesMouseMoveWithNegativeCoordinates() throws {
let commands = getCommands(CommandsIterator("<m:-1,-2,-3,-4:0.1>"))
XCTAssertEqual(
commands,
[
MouseMoveCommand(x1: -1, y1: -2, x2: -3, y2: -4, duration: 0.1, modifiers: [])
])
}

func testParsesMouseMoveWithDurationAndModifiers() throws {
let commands = getCommands(CommandsIterator("<m:1,2,3,4:0.1:shift,command>"))
XCTAssertEqual(
Expand Down Expand Up @@ -298,6 +307,15 @@ final class CommandIteratorTests: XCTestCase {
])
}

func testParsesMouseDragWithDurationWithNegativeCoordinates() throws {
let commands = getCommands(CommandsIterator("<d:-1,-2,-3,-4:0.1>"))
XCTAssertEqual(
commands,
[
MouseDragCommand(x1: -1, y1: -2, x2: -3, y2: -4, duration: 0.1, button: "left", modifiers: [])
])
}

func testParsesMouseDragWithDurationAndButton() throws {
let commands = getCommands(CommandsIterator("<d:1,2,3,4:0.1:right>"))
XCTAssertEqual(
Expand Down

0 comments on commit f50209a

Please sign in to comment.