Simulate input/Keyboard: Difference between revisions
No edit summary |
(added VBScript; spaced out existing examples; minor edits) |
||
Line 1: | Line 1: | ||
{{task|GUI}}[[Category:Testing]]{{Omit From|Modula-3}} |
{{task|GUI}}[[Category:Testing]]{{Omit From|Modula-3}} |
||
Send simulated keystrokes to a GUI window. You should specify whether the target may be externally created (i.e., if the keystrokes are going to an application other than the application that is creating them). |
Send simulated keystrokes to a GUI window. You should specify whether the target may be externally created (i.e., if the keystrokes are going to an application other than the application that is creating them). |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Target may be externally created. |
|||
<lang AutoHotkey>run, cmd /k |
<lang AutoHotkey>run, cmd /k |
||
WinWait, ahk_class ConsoleWindowClass |
WinWait, ahk_class ConsoleWindowClass |
||
controlsend, ,hello console, ahk_class ConsoleWindowClass</lang> |
controlsend, ,hello console, ahk_class ConsoleWindowClass</lang> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
Line 25: | Line 27: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
This only works with windows created by Tk; it sends a single key "x" to the given window. |
This only works with windows created by Tk; it sends a single key "x" to the given window. |
||
<lang tcl>set key "x" |
<lang tcl>set key "x" |
||
Line 39: | Line 43: | ||
}</lang> |
}</lang> |
||
Note also that the task on [[Keyboard macros#Tcl|keyboard macros]] illustrates a very closely related method. |
Note also that the task on [[Keyboard macros#Tcl|keyboard macros]] illustrates a very closely related method. |
||
=={{header|VBScript}}== |
|||
The keystrokes are sent to the active window. |
|||
<lang vbscript>Dim WshShell |
|||
Set WshShell = WScript.CreateObject("WScript.Shell") |
|||
WshShell.SendKeys "{Down}{F2}" |
|||
WScript.Sleep 1000 ' one-second delay |
|||
WshShell.SendKeys "{Left}{Left}{BkSp}{BkSp}Some text here.~" ' ~ -> Enter</lang> |
|||
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} |
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} |
Revision as of 19:29, 15 January 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Send simulated keystrokes to a GUI window. You should specify whether the target may be externally created (i.e., if the keystrokes are going to an application other than the application that is creating them).
AutoHotkey
Target may be externally created. <lang AutoHotkey>run, cmd /k WinWait, ahk_class ConsoleWindowClass controlsend, ,hello console, ahk_class ConsoleWindowClass</lang>
Java
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. <lang java5>public static void type(String str){
Robot robot = new Robot(); for(char ch:str.toCharArray()){ if(Character.isUpperCase(ch)){ robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress((int)ch); robot.keyRelease((int)ch); robot.keyRelease(KeyEvent.VK_SHIFT); }else{ char upCh = Character.toUpperCase(ch); robot.keyPress((int)upCh); robot.keyRelease((int)upCh); } }
}</lang>
Tcl
This only works with windows created by Tk; it sends a single key "x" to the given window. <lang tcl>set key "x" event generate $target <Key-$key></lang> To send multiple keys, call repeatedly in order. Alphabetic keys can be used directly as events, " " has to be mapped to "<space>". <lang Tcl>package require Tk pack [text .t] focus -force .t foreach c [split "hello world" ""] {
event generate .t [expr {$c eq " "?"<space>": $c}]
}</lang> Note also that the task on keyboard macros illustrates a very closely related method.
VBScript
The keystrokes are sent to the active window.
<lang vbscript>Dim WshShell Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.SendKeys "{Down}{F2}" WScript.Sleep 1000 ' one-second delay WshShell.SendKeys "{Left}{Left}{BkSp}{BkSp}Some text here.~" ' ~ -> Enter</lang>