Skip to content

Commit

Permalink
feat: add option to change animation refresh rate
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
socsieng committed Jan 6, 2021
1 parent f50209a commit 73a2c29
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 20 deletions.
8 changes: 8 additions & 0 deletions Sources/SendKeysLib/Commands/Command.swift
Expand Up @@ -23,6 +23,14 @@ public protocol CommandProtocol {
func equals(_ comparison: Command) -> Bool
}

protocol RequiresKeyPresser {
var keyPresser: KeyPresser? { get set }
}

protocol RequiresMouseController {
var mouseController: MouseController? { get set }
}

public class Command: Equatable, CustomStringConvertible {
public class var commandType: CommandType { return .undefined }

Expand Down
26 changes: 24 additions & 2 deletions Sources/SendKeysLib/Commands/CommandFactory.swift
Expand Up @@ -14,7 +14,29 @@ public class CommandFactory {
DefaultCommand.self
]

public static func create(_ commandType: Command.Type, arguments: [String?]) -> Command {
return commandType.init(arguments: arguments)
let keyPresser: KeyPresser
let mouseController: MouseController

init(keyPresser: KeyPresser, mouseController: MouseController) {
self.keyPresser = keyPresser
self.mouseController = mouseController
}

convenience public init() {
self.init(keyPresser: KeyPresser(), mouseController: MouseController(animationRefreshInterval: 0.01))
}

public func create(_ commandType: Command.Type, arguments: [String?]) -> Command {
let command = commandType.init(arguments: arguments)

if var keyCommand = command as? RequiresKeyPresser {
keyCommand.keyPresser = keyPresser
}

if var mouseCommand = command as? RequiresMouseController {
mouseCommand.mouseController = mouseController
}

return command
}
}
7 changes: 5 additions & 2 deletions Sources/SendKeysLib/Commands/CommandsIterator.swift
Expand Up @@ -4,10 +4,13 @@ public class CommandsIterator: IteratorProtocol {
public typealias Element = Command

let commandString: String
let commandFactory: CommandFactory

var index = 0;

public init(_ commandString: String) {
public init(_ commandString: String, commandFactory: CommandFactory = CommandFactory()) {
self.commandString = commandString
self.commandFactory = commandFactory
}

public func next() -> Element? {
Expand All @@ -20,7 +23,7 @@ public class CommandsIterator: IteratorProtocol {
}
) {
let args = getArguments(commandString, matchResult!)
let command = CommandFactory.create(commandType, arguments: args)
let command = commandFactory.create(commandType, arguments: args)

if matchResult != nil {
let range = Range(matchResult!.range, in: commandString)
Expand Down
13 changes: 11 additions & 2 deletions Sources/SendKeysLib/Commands/CommandsProcessor.swift
Expand Up @@ -5,22 +5,31 @@ public class CommandsProcessor {

let numberFormatter = NumberFormatter()
let commandExecutor: CommandExecutorProtocol
let keyPresser: KeyPresser
let mouseController: MouseController

public init(defaultPause: Double, commandExecutor: CommandExecutorProtocol? = nil) {
init(defaultPause: Double, keyPresser: KeyPresser, mouseController: MouseController, commandExecutor: CommandExecutorProtocol? = nil) {
self.defaultPause = defaultPause
self.commandExecutor = commandExecutor ?? CommandExecutor()
self.keyPresser = keyPresser
self.mouseController = mouseController

numberFormatter.usesSignificantDigits = true
numberFormatter.minimumSignificantDigits = 1
numberFormatter.maximumSignificantDigits = 3
}

convenience public init(defaultPause: Double, commandExecutor: CommandExecutorProtocol? = nil) {
self.init(defaultPause: defaultPause, keyPresser: KeyPresser(), mouseController: MouseController(animationRefreshInterval: 0.01), commandExecutor: commandExecutor)
}

private func getDefaultPauseCommand() -> Command {
return PauseCommand(duration: defaultPause)
}

public func process(_ commandString: String) {
let commands = IteratorSequence(CommandsIterator(commandString))
let commandFactory = CommandFactory(keyPresser: keyPresser, mouseController: mouseController)
let commands = IteratorSequence(CommandsIterator(commandString, commandFactory: commandFactory))
var shouldDefaultPause = false
var shouldIgnoreNextCommand = false

Expand Down
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/Commands/KeyDownCommand.swift
Expand Up @@ -15,6 +15,6 @@ public class KeyDownCommand: KeyPressCommand {
}

public override func execute() throws {
let _ = try! keyPresser.keyDown(key: key!, modifiers: modifiers)
let _ = try! keyPresser!.keyDown(key: key!, modifiers: modifiers)
}
}
6 changes: 3 additions & 3 deletions Sources/SendKeysLib/Commands/KeyPressCommand.swift
@@ -1,6 +1,6 @@
import Foundation

public class KeyPressCommand: Command {
public class KeyPressCommand: Command, RequiresKeyPresser {
public override class var commandType: CommandType { return .keyPress }

private static let _expression = try! NSRegularExpression(pattern: "\\<[ck]:(.|[\\w]+)(:([,\\w⌘^⌥⇧]+))?\\>")
Expand All @@ -9,7 +9,7 @@ public class KeyPressCommand: Command {
var key: String?
var modifiers: [String] = []

let keyPresser = KeyPresser()
var keyPresser: KeyPresser?

override init() {
super.init()
Expand All @@ -30,7 +30,7 @@ public class KeyPressCommand: Command {
}

public override func execute() throws {
try! keyPresser.keyPress(key: key!, modifiers: modifiers)
try! keyPresser!.keyPress(key: key!, modifiers: modifiers)
}

public override func equals(_ comparison: Command) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/Commands/KeyUpCommand.swift
Expand Up @@ -15,6 +15,6 @@ public class KeyUpCommand: KeyPressCommand {
}

public override func execute() throws {
let _ = try! keyPresser.keyUp(key: key!, modifiers: modifiers)
let _ = try! keyPresser!.keyUp(key: key!, modifiers: modifiers)
}
}
6 changes: 3 additions & 3 deletions Sources/SendKeysLib/Commands/MouseClickCommand.swift
@@ -1,6 +1,6 @@
import Foundation

public class MouseClickCommand: Command {
public class MouseClickCommand: Command, RequiresMouseController {
public override class var commandType: CommandType { return .mouseClick }

private static let _expression = try! NSRegularExpression(pattern: "\\<m:([a-z]+)(:([a-z,]+))?(:(\\d+))?\\>")
Expand All @@ -10,7 +10,7 @@ public class MouseClickCommand: Command {
var modifiers: [String] = []
var clicks: Int = 1

let mouseController = MouseController()
var mouseController: MouseController?

override init() {
super.init()
Expand All @@ -33,7 +33,7 @@ public class MouseClickCommand: Command {
}

public override func execute() throws {
try! mouseController.click(
try! mouseController!.click(
nil,
button: getMouseButton(button: button!),
flags: try! KeyPresser.getModifierFlags(modifiers),
Expand Down
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/Commands/MouseDragCommand.swift
Expand Up @@ -34,7 +34,7 @@ public class MouseDragCommand: MouseMoveCommand {
}

public override func execute() throws {
try! mouseController.drag(
try! mouseController!.drag(
start: x1 == nil || y1 == nil ? nil : CGPoint(x: x1!, y: y1!),
end: CGPoint(x: x2, y: y2),
duration: duration,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/Commands/MouseMoveCommand.swift
Expand Up @@ -38,7 +38,7 @@ public class MouseMoveCommand: MouseClickCommand {
}

public override func execute() throws {
mouseController.move(
mouseController!.move(
start: x1 == nil || y1 == nil ? nil : CGPoint(x: x1!, y: y1!),
end: CGPoint(x: x2, y: y2),
duration: duration,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/Commands/MouseScrollCommand.swift
Expand Up @@ -29,7 +29,7 @@ public class MouseScrollCommand: MouseClickCommand {
}

public override func execute() throws {
mouseController.scroll(
mouseController!.scroll(
CGPoint(x: x, y: y),
duration,
flags: try! KeyPresser.getModifierFlags(modifiers)
Expand Down
6 changes: 5 additions & 1 deletion Sources/SendKeysLib/MouseController.swift
Expand Up @@ -6,9 +6,13 @@ class MouseController {
case vertical
}

let animationRefreshInterval: TimeInterval = 0.01
let animationRefreshInterval: TimeInterval
let keyPresser = KeyPresser()

init(animationRefreshInterval: TimeInterval) {
self.animationRefreshInterval = animationRefreshInterval
}

func move(start: CGPoint?, end: CGPoint, duration: TimeInterval, flags: CGEventFlags) {
let resolvedStart = start ?? getLocation()!
let eventSource = CGEventSource(event: nil)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/MousePosition.swift
Expand Up @@ -31,7 +31,7 @@ class MousePosition: ParsableCommand {
}

func printMousePosition() {
let location = MouseController().getLocation()!
let location = MouseController(animationRefreshInterval: 0.01).getLocation()!
print(String(format: "%.0f,%.0f", location.x, location.y))
}

Expand Down
7 changes: 6 additions & 1 deletion Sources/SendKeysLib/Sender.swift
Expand Up @@ -23,6 +23,9 @@ public struct Sender: ParsableCommand {
@Option(name: .shortAndLong, help: "String of characters to send.")
var characters: String?

@Option(help: "Number of seconds between animation updates.")
var animationInterval: Double = 0.01

public init() { }

public mutating func run() throws {
Expand All @@ -32,7 +35,9 @@ public struct Sender: ParsableCommand {
fputs("WARNING: Accessibility preferences must be enabled to use this tool. If running from the terminal, make sure that your terminal app has accessibility permissiions enabled.\n\n", stderr)
}

let commandProcessor = CommandsProcessor(defaultPause: delay)
let keyPresser = KeyPresser()
let mouseController = MouseController(animationRefreshInterval: animationInterval)
let commandProcessor = CommandsProcessor(defaultPause: delay, keyPresser: keyPresser, mouseController: mouseController)
var commandString: String?

if !(inputFile ?? "").isEmpty {
Expand Down

0 comments on commit 73a2c29

Please sign in to comment.