Code Monkey home page Code Monkey logo

keyboardsimulator's Introduction

KeyboardSimulator

CRAN

KeyboardSimulator is a package for R language that make users simulate keyboard event, cursor movement and mouse click with simple R function.

This package provide 6 functions which are developed in C++:

  • keybd.press() Simulate keyboard key presses. Multiple keys can be pressed simultaneously by using + as separator.
  • keybd.release() Simulate the release of keyboard keys held by keybd.press.
  • keybd.type_string() Type a string with simulated keyboard input.
  • mouse.click() Simulate left and right button of mouse clicks.
  • mouse.release() Simulate the release of mouse button held by mouse.click.
  • mouse.get_cursor() Get current cursor coordinate of screen.
  • mouse.move() Move cursor to specific coordinate of screen.

The supported keys are listed in keyboard_value.


FAQ

Does KeyboardSimulator support operating systems other than Windows?

No, KeyboardSimulator is built on top of the Windows API and is not compatible with other operating systems.

This package doesn't work on my computer! How can I make it work?

Open a GitHub issue and let us know what version of Windows you are using and what keyboard and mouse hardware you have connected.


Package Setup

install.packages("KeyboardSimulator")
library(KeyboardSimulator)

or

if (!require("devtools")) install.packages("devtools")
devtools::install_github("ChiHangChen/KeyboardSimulator")
library(KeyboardSimulator)

Examples

Simulate keyboard event

  • Press single key
keybd.press("Enter")
  • Press multiple keys with + as seperator
keybd.press("Alt+f4")
  • Hold keys with argument hold=TRUE and use keybd.release to release the held key
keybd.press("Alt", hold = TRUE)
keybd.press('Tab')
keybd.release('Alt')

Simulate mouse event

  • Get current location of cursor
mouse.get_cursor()
# Return the x,y coordinate of screen
[1] 960 540
  • Press mouse button
mouse.click()
mouse.click("right")
  • Move cursor to the center of screen on 1080FHD monitor
mouse.move(960, 540)
  • Move cursor to the center of screen on 1080FHD monitor within 3 seconds
mouse.move(960, 540, duration=3)
  • Drag an item to center of screen
mouse.click(button = "left", hold = TRUE)
mouse.move(960, 540)
mouse.release()

Developers

Dependencies

It is highly recommended that you use the RStudio IDE to develop this package. In addition to an R installation, you'll also need to install Rtools and a TeX distribution. TeX Live is a comprehensive distribution that seems to work well.

Once you have cloned this repository, you'll need to download the build package dependencies. These dependencies are managed with packrat and can be restored by running

if (!require(packrat)) install.packages("packrat")
packrat::on()
packrat::restore()

Documentation

The roxygen2 R package is used to manage KeyboardSimulator's documentation. Do not edit any of the files that have a warning comment at the top about manually editing those files!

You can regenerate package documentation with the RStudio shortcut Ctrl+Shift+Enter, or by running

roxygen2::roxygenize()

The project is set up to do this whenever building and testing the package.


Other

keyboardsimulator's People

Contributors

chihangchen avatar jeffkeller87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

keyboardsimulator's Issues

Could help add function get the mousestyle Value?

The function of the below code block run by Visual basic( version 6.0) is get the mouse style value. Could you help to add such function get the mouse style value in the KeyboardSimulator?


Option Explicit

Private Declare Function GetCursorInfo Lib "user32 " (pci As CURSORINFO) As Long

Private Type POINTAPI
    X As Long
    Y As Long
End Type
 
 
Private Type CURSORINFO
  cbSize   As Long
  flags   As Long
  hCursor   As Long
  ptScreenPos   As POINTAPI
End Type
 
 
Private Sub Form_Load()
  Timer1.Enabled = True
  Timer1.Interval = 500
End Sub

Private Sub Timer1_Timer()
  Dim CursorPar     As CURSORINFO
  CursorPar.cbSize = Len(CursorPar)
  GetCursorInfo CursorPar
  Debug.Print CursorPar.hCursor
End Sub

add Fn key

HI, Could help to add Fn key function in next version? Thanks.

How to use KeyboardSimulator in Laptop with Screen closed

I am using KeyboardSimulator to do a task but when I close the Laptopt screen It doesn't work. I have implemented a Loop and It run in R because I receiv the alarm when It finish but the mouse movement and the key press don't work. Is it possible to use if I close my laptop screen? I have proved with the screen saver and it work but not if I close the screen laptop.

