Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
pmur002 committed Dec 4, 2016
1 parent 8905adf commit b25f68b
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
13 changes: 13 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Package: xdotool
Type: Package
Title: R interface to xdotool command
Version: 0.1
Date: 2016-11-25
Author: Paul Murrell
Maintainer: Paul Murrell <paul@stat.auckland.ac.nz>
Description: Provides R functions to simulate keyboard and mouse actions on a
Linux desktop (with an X Window manager that is compatible with the
EWMH/NetWM specification). The functions all work by making system calls
to the xdotool command.
License: GPL-3
SystemRequirements: xdotool
14 changes: 14 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export("keystroke",
"keydown",
"keyup",
"typestring",

"mousemove",
"mousemovePolar",
"mousemoveRel",
"mousemoveRelPolar",
"click",
"mousedown",
"mouseup")

59 changes: 59 additions & 0 deletions R/key.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

## Each value of 'key' is a Keysym (e.g., 'a', 'ctrl+a', 'BackSpace')
## 'delay' is in milliseconds
## Can handle multiple values; they are collapsed using " "
keystroke <- function(key, delay=NULL, clearmodifiers=FALSE) {
cmd <- "key "
if (!is.null(delay)) {
cmd <- paste0(cmd, "--delay ", delay, " ")
}
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
cmd <- paste(cmd, paste(key, collapse=" "))
xdotool(cmd)
}

## Should just be single 'key'
keydown <- function(key, clearmodifiers=FALSE) {
cmd <- "keydown "
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
cmd <- paste(cmd, key)
xdotool(cmd)
}

## Should just be single 'key'
keyup <- function(key) {
cmd <- "keydown "
cmd <- paste(cmd, key)
xdotool(cmd)
}

## Can handle multiple values, in which case 'delay' is recycled
typestring <- function(string, delay=NULL, clearmodifiers=FALSE, sep="\n") {
if (!is.null(delay)) {
delay <- rep(pmin(delay, 1000), length.out=length(string))
}
for (i in seq_along(string)) {
cmd <- "type "
if (!is.null(delay)) {
cmd <- paste0(cmd, "--delay ", sprintf("%d", delay[i]), " ")
}
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
## Hack: if the first character is '-', insert a space
## so xdotool does not interpret string as an option
if (grepl("^-", string[i])) {
str <- paste0(" ", string[i])
} else {
str <- string[i]
}
cmd <- paste0(cmd, "'", str, "'")
xdotool(cmd)
}
invisible()
}

73 changes: 73 additions & 0 deletions R/mouse.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

mousemove <- function(x, y, clearmodifiers=FALSE, sync=FALSE) {
cmd <- "mousemove "
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
if (sync) {
cmd <- paste0(cmd, "--sync ")
}
cmd <- paste0(cmd, x, " ", y)
xdotool(cmd)
}

mousemovePolar <- function(theta, distance, clearmodifiers=FALSE, sync=FALSE) {
cmd <- "mousemove "
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
if (sync) {
cmd <- paste0(cmd, "--sync ")
}
cmd <- paste0(cmd, "--polar ", theta, " ", distance)
xdotool(cmd)
}

mousemoveRel <- function(x, y, clearmodifiers=FALSE, sync=FALSE) {
cmd <- "mousemove_relative "
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
if (sync) {
cmd <- paste0(cmd, "--sync ")
}
cmd <- paste0(cmd, x, " ", y)
xdotool(cmd)
}

mousemoveRelPolar <- function(theta, distance, clearmodifiers=FALSE, sync=FALSE) {
cmd <- "mousemove_relative "
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
if (sync) {
cmd <- paste0(cmd, "--sync ")
}
cmd <- paste0(cmd, "--polar ", theta, " ", distance)
xdotool(cmd)
}

mouseAction <- function(action, button, rep, delay, clearmodifiers) {
cmd <- action
cmd <- paste0(cmd, "--repeat ", rep, " ")
if (!is.null(delay)) {
cmd <- paste0(cmd, "--delay ", delay, " ")
}
if (clearmodifiers) {
cmd <- paste0(cmd, "--clearmodifiers ")
}
cmd <- paste0(cmd, button)
xdotool(cmd)
}

click <- function(button, rep=1, delay=NULL, clearmodifiers=FALSE) {
mouseAction("click ", button, rep, delay, clearmodifiers)
}

mousedown <- function(button, rep=1, delay=NULL, clearmodifiers=FALSE) {
mouseAction("mousedown ", button, rep, delay, clearmodifiers)
}

mouseup <- function(button, rep=1, delay=NULL, clearmodifiers=FALSE) {
mouseAction("mouseup ", button, rep, delay, clearmodifiers)
}
5 changes: 5 additions & 0 deletions R/xdotool.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

xdotool <- function(command) {
cmd <- paste0("xdotool ", command)
system(cmd)
}
50 changes: 50 additions & 0 deletions man/key.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
\name{keystroke}
\alias{keystroke}
\alias{keydown}
\alias{keyup}
\alias{typestring}
\title{
Simulate a keystroke
}
\description{
Send one or more keystrokes to the active window.
}
\usage{
keystroke(key, delay = NULL, clearmodifiers = FALSE)
keydown(key, clearmodifiers = FALSE)
keyup(key)
typestring(string, delay = NULL, clearmodifiers = FALSE, sep = "\n")
}
\arguments{
\item{key}{
A character vector describing the keys to type. The key is
interpreted as a Keysym
(e.g., \code{a}, \code{ctrl+a}, \code{BackSpace}).
\code{keystroke} will accept multiple keys, either in a single
character value, separated by spaces, or in separate character values.
}
\item{delay}{
The delay between keystrokes (in milliseconds).
Default (\code{NULL}) means 12 milliseconds.
Maximum value is 1000. A very small delay (e.g., 0) may
not produce all keys.
}
\item{clearmodifiers}{
Logical whether to clear input modifiers (such as the shift key).
}
\item{string}{
A character value that is typed out literally. Can have embedded
spaces and newlines and tabs.
}
\item{sep}{
If multiple character values are provided to \code{typestring},
they are collapsed to a single value using this separator.
}
}
\references{
\url{http://www.semicomplete.com/projects/xdotool/}
}
\author{
Paul Murrell
}
\keyword{ utilities }
68 changes: 68 additions & 0 deletions man/mouse.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
\name{mouse}
\alias{mousemove}
\alias{mousemovePolar}
\alias{mousemoveRel}
\alias{mousemoveRelPolar}
\alias{mousedown}
\alias{mouseup}
\alias{click}
\title{
Move and click the mouse
}
\description{
Reposition the mouse and simulate mouse clicks.
}
\usage{
mousemove(x, y, clearmodifiers = FALSE, sync = FALSE)
mousemovePolar(theta, distance, clearmodifiers = FALSE, sync = FALSE)
mousemoveRel(x, y, clearmodifiers = FALSE, sync = FALSE)
mousemoveRelPolar(theta, distance, clearmodifiers = FALSE, sync = FALSE)
click(button, rep=1, delay=NULL, clearmodifiers=FALSE)
mousedown(button, rep=1, delay=NULL, clearmodifiers=FALSE)
mouseup(button, rep=1, delay=NULL, clearmodifiers=FALSE)
}
\arguments{
\item{x}{
The \code{x} coordinate for the mouse move (for the \code{Rel} version,
relative to the current x coordinate).
}
\item{y}{
The \code{y} coordinate for the mouse move (for the \code{Rel} version,
relative to the current y coordinate).
}
\item{theta}{
The angle for the mouse move (0 is up and positive angles are
clockwise), relative to the centre of the screen (or, for the
\code{Rel} version, relative to the current mouse position).
}
\item{distance}{
The distance for the mouse move, relative to the centre of the
screen (or, for the \code{Rel} version, relative to the current
mouse position).
}
\item{button}{
The mouse button to simulate (typically, 1 is left, 2 is middle,
3 is right, 4 is wheel up, 5 is wheel down).
}
\item{rep}{
The number of times to click.
}
\item{delay}{
The delay between clicks (in milliseconds), if \code{rep} is more
than 1.
}
\item{clearmodifiers}{
Logical whether to clear input modifiers (such as the shift key).
}
\item{sync}{
Whether to wait for the mouse to begin moving (will not necessarily
wait for the mouse to reach its destination).
}
}
\references{
\url{http://www.semicomplete.com/projects/xdotool/}
}
\author{
Paul Murrell
}
\keyword{ utilities }
26 changes: 26 additions & 0 deletions man/xdotool-package.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
\name{xdotool-package}
\alias{xdotool-package}
\alias{xdotool}
\docType{package}
\title{
\packageTitle{xdotool}
}
\description{
\packageDescription{xdotool}
}
\details{
An R interface to the \code{xdotool} command (on Linux).
}
\author{
\packageAuthor{xdotool}

Maintainer: \packageMaintainer{xdotool}
}
\references{
\url{http://www.semicomplete.com/projects/xdotool/}
}
\keyword{ package }
\examples{
mousemove(200, 200)
keystroke("Ctrl")
}

0 comments on commit b25f68b

Please sign in to comment.