Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Scripting
Windows Script Host
 SendKeys Method

  Switch on low bandwidth view
This page is specific to
.NET Framework 3.0

Other versions are also available for the following:
Windows Script Host
SendKeys Method

Sends one or more keystrokes to the active window (as if typed on the keyboard).

object.SendKeys(string)
object

WshShell object.

string

String value indicating the keystroke(s) you want to send.

Use the SendKeys method to send keystrokes to applications that have no automation interface. Most keyboard characters are represented by a single keystroke. Some keyboard characters are made up of combinations of keystrokes (CTRL+SHIFT+HOME, for example). To send a single keyboard character, send the character itself as the string argument. For example, to send the letter x, send the string argument "x".

NoteNote:

To send a space, send the string " ".

You can use SendKeys to send more than one keystroke at a time. To do this, create a compound string argument that represents a sequence of keystrokes by appending each keystroke in the sequence to the one before it. For example, to send the keystrokes a, b, and c, you would send the string argument "abc". The SendKeys method uses some characters as modifiers of characters (instead of using their face-values). This set of special characters consists of parentheses, brackets, braces, and the:

  • plus sign       "+",

  • caret             "^",

  • percent sign "%",

  • and tilde       "~"

Send these characters by enclosing them within braces "{}". For example, to send the plus sign, send the string argument "{+}". Brackets "[ ]" have no special meaning when used with SendKeys, but you must enclose them within braces to accommodate applications that do give them a special meaning (for dynamic data exchange (DDE) for example).

  • To send bracket characters, send the string argument "{[}" for the left bracket and "{]}" for the right one.

  • To send brace characters, send the string argument "{{}" for the left brace and "{}}" for the right one.

Some keystrokes do not generate characters (such as ENTER and TAB). Some keystrokes represent actions (such as BACKSPACE and BREAK). To send these kinds of keystrokes, send the arguments shown in the following table:

Key

Argument

BACKSPACE

{BACKSPACE}, {BS}, or {BKSP}

BREAK

{BREAK}

CAPS LOCK

{CAPSLOCK}

DEL or DELETE

{DELETE} or {DEL}

DOWN ARROW

{DOWN}

END

{END}

ENTER

{ENTER} or ~

ESC

{ESC}

HELP

{HELP}

HOME

{HOME}

INS or INSERT

{INSERT} or {INS}

LEFT ARROW

{LEFT}

NUM LOCK

{NUMLOCK}

PAGE DOWN

{PGDN}

PAGE UP

{PGUP}

PRINT SCREEN

{PRTSC}

RIGHT ARROW

{RIGHT}

SCROLL LOCK

{SCROLLLOCK}

TAB

{TAB}

UP ARROW

{UP}

F1

{F1}

F2

{F2}

F3

{F3}

F4

{F4}

F5

{F5}

F6

{F6}

F7

{F7}

F8

{F8}

F9

{F9}

F10

{F10}

F11

{F11}

F12

{F12}

F13

{F13}

F14

{F14}

F15

{F15}

F16

{F16}

To send keyboard characters that are comprised of a regular keystroke in combination with a SHIFT, CTRL, or ALT, create a compound string argument that represents the keystroke combination. You do this by preceding the regular keystroke with one or more of the following special characters:

Key

Special Character

SHIFT

+

CTRL

^

ALT

%

NoteNote:

When used this way, these special characters are not enclosed within a set of braces.

To specify that a combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, create a compound string argument with the modified keystrokes enclosed in parentheses. For example, to send the keystroke combination that specifies that the SHIFT key is held down while:

  • e and c are pressed, send the string argument "+(ec)".

  • e is pressed, followed by a lone c (with no SHIFT), send the string argument "+ec".

You can use the SendKeys method to send a pattern of keystrokes that consists of a single keystroke pressed several times in a row. To do this, create a compound string argument that specifies the keystroke you want to repeat, followed by the number of times you want it repeated. You do this using a compound string argument of the form {keystroke number}. For example, to send the letter "x" ten times, you would send the string argument "{x 10}". Be sure to include a space between keystroke and number.

NoteNote:

The only keystroke pattern you can send is the kind that is comprised of a single keystroke pressed several times. For example, you can send "x" ten times, but you cannot do the same for "Ctrl+x".

NoteNote:

You cannot send the PRINT SCREEN key {PRTSC} to an application.

Description

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). Each job runs the Windows calculator and sends it keystrokes to execute a simple calculation.

<package>
   <job id="vbs">
      <script language="VBScript">
         set WshShell = WScript.CreateObject("WScript.Shell")
         WshShell.Run "calc"
         WScript.Sleep 100
         WshShell.AppActivate "Calculator"
         WScript.Sleep 100
         WshShell.SendKeys "1{+}"
         WScript.Sleep 500
         WshShell.SendKeys "2"
         WScript.Sleep 500
         WshShell.SendKeys "~"
         WScript.Sleep 500
         WshShell.SendKeys "*3"
         WScript.Sleep 500
         WshShell.SendKeys "~"
         WScript.Sleep 2500
      </script>
   </job>

   <job id="js">
      <script language="JScript">
         var WshShell = WScript.CreateObject("WScript.Shell");
         WshShell.Run("calc");
         WScript.Sleep(100);
         WshShell.AppActivate("Calculator");
         WScript.Sleep(100);
         WshShell.SendKeys ("1{+}");
         WScript.Sleep(500);
         WshShell.SendKeys("2");
         WScript.Sleep(500);
         WshShell.SendKeys("~");
         WScript.Sleep(500);
         WshShell.SendKeys("*3");
         WScript.Sleep(500);
         WshShell.SendKeys("~");
         WScript.Sleep(2500);
      </script>
   </job>
</package>

Applies To:

Community Content   What is Community Content?
Add new content RSS  Annotations
Simulate a Right-Click      i am donavon   |   Edit   |   Show History

You can use SendKeys to simulate a mouse right-click by sending a shift F10:

WshShell.SendKeys("+{F10}");


Tags What's this?: Add a tag
Flag as ContentBug
Disabled on Vista/Server 2008      SharpDev   |   Edit   |   Show History
SendKeys() has been disabled on Vista/Server 2008, due to security concerns I suspect. The same is true for the SendKeys() function in Visual Basic 6.
Tags What's this?: Add a tag
Flag as ContentBug
Select All?      skappa ... Moonlight137   |   Edit   |   Show History
Q: Can I make a select all command like: ("%a")
When I run this it makes a *pling* but nothing is selected.. Can anyone help me?

A: Select All is usually Ctrl-A, or ("^a"). Alt-A ("%a") will do different things in different applications, but almost certainly not Select All. In MSIE 6, for example, it opens the Favorites menu.
Back Slash      testautomator   |   Edit   |   Show History
SendKeys seems to ignore the backslash ("{\}"). It seems like the backslash has more functions that make it difficult to use SendKeys to type a file location. Please comment.
Tags What's this?: Add a tag
Flag as ContentBug
Laptop fn button      Kyoleet ... udah1   |   Edit   |   Show History
Does anyone know what the keystroke would be to use the fn button on the keyboard of a laptop? I was messing around with WSF files, teaching myself, and I want to make a script that would automate taking a screen shot, opening paint, pasting it in, then saving the file. The biggest concern of mine is figuring out what the keystroke is. It's the fn button, then, the prt sc button, but, when you press that button without fn, it's the home button. Anyone have any ideas of what to type for the keystroke?


A: it's noted in this page that: "You cannot send the PRINT SCREEN key {PRTSC} to an application."
the fn button is irrelevant, the SendKeys sends the actual key and not the one you press on your keyboard.
Flag as ContentBug
Simulate a Left-Click      Bilbogallant ... Thomas Lee   |   Edit   |   Show History

Q: How can you use SendKeys to simulate a mouse left-click?

[tfl - 09 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content.
 
For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Sending unicode characters      pauliemac ... Thomas Lee   |   Edit   |   Show History
Is it possible to simulate pressing 4 digits (one-by-one) whilst holding the Alt key? These are the codes used in Windows for printing unidcode characters. For example to get the letter 'é' you would press Alt+0233.

[tfl - 23 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content. For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

Sending Right Alt key      Joe Train ... Thomas Lee   |   Edit   |   Show History

Is it possible to send right alt key (keys.RMENU)?

[tfl - 23 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content. For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker