Simulate input/Keyboard: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 25: | Line 25: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Target may be externally created. |
Target may be externally created. |
||
< |
<syntaxhighlight lang="autohotkey">run, cmd /k |
||
WinWait, ahk_class ConsoleWindowClass |
WinWait, ahk_class ConsoleWindowClass |
||
controlsend, ,hello console, ahk_class ConsoleWindowClass</ |
controlsend, ,hello console, ahk_class ConsoleWindowClass</syntaxhighlight> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
Code assumes you're working on a windows box. Run() can use any program, and WinWaitActive() requires the title of the program as it will be when it opens. |
Code assumes you're working on a windows box. Run() can use any program, and WinWaitActive() requires the title of the program as it will be when it opens. |
||
< |
<syntaxhighlight lang="autoit">Run("notepad") |
||
WinWaitActive("Untitled - Notepad") |
WinWaitActive("Untitled - Notepad") |
||
Send("The answer is 42")</ |
Send("The answer is 42")</syntaxhighlight> |
||
It takes user input in variable using "input box" |
It takes user input in variable using "input box" |
||
and displays that in "message box" |
and displays that in "message box" |
||
< |
<syntaxhighlight lang="autoit">$name="type your name here" |
||
$name = InputBox("Name","Your name please ?",$name) |
$name = InputBox("Name","Your name please ?",$name) |
||
MsgBox(0,"Name","Your name is: "&$name)</ |
MsgBox(0,"Name","Your name is: "&$name)</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
EXEC KB executes the text file KB from disk as if you had typed everything that is in the KB text file. MON I will MONitor Input from disk. |
EXEC KB executes the text file KB from disk as if you had typed everything that is in the KB text file. MON I will MONitor Input from disk. |
||
< |
<syntaxhighlight lang="basic"> 100 REM SIMULATE KEYBORD INPUT |
||
110 GOSUB 170"OPEN&WRITE KB |
110 GOSUB 170"OPEN&WRITE KB |
||
120 PRINT "HOME:RUN140"M$"MARK |
120 PRINT "HOME:RUN140"M$"MARK |
||
Line 56: | Line 56: | ||
200 PRINT D$"MON I"M$D$O$; |
200 PRINT D$"MON I"M$D$O$; |
||
210 PRINT A$C$A$"DELETE"A$O$; |
210 PRINT A$C$A$"DELETE"A$O$; |
||
220 PRINT A$"WRITE"F$: RETURN </ |
220 PRINT A$"WRITE"F$: RETURN </syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 68: | Line 68: | ||
Most of the logic here is dedicated to detecting which Commodore model the program is being run on, in order to use the correct addresses for the keyboard input buffer. |
Most of the logic here is dedicated to detecting which Commodore model the program is being run on, in order to use the correct addresses for the keyboard input buffer. |
||
< |
<syntaxhighlight lang="basic">100 : REM SIMULATE KEYBORD INPUT |
||
110 GOSUB 170:REM DETERMINE MODEL/LOCATIONS |
110 GOSUB 170:REM DETERMINE MODEL/LOCATIONS |
||
120 PRINT CHR$(147);"NAME? MARK";CHR$(19); |
120 PRINT CHR$(147);"NAME? MARK";CHR$(19); |
||
Line 84: | Line 84: | ||
240 V=45:GOSUB 260:IF B=58 THEN NC=208:KB=842:RETURN:REM C-128 |
240 V=45:GOSUB 260:IF B=58 THEN NC=208:KB=842:RETURN:REM C-128 |
||
250 PRINT "UNKNOWN MODEL!":END |
250 PRINT "UNKNOWN MODEL!":END |
||
260 A=PEEK(V)+256*PEEK(V+1):B=PEEK(A+4):RETURN</ |
260 A=PEEK(V)+256*PEEK(V+1):B=PEEK(A+4):RETURN</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 102: | Line 102: | ||
gcc -o simkeypress -L/usr/X11R6/lib -lX11 simkeypress.c |
gcc -o simkeypress -L/usr/X11R6/lib -lX11 simkeypress.c |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <X11/Xlib.h> |
#include <X11/Xlib.h> |
||
Line 215: | Line 215: | ||
XCloseDisplay(dpy); |
XCloseDisplay(dpy); |
||
return 1; |
return 1; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="clojure">(import java.awt.Robot) |
||
(import java.awt.event.KeyEvent) |
(import java.awt.event.KeyEvent) |
||
(defn keytype [str] |
(defn keytype [str] |
||
Line 233: | Line 233: | ||
(doto robot |
(doto robot |
||
(.keyPress (int upCh)) |
(.keyPress (int upCh)) |
||
(.keyRelease (int upCh))))))))</ |
(.keyRelease (int upCh))))))))</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 243: | Line 243: | ||
N.B. You may need to execute: 'sudo chmod +0666 /dev/uinput' first if running on Linux. |
N.B. You may need to execute: 'sudo chmod +0666 /dev/uinput' first if running on Linux. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 271: | Line 271: | ||
log.Fatal(err) |
log.Fatal(err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|GUISS}}== |
=={{header|GUISS}}== |
||
< |
<syntaxhighlight lang="guiss">Start,Programs,Accessories,Notepad,Textbox,Type:Hello World[pling]</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 283: | Line 283: | ||
Keystrokes when this function is executed will go to whatever application has focus at the time. Special cases may need to be made for certain symbols, but most of the VK values in KeyEvent map to the ASCII values of characters. |
Keystrokes when this function is executed will go to whatever application has focus at the time. Special cases may need to be made for certain symbols, but most of the VK values in KeyEvent map to the ASCII values of characters. |
||
< |
<syntaxhighlight lang="java5">import java.awt.Robot |
||
public static void type(String str){ |
public static void type(String str){ |
||
Robot robot = new Robot(); |
Robot robot = new Robot(); |
||
Line 298: | Line 298: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.awt.Robot |
import java.awt.Robot |
||
Line 321: | Line 321: | ||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
sendChars("dir") // runs 'dir' command |
sendChars("dir") // runs 'dir' command |
||
}</ |
}</syntaxhighlight> |
||
=={{header|LabVIEW}}== |
=={{header|LabVIEW}}== |
||
Line 329: | Line 329: | ||
This example implements limited capability of simulating input/Keyboard on Windows using standard library only. For better Windows support we can use [https://github.com/khchen/winim Winim] and [https://github.com/juancarlospaco/nim-xdo xdo] for Linux. |
This example implements limited capability of simulating input/Keyboard on Windows using standard library only. For better Windows support we can use [https://github.com/khchen/winim Winim] and [https://github.com/juancarlospaco/nim-xdo xdo] for Linux. |
||
Current example will simulate keyboard input of typing Hello world to the current focused window. |
Current example will simulate keyboard input of typing Hello world to the current focused window. |
||
< |
<syntaxhighlight lang="nim">when defined(windows): |
||
import winlean |
import winlean |
||
else: |
else: |
||
Line 410: | Line 410: | ||
main() |
main() |
||
</syntaxhighlight> |
|||
</lang> |
|||
Compile and run it by (assuming we have GNU C compiler): |
Compile and run it by (assuming we have GNU C compiler): |
||
<pre>nim c -r ourfile.nim</pre> |
<pre>nim c -r ourfile.nim</pre> |
||
Line 423: | Line 423: | ||
ocaml -I +Xlib Xlib.cma keysym.cma send_event.ml |
ocaml -I +Xlib Xlib.cma keysym.cma send_event.ml |
||
< |
<syntaxhighlight lang="ocaml">open Xlib |
||
let () = |
let () = |
||
Line 527: | Line 527: | ||
(* close connection to server *) |
(* close connection to server *) |
||
xCloseDisplay d; |
xCloseDisplay d; |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
Line 533: | Line 533: | ||
Oz' default GUI toolkit is based on Tk. So we can do the same thing as in Tcl: |
Oz' default GUI toolkit is based on Tk. So we can do the same thing as in Tcl: |
||
< |
<syntaxhighlight lang="oz">declare |
||
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']} |
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']} |
||
Entry |
Entry |
||
Line 546: | Line 546: | ||
{Delay 100} |
{Delay 100} |
||
{Tk.send event(generate Entry Key)} |
{Tk.send event(generate Entry Key)} |
||
end</ |
end</syntaxhighlight> |
||
This only works with internal windows. |
This only works with internal windows. |
||
Line 555: | Line 555: | ||
Target may be externally created, but process must be able to open tty/pty for writing. |
Target may be externally created, but process must be able to open tty/pty for writing. |
||
< |
<syntaxhighlight lang="perl">$target = "/dev/pts/51"; |
||
### How to get the correct value for $TIOCSTI is discussed here : http://www.perlmonks.org/?node_id=10920 |
### How to get the correct value for $TIOCSTI is discussed here : http://www.perlmonks.org/?node_id=10920 |
||
$TIOCSTI = 0x5412 ; |
$TIOCSTI = 0x5412 ; |
||
Line 563: | Line 563: | ||
sleep(2); |
sleep(2); |
||
foreach $a ( @c ) { ioctl(TTY,$TIOCSTI,$a); select(undef,undef,undef,0.1);} ; |
foreach $a ( @c ) { ioctl(TTY,$TIOCSTI,$a); select(undef,undef,undef,0.1);} ; |
||
print "DONE\n";</ |
print "DONE\n";</syntaxhighlight> |
||
Line 573: | Line 573: | ||
Target may be externally created. |
Target may be externally created. |
||
< |
<syntaxhighlight lang="perl">SendKeys("Hello, how are you?\n");</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/pGUI}} |
{{libheader|Phix/pGUI}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\Simulate_keyboard_input.exw |
-- demo\rosetta\Simulate_keyboard_input.exw |
||
Line 605: | Line 605: | ||
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 617: | Line 617: | ||
in the following example. Keyboard input is simulated with the function 'enter' |
in the following example. Keyboard input is simulated with the function 'enter' |
||
to fill the login form's name and password fields. |
to fill the login form's name and password fields. |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/http.l" "@lib/scrape.l") |
||
# Connect to the demo app at http://7fach.de/8080 |
# Connect to the demo app at http://7fach.de/8080 |
||
Line 631: | Line 631: | ||
(click "Spare Part") # Click on "Spare Part" article |
(click "Spare Part") # Click on "Spare Part" article |
||
(prinl (value 8)) # Print the price (12.50) |
(prinl (value 8)) # Print the price (12.50) |
||
(click "logout") # Log out</ |
(click "logout") # Log out</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>12.50</pre> |
<pre>12.50</pre> |
||
Line 639: | Line 639: | ||
The Start-Sleep CmdLet must be used because no application loads instantaneously. The -Milliseconds parameter should be |
The Start-Sleep CmdLet must be used because no application loads instantaneously. The -Milliseconds parameter should be |
||
adjusted accordingly for every application. |
adjusted accordingly for every application. |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
Add-Type -AssemblyName Microsoft.VisualBasic |
Add-Type -AssemblyName Microsoft.VisualBasic |
||
Add-Type -AssemblyName System.Windows.Forms |
Add-Type -AssemblyName System.Windows.Forms |
||
Line 646: | Line 646: | ||
[Microsoft.VisualBasic.Interaction]::AppActivate(“Calc”) |
[Microsoft.VisualBasic.Interaction]::AppActivate(“Calc”) |
||
[System.Windows.Forms.SendKeys]::SendWait(“2{ADD}2=”) |
[System.Windows.Forms.SendKeys]::SendWait(“2{ADD}2=”) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
{{libheader|AutoWin}} |
{{libheader|AutoWin}} |
||
< |
<syntaxhighlight lang="purebasic">If AW_WinActivate("Calc") |
||
AW_SendKeys("123+3=") |
AW_SendKeys("123+3=") |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 660: | Line 660: | ||
{{libheader|AutoPy}} |
{{libheader|AutoPy}} |
||
< |
<syntaxhighlight lang="python">import autopy |
||
autopy.key.type_string("Hello, world!") # Prints out "Hello, world!" as quickly as OS will allow. |
autopy.key.type_string("Hello, world!") # Prints out "Hello, world!" as quickly as OS will allow. |
||
autopy.key.type_string("Hello, world!", wpm=60) # Prints out "Hello, world!" at 60 WPM. |
autopy.key.type_string("Hello, world!", wpm=60) # Prints out "Hello, world!" at 60 WPM. |
||
autopy.key.tap(autopy.key.Code.RETURN) |
autopy.key.tap(autopy.key.Code.RETURN) |
||
autopy.key.tap(autopy.key.Code.F1) |
autopy.key.tap(autopy.key.Code.F1) |
||
autopy.key.tap(autopy.key.Code.LEFT_ARROW)</ |
autopy.key.tap(autopy.key.Code.LEFT_ARROW)</syntaxhighlight> |
||
{{works with|Python|2.5+}} |
{{works with|Python|2.5+}} |
||
Line 673: | Line 673: | ||
Target may be externally created. |
Target may be externally created. |
||
< |
<syntaxhighlight lang="python">>>> import pyautogui |
||
>>> pyautogui.typewrite('Hello world!') # prints out "Hello world!" instantly |
>>> pyautogui.typewrite('Hello world!') # prints out "Hello world!" instantly |
||
>>> pyautogui.typewrite('Hello world!', interval=0.25) # prints out "Hello world!" with a quarter second delay after each character |
>>> pyautogui.typewrite('Hello world!', interval=0.25) # prints out "Hello world!" with a quarter second delay after each character |
||
Line 682: | Line 682: | ||
>>> pyautogui.press('left') # press the left arrow key |
>>> pyautogui.press('left') # press the left arrow key |
||
>>> pyautogui.keyUp('shift') # release the shift key |
>>> pyautogui.keyUp('shift') # release the shift key |
||
>>> pyautogui.hotkey('ctrl', 'shift', 'esc')</ |
>>> pyautogui.hotkey('ctrl', 'shift', 'esc')</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket/gui |
||
(define frame (new frame% |
(define frame (new frame% |
||
Line 708: | Line 708: | ||
(send frame show #t) ; Shows the frame with a white canvas inside |
(send frame show #t) ; Shows the frame with a white canvas inside |
||
(send canvas simulate-key (new key-event% (key-code #\k))) ; Sends the simulated key press (with a key-event% instance) |
(send canvas simulate-key (new key-event% (key-code #\k))) ; Sends the simulated key press (with a key-event% instance) |
||
;outputs k</ |
;outputs k</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 715: | Line 715: | ||
Use libxdo bindings to send text / keystrokes to any application that will accept keystrokes from X11. |
Use libxdo bindings to send text / keystrokes to any application that will accept keystrokes from X11. |
||
<lang |
<syntaxhighlight lang="raku" line>use X11::libxdo; |
||
my $xdo = Xdo.new; |
my $xdo = Xdo.new; |
||
Line 745: | Line 745: | ||
sleep .5; |
sleep .5; |
||
$xdo.send-sequence($w, 'Return'); |
$xdo.send-sequence($w, 'Return'); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 751: | Line 751: | ||
{{works with|Personal REXX}} |
{{works with|Personal REXX}} |
||
Note: this REXX program ''only'' works with the above two REXXes. |
Note: this REXX program ''only'' works with the above two REXXes. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm shows how to use the REXX/PC PRESS cmd to simulate keyboard input.*/ |
||
call press 'This text will be put into a buffer as if it came from the keyboard' |
call press 'This text will be put into a buffer as if it came from the keyboard' |
||
/* [↑] text will be available for any program to use (including DOS).*/ |
/* [↑] text will be available for any program to use (including DOS).*/ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight><br><br> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 762: | Line 762: | ||
{{libheader|AutoPilot}} |
{{libheader|AutoPilot}} |
||
< |
<syntaxhighlight lang="rust">extern crate autopilot; |
||
fn main() { |
fn main() { |
||
autopilot::key::type_string("Hello, world!", None, None, &[]); |
autopilot::key::type_string("Hello, world!", None, None, &[]); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
< |
<syntaxhighlight lang="scala">import java.awt.Robot |
||
import java.awt.event.KeyEvent |
import java.awt.event.KeyEvent |
||
Line 794: | Line 794: | ||
} |
} |
||
keystroke(args(0)) |
keystroke(args(0)) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 801: | Line 801: | ||
This only works with windows created by Tk; |
This only works with windows created by Tk; |
||
it sends a single key "x" to the given window. |
it sends a single key "x" to the given window. |
||
< |
<syntaxhighlight lang="tcl">set key "x" |
||
event generate $target <Key-$key></ |
event generate $target <Key-$key></syntaxhighlight> |
||
To send multiple keys, call repeatedly in order. |
To send multiple keys, call repeatedly in order. |
||
Alphabetic keys can be used directly as events, " " has to be mapped to "<space>". |
Alphabetic keys can be used directly as events, " " has to be mapped to "<space>". |
||
< |
<syntaxhighlight lang="tcl">package require Tk |
||
pack [text .t] |
pack [text .t] |
||
focus -force .t |
focus -force .t |
||
foreach c [split "hello world" ""] { |
foreach c [split "hello world" ""] { |
||
event generate .t [expr {$c eq " "?"<space>": $c}] |
event generate .t [expr {$c eq " "?"<space>": $c}] |
||
}</ |
}</syntaxhighlight> |
||
Note also that the task on [[Keyboard macros#Tcl|keyboard macros]] |
Note also that the task on [[Keyboard macros#Tcl|keyboard macros]] |
||
illustrates a very closely related method. |
illustrates a very closely related method. |
||
On Windows simulate pressing the Escape, Backspace etc keys |
On Windows simulate pressing the Escape, Backspace etc keys |
||
< |
<syntaxhighlight lang="tcl">package require twapi |
||
twapi::send_keys ({Esc})</ |
twapi::send_keys ({Esc})</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
The keystrokes are sent to the active window. |
The keystrokes are sent to the active window. |
||
< |
<syntaxhighlight lang="vbscript">Dim WshShell |
||
Set WshShell = WScript.CreateObject("WScript.Shell") |
Set WshShell = WScript.CreateObject("WScript.Shell") |
||
WshShell.SendKeys "{Down}{F2}" |
WshShell.SendKeys "{Down}{F2}" |
||
WScript.Sleep 1000 ' one-second delay |
WScript.Sleep 1000 ' one-second delay |
||
WshShell.SendKeys "{Left}{Left}{BkSp}{BkSp}Some text here.~" ' ~ -> Enter</ |
WshShell.SendKeys "{Left}{Left}{BkSp}{BkSp}Some text here.~" ' ~ -> Enter</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 832: | Line 832: | ||
<br> |
<br> |
||
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task. |
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task. |
||
< |
<syntaxhighlight lang="ecmascript">/* simulate_input_keyboard.wren */ |
||
import "random" for Random |
import "random" for Random |
||
Line 991: | Line 991: | ||
/* close connection to server */ |
/* close connection to server */ |
||
xd.closeDisplay()</ |
xd.closeDisplay()</syntaxhighlight> |
||
<br> |
<br> |
||
We now embed this Wren script in the following C program, compile and run it. |
We now embed this Wren script in the following C program, compile and run it. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 1,327: | Line 1,327: | ||
free(script); |
free(script); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |