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.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
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...
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.
!--removed tag-->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]
Thanks,
It is working.
Sunil R
HI,
Thanks for replyng.
Can you explain in detail how this can be done,
coz im new to all this stuff.
thanks
Sunil
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
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???
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
Thank you.
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
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)"
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
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
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
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
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.
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
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.
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.
...
I am trying to copy a field and paste it into another field.
I believe the sendkeys command for the spacebar is { } (make sure you put a space between).
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
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
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.
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!
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".
I am curious as to why the code on this webpage does not work in NotePad.
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
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
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
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 ?
Nice Example. This code can be quickly adepted to make a nice keylogger.
This thread is for discussions of SendKeys Command.