Keyboard Commands behave as if "crtl+" were added

Hi,
I run into the following issue on both machines I tried the package with:
While mouse works fine, the keyboard commands work as if "crtl+" were added. So if I say:

keybd.press("a", hold = FALSE)

it selects the whole script (as if "crtl+a" were executed). The same with other keys like "s" (tries to save script), "o", opens explorer to open a script, etc.
I made sure "crtl" is not held. I can

keybd.release('ctrl')

and nothing changes of the above behavior.
Both machines run Windows 10, one Enterprise, one Pro. One is a Dell, the other a Logitech Keyboard, both German-layout. Switching to US keyboard layout in Windows did not do the trick.
Any ideas would be welcome.

any plan to mouse move with time?

I want to add time to the mouse.move() function to make the pointer move slowly. Are there any such improvements planned?

Thank you for making the package.

Screensaver/screen lock prevention

I noticed that the package's function mouse.move() will not prevent the screensaver or the screen lock from becoming active. Is it a bug of feature? For monitoring reasons it would be great to have the option of prevention. Thanks!

Enabling scroll function to scroll up and down

Hi! This is exactly the package I was looking for but it's lacking an essential feature: scrolling.
Would it be viable to add a new mouse.scroll() function?
To give a little more context, the up and down keys don't work to scroll in my use-case.

cannot simulate caracter "@"

Hello, when using the following command:

keybd.press('@')

I get the caracter "é" in the R console with the following warning:

Warning message: In keybd.press("@") : Button '@' will be treated as lowercase. Use 'keybd.type_string' for uppercase keyboard simulation.

Do you have any idea to solve this issue ?
Best regards

Windows 10 sleep/hibernate affecting actions?

Hello,

I have a long running script that will perform some mouse/keyboard actions once every hour or so, basically clicking some buttons in a Windows application to upload some data. I noticed that it will work when I am at my computer observing, but it does not seem to work when the script is left running overnight and I am away. All other aspects of the R script work, but it is almost as if the mouse/keyboard section of the script was never executed because I will check after many hours have gone by and it seems fine, with the exception of the mouse/keyboard actions allegedly not executing.

I have put in a number of Sys.sleep() functions before/after mouse and keyboard actions to ensure the script is not executing moves too fast for the OS, but I continue to observe the same behaviour of working when I am there but seemingly skipped when I am away for long periods of time. I am wondering if there is a known affect on KeyboardSimulator if Windows goes into some kind of sleep/hibernate mode? Will the first action potentially be lost, and do I need to "wake up" the OS somehow first to reliably get mouse/keyboard actions to register with the OS?

Any other tips or thoughts about this issue would be greatly appreciated.

Thanks

R 4.3.X not working?

I am getting this error when trying to use mouse.click in R 4.3.0 and 4.3.1, but everything works fine in 4.2.3

KeyboardSimulator::mouse.click()
Error in .Call("_KeyboardSimulator_mouse_left_click", PACKAGE = "KeyboardSimulator") : "_KeyboardSimulator_mouse_left_click" not available for .Call() for package "KeyboardSimulator"

keybd.type_string() typing error

In my case keybd.type_string() function types "/" as "ö" and "\" as "ç" where backslash is the main character I needed. Any solution or recommendation related to this issue?

Can't install package for some reason

When I try to install the package I get an error saying that it is not available for R version 3.3.3

See my code below:

install.packages("KeyboardSimulator")
Installing package into ‘C:/Users/SchubertK/Documents/R/win-library/3.3’
(as ‘lib’ is unspecified)
Warning in install.packages :
package ‘KeyboardSimulator’ is not available (for R version 3.3.3)

Does not work with Windows Task Scheduler when not logged in

First, thank you for developing this package! It works beautifully when I am using my computer (even if R is not in the foreground). However, the problem I am trying to solve involves running the script every night on a server. I set up a scheduled task using Windows Task Scheduler, but the same script does not work on the server. Any suggestion?

Keys for command line results in "Error, attempt to apply non-function"

I would like to know if it is possible to use keyboardSimulator to enter a command at the command line. From the description of the package, it sounds as it should be able to do it, but I'm stuck with a single command that doesn't want to budge, and stops the execution with an error ("attempt to apply non-function").

To provide some context, I need to do a screenshot of a Viewer pane, for a few files, one at a time, and cannot be hitting 'Enter' continuously. There is a nice procedure for a single screenshot, using websocket, at

   https://stackoverflow.com/questions/50946083/how-to-save-viewer-pane-as-image-through-command-line

(middle of the page in the url, "The Code", reproduced below). If I run those lines in one go, the process reports, "Error, attempt to apply non-function", at the line

   websocket$send("go") # send any message

However, the process works well if I execute in block from the start until the previous line, select the offending line [websocket$send("go")], and press either (a) 'Enter' or (b) "Run" at the top. In these cases it produces the png file perfectly.

I tried to replace 'websocket$send("go")' with

    keybd.type_string('websocket$send(\"go\")'); keybd.press("Enter")

or

    keybd.type_string('websocket$send(\"go\")'); mouse.click(); mouse.release()

but, in these cases, although I don't receive any error, the websocket is not called, pdf continues to be NULL, and no png file is produced (showing another error when it tries to download it).

I'm sure I'm writing something wrong to execute the command line. I'd also like to produce a shorter code but I'm afraid it may not reproduce the exact chain of events (Viewer -> websocket -> png -> download), although I think the issue is with formatting that single line of code correctly.

Can you provide some guidance?

Thank you so much ...

# From https://stackoverflow.com/questions/50946083/how-to-save-viewer-pane-as-image-through-command-line
# Updated two deprecated commands to start and stop the server

library(httpuv)

# Initialize variables
png <- NULL
websocket <- NULL

# Download Javascript libraries
polyfill_promise <- readLines('https://cdn.jsdelivr.net/npm/es6-promise/dist/es6-promise.auto.min.js')
html2canvas <- readLines('https://html2canvas.hertzen.com/dist/html2canvas.min.js')

# Configure the httpuv server
app <- list(
  call = function(req) {
    list(
      status  = 200L,
      headers = list( 'Content-Type' = 'text/html' ),
      body = paste0(collapse = "\r\n",
                    c("<!DOCTYPE html>",
                      "<html>", 
                      "<head>",
                      # polyfill the RStudio viewer to support JavaScript promises
                        '<script type="text/javascript">', polyfill_promise, "</script>",
                      # use html2canvas library
                        '<script type="text/javascript">', html2canvas, "</script>",
                      "</head>",

                      "<body>", html_body, "</body>",
                      '<script type="text/javascript">',
                      # Configure the client-side websocket connection:
                        'var ws = new WebSocket("ws://" + location.host);',
                      # When a websocket message is received:
                        "ws.onmessage = function(event) {",
                      # Take a screenshot of the HTML body element
                        "  html2canvas(document.body).then(function(canvas) {",
                      # Transform it to png
                        "    var dataURL = canvas.toDataURL();",
                      # Send it back to the server
                        "    ws.send(dataURL);",
                        "  });",
                        "};",
                      "</script>",
                      "</html>"
                    )
      )
    )
  },
  # Configure the server-side websocket connection
  onWSOpen = function(ws) {
    # because we need to send websocket message from the R command line:
      websocket <<- ws
    # when a websocket message is received from the client
      ws$onMessage(function(binary, message) {
        png <<- message
    })
  }
)

# From the original question:
html_body <- c(
  '<h1> Content</h1>', 
  '<h2> Content</h2>', 
  'lorem ipsum...'
)

# Start the server:
server <- startServer("0.0.0.0", 9454, app) # deprecated -> startDaemonizedServer("0.0.0.0", 9454, app)

# Open the RStudio viewer:
  rstudioapi::viewer("http://localhost:9454")
# Wait to see the result...

# Send a websocket message from the command line: 
  websocket$send("go") # send any message    # <=========== How to modify this line with keyboardSimulator?

# Write the png image to disk:
writeBin(
  RCurl::base64Decode( gsub("data:image/png;base64,", "", png), "raw" ), 
  "screenshot.png"
)

# Close the websocket connection
  websocket$close()

# Stop the server
  stopServer(server) # deprecated -> stopDaemonizedServer(server)

keybd.type_string not support Chinese.

it's pity keybd.type_string not support Chinese.Could you help improve it ?

keybd.type_string("我爱你")
Error in keybd.type_string("我爱你") :
The 'button' value '我' is not supported. Available keys are : a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0 num0 num1 num2 num3 num4 num5 num6 num7 num8 num9 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 backspace tab enter shift ctrl alt capslock esc pageup pagedown end home left up right down insert del numlock win ` ~ ! @ # $ % ^ & * ( ) - _ + = [ { ] } \ | ; : ' " , < . > / ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.