Library tutorials & articles

SendKeys Command

Sending Keys

You can easily send keys to the active window, as if the user was pressing keys on the keyboard by using the SendKeys statement.

Shell "notepad", vbNormalFocus
SendKeys "This is a test string"

The code above runs notepad, and sends the keys 'This is a test string' to it. If you want to use special keys, such as  ENTER or TAB, and keys that represent actions rather than characters, use the codes shown below:

Key Code
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 specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes:

Key Code
SHIFT +
CTRL ^
ALT %

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times.

Comments

  1. 09 Sep 2009 at 15:38

    Sometimes, I've found, if you change the 'true' to 'false,' in the SendKeys statement, the SendKeys statement will work. I use the default 'false' most of the time. And/or, you might have to put a pause in your code between the AppActivate and SendKeys statements to prevent the keys being sent before the object is ready to receive them. The AppActivate/SendKeys method is unreliable, so you have to tweak things sometimes to get it to work.

  2. 14 Apr 2009 at 06:20
    I think the following example will solve all of your problem SendKeys("+52222222+6+6?ABCD~") This will send the following text %2222222^^?ABCD followed by Enter key as shown above + means Shift key so +5 means Shift and 5 that is % so +6 means Shift and 6 that is ^ ~ means Enter key Hope this will helpfull. Regards Akhtar Tehnical Lead
  3. 04 Dec 2007 at 07:22

     

    I have a similar issue. If any one has any idea how to send key strokes to a citrix client plz reply

     

    [quote user="torcaz99"]I´m using VBScript and it works if I put something like:

     AppActivate "Bloc de notas"                    ' I´ve win2000 in Spanish
     sendKeys ("keys to send"), true

    But I´m using Citrix and execute remote applications. Then I open a remote Notepad and write:

     AppActivate "Bloc de notas - \\Remote"     ' win2000 in Spanish
     sendKeys ("keys to send"), true

    The first line works ok, the remote application activates, but the sendKeys doesn´t work.

    Please, ¿can anyone tell me if there is any way to send key to remote applications?

     Thanks
    [/quote]

  4. 27 Apr 2007 at 19:31
    Use the Windows API.
  5. 03 Nov 2005 at 09:05
    Hello, I was wondering if there is a way to send keystrokes to a window that isn't active, such as a minimized application.
  6. 11 Apr 2005 at 05:39

    Thanks,


    It is working.


    Sunil R

  7. 11 Apr 2005 at 05:22

    HI,


    Thanks for replyng.


    Can you explain in detail how this can be done,
    coz im new to all this stuff.


    thanks


    Sunil

  8. 10 Apr 2005 at 20:10

    Hi,


    You can lock a windows nt based computer (2000,Xp,2003 etc) by the following shell command:


    shell "rundll32.exe user32.dll,LockWorkStation",vbhide

  9. 20 Dec 2004 at 14:07

    Awesoem!, but u kno how when useing  command prompt, u can hit alt+enter to make it go full screen, how would i get it to do tht, its not working i have this so far -
    Shell "C:\WINDOWS\system32\cmd.exe", vbNormalFocus
    SendKeys ("%~")
    SendKeys "@echo off~"
    SendKeys "cd\~"
    SendKeys "cd windows~"
    but i cant get the %~ command to work???? can neone help???

  10. 17 Nov 2004 at 07:07

    in vb how do I send Ctrl + Alt + Del  followed by an Enter key in VB Code
    this is to lock the system.


    thanks


    /Sunil

  11. 04 May 2004 at 14:50

    Thank you.

  12. 03 May 2004 at 08:31

    Hi,


    I've read your post about sendkeys and I have the same issue.


    In a script I want to copy the text selected and it works well on my computer with this code :


    Set Wsh = WScript.CreateObject("WScript.Shell")
    Wsh.SendKeys "^{c}"


    But I installed the program on other computers and there, the script doesn't copy anything !!


    Do you know how can this happen ?
    Are there rights that I need to configure on other computers so that sendkeys can be called ?


    Thanks,


    Jerome

  13. 20 Mar 2004 at 21:10
    I'm not sure if this can be accomplished with the send keys but how can you simulate a left or right mouse click?
  14. 09 Mar 2004 at 12:01

    If you need to use sendkeys to send a combination of keys for example CTRL+C (for copy) you need to use brackets. Sendkeys "^C" sends CTRL and C separately. Use something like Sendkeys "(^C)"

  15. 09 Feb 2004 at 07:53

    I dont know if it will help
    The same case happen when I use ComponentOne Flexgrid, I use to simulate left key to do the shift tab


           ' Left or Shift Tab (65536 + 9)
           If lastkey = 37 Or lastkeydata = 65545 Then
                 System.Windows.Forms.SendKeys.Send("+{TAB}")
           Else
               System.Windows.Forms.SendKeys.Send("{TAB}")
           End If


    for a ctrl, try 131072 + 9 instead of 65536 + 9
    I just curious if it work in your case

  16. 09 Feb 2004 at 07:47

    I dont know if it will help
    The same case happen when I use ComponentOne Flexgrid, I use to simulate left key to do the shift tab


           ' Left or Shift Tab (65536 + 9)
           If lastkey = 37 Or lastkeydata = 65545 Then
                 System.Windows.Forms.SendKeys.Send("+{TAB}")
           Else
               System.Windows.Forms.SendKeys.Send("{TAB}")
           End If


    I just curious if it work in your case

  17. 21 Oct 2003 at 10:09

    Hey All,


        I'm having the same problem with sendkeys "^{TAB}". My application treats this command as a TAB - "{TAB}", but I need a CTRL+TAB to skip through a text box. Does anyone have any suggestions? Thanks,


    Steve

  18. 16 Oct 2003 at 09:46
    Hi

     I am trying to simulate data entry in SAP screen from VB. The Ctrl+tab and Shift+tab are not working.

     I used "^{tab}" , "^(vbtab)" , "^((vbtab))", "^({tab})" but nothing worked.

     Please do clarify
  19. 08 Aug 2003 at 23:42

    How can I simulate Ctrl+Alt+Del on WinXP? I tried it and although it sends the keys (and my sort-of-keylogger verified it), Windows ignores it.

  20. 16 Jul 2003 at 15:58

    Good Afternoon - I have a VB program that launch the C shell (services for UNIX).  I will like to print some commands at the prompt using the SendKeys but it won't work.  Below is my code:


    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


    'Public customReportName As String, customReportName2 As String


    Private Const WM_CLOSE = &H10
    Private openCShellExe As Long


    'EXECUTE THE EXECUTABLE FILE ASSOICATED WITH THE MEDTOX REPORTS
    Public Sub cmdOpenCShell()
       
       frmPrinter.WindowState = 1
       If frmPrinter.cmdPrint.Value = True Then
           Call Shell("C:\WINDOWS\SYSTEM32\POSIX.EXE /u /c /bin/csh -l", vbNormalFocus)
           'Call Shell("cmd.exe", vbNormalFocus)
           DoEvents
           openCShellExe = GetForegroundWindow
           SendKeys "cd .." ', True
           SendKeys ("{Enter}")
           SendKeys "ls"
        Else
           Exit Sub
       End If
       
    End Sub

  21. 12 Jul 2003 at 04:34

    You're opening Notepad on another PC? It won't work then. It only simulates keypresses on your keyboard. You'd have to get it to run an app/script that sends the keys on the other PC.

  22. 09 Jul 2003 at 14:07


    I think what happened was the  SendKeys ("{TAB 9}"), True can't be interpreted  as tab for 9 times. Hence the SendKey "^C" statement was ignored. Why I don't know.


    Solution: I use For statement to loop 9 times.
     For I = 1 to 9
        SendKeys ("{TAB}"), True
     Next I


     SendKeys "^C"


    This then works.
    ...

  23. 09 Jul 2003 at 12:56

    I am trying to copy a field and paste it into another field.

  24. 10 Jun 2003 at 11:17

    I believe the sendkeys command for the spacebar is { } (make sure you put a space between).

  25. 05 Jun 2003 at 11:24

    I´m using VBScript and it works if I put something like:


     AppActivate "Bloc de notas"                    ' I´ve win2000 in Spanish
     sendKeys ("keys to send"), true


    But I´m using Citrix and execute remote applications. Then I open a remote Notepad and write:


     AppActivate "Bloc de notas - \Remote"     ' win2000 in Spanish
     sendKeys ("keys to send"), true


    The first line works ok, the remote application activates, but the sendKeys doesn´t work.


    Please, ¿can anyone tell me if there is any way to send key to remote applications?


     Thanks

  26. 01 Jun 2003 at 18:19

    I appreciate finding a more complete listing of Sendkeys.  Unfortuantely, the one I need is the only one not listed.  I need the one for the SPACEBAR.   I need to a SendKey command that holds down the Ctrl+Alt+Spacebar so the field will update a default in the field.  So far I have SendKeys "(^% )"  (With the one for spacebar missing.  I'm using this in an Access 2000 database.  My thanks to anyone who may be able to help.  Sue

  27. 27 Mar 2003 at 16:36

    You mean the classic "This computer has been locked, only XXX or an administrator can unlock" message? I doubt it would let your program run. However if it did, it would merely send the keys to the dialog, which shouldn't be a problem but will (probably, unless you get lucky ) generate an Invalid Password when you hit Enter and make a lot of noise when you exceed the maximum text length.

  28. 27 Mar 2003 at 16:30

    Well, if you know the following will be true:
    -The file will be changed
    -There won't be a file with the same name
    You can do this:
    SendKeys "%{F4}{ENTER}file1.txt{TAB}{TAB}{ENTER}
    This will close the program, it'll ask if you want to save, OK will be chosen, a filename entered and OK chosen again. This is a very unreliable method and will only work on WinXP (small adjustments are required for others due to less listboxes), but it should do for simple stuff. file1.txt is the filename, which will be entered right where it should be. If the file already exists the user will have to manually choose whether to overwrite, and if it's unchanged Notepad will close and everything after Alt+F4 will be sent to the next program which could be a problem, so consider typing and deleting something to avoid this.


    [edit] Oh yeah, and on XP you can use Ctrl+S to save - the filename dialog will only appear on an unsaved file!

  29. 27 Mar 2003 at 16:26

    It does for me. Is Notepad opening minimized? It won't work then. You should be able to figure it out from the compiler, I think it's 'shell "notepad" as vbMaximizedFocus".

  30. 24 Mar 2003 at 08:01

    I am curious as to why the code on this webpage does not work in NotePad.

  31. 12 Mar 2003 at 14:43

    I have a question to ask you, I noticed that you had looked at the senkeys method on developer fusion and it seemed like you probably had yours working.  I'm working in Access 2000, and I am trying to open a program called Macro Angel and send the key combination of Shift+Q over to it, I cannot get it to work.  Here is some code that I have tried to use, maybe you could help me out here.  If you could email me back at rwjeske@hotmail.com it would be greatly appreciated.


       stAppName = "C:\Program Files\Demirhan\Macro Angel\MacroAngel.exe"
       Call Shell(stAppName, 1)
       SendKeys ("+q")


       Shell "c:\Program Files\Demirhan\Macro Angel\MacroAngel.exe", vbMinimizedFocus
       SendKeys "+q"


    Regards,
    Ryan

  32. 05 Mar 2003 at 07:44

    i want to save an opened notepad file. how can i do sendkeys for this. please send me code. This file will be opened manually by user. I just want to save this

  33. 16 Jan 2003 at 08:45

    What's happen if I use the sendkeys in a script, and the computer was locked before the script starts ?  (I use events scheduler to start the script !)  thanks

  34. 03 Nov 2002 at 12:00

    nice job, but how do i get it to send the keys to the window thats got the focus when i press a certain button,
    e.g. send the text to internet explorer when i press F3 or something like that ?

  35. 15 Jul 2002 at 14:41

    Nice Example. This code can be quickly adepted to make a nice keylogger.

  36. 01 Jan 1999 at 00:00

    This thread is for discussions of SendKeys Command.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!