I wrote this little vbs script to press the left arrow key in my Chrome browser:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Google Chrome"
WshShell.SendKeys "{LEFT}"
But it simulates a keypress with instant releases. Is there also a way to parse the pressed key over a longer time?
(I know that this would be easy to handle in Javascript by the Keypress even, but I am trying to learn vbs.)
The WshShell object does not provide a way to send KeyUp and KeyDown events. To closest you can get to what you want to do is by doing repetitions of the same key. This can be done by putting the .SendKeys in a loop or by putting a number after the key within the braces.
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Google Chrome"
WshShell.SendKeys "{LEFT 40}"
Related
Option Explicit
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files (x86)\Blackmagic Design\Blackmagic Media Express\MediaExpress.exe""" ,1,true
Wscript.Sleep 100
WshShell.SendKeys "^1"
Set WshShell = Nothing
.SendKeys method uses alphanumeric keypad for transmitting digits. My keyboard (the line with numbers under the uppermost EscF1… key row) behaves as follows (using Czech keyboard input):
°1234567890 with Shift
;+ĚŠČŘŽÝÁÍÉ with CapsLock
;+ěščřžýáíé unmodified
I can confirm that e.g. WshShell.SendKeys "^1" (with Windows Script Host Version 5.8) sends Ctrl+Shift+1 keystrokes to the active window as if typed on the keyboard.
My answer: I need to use WshShell.SendKeys "^{+}" to emulate keyboard Ctrl+1 input.
Above .SendKeys behaviour is independent on current keyboard layout (input method) in calling cmd window (cscript //nologo 34189495.vbs) and in window activated right before WshShell.SendKeys "^1". Tested all combinations of
- Windows display language English (UK), keyboard input method Czech/United Kingdom and
- display language Czech, keyboard input method Czech/US-international,
each versus each other…
Another problem: how to emulate keyboard input Ctrl+2 using .SendKeys method? I can't use simply WshShell.SendKeys "^{ě}". It does not work because ě (latin small letter E with caron) hexadecimal value is greater than 0x79 (and ščřžýáíé as well). As per Keyboard Scan Code Specification Microsoft document, Prefixed Scan Codes part (sorry for link to download):
If you use scan codes from the 0xE0 set, make sure the second byte
is suitable in the same way as single byte scan code values. In other
words: Not greater than 0x79, Not 0x60 or 0x61, Not 0x00…
However, here is a workaround: WshShell.SendKeys "{NUMLOCK}^{ě}{NUMLOCK}" (who knows why it works? Derived from my previous experience with keyboard wedge barcode scanners!)
Addendum: tested using my preferred text and code editor PSPad;
Here is my working code:
option explicit
'On Error Resume Next
On Error GoTo 0
Dim WshShell
set WshShell = WScript.CreateObject("WScript.Shell")
' WshShell.Run omitted, activate existing window (this script open in PSPad)
WScript.Sleep 100
WshShell.AppActivate "34189495.vbs"
WScript.Sleep 200
' Ctrl+Shift+3 set marker #3
WshShell.SendKeys "^3"
WScript.Sleep 100
' Ctrl+1 goto marker #1, i.e. move cursor to that line (pre)marked 1
WshShell.SendKeys "^{+}"
' more time to observe
WScript.Sleep 3000
' Ctrl+3 goto marker #3, i.e. move cursor to that line marked 3
WshShell.SendKeys "{NUMLOCK}^{Š}{NUMLOCK}"
WScript.Sleep 100
' more time before quit
WScript.Sleep 3000
Wscript.Echo Wscript.ScriptName
Wscript.Quit
In the past I've used the script below to detect if an active window with the title "Remote Desktop" is or isn't active. If it isn't active the script will auto launch or make it active again.
Question: Is there a way to detect if a window is active and auto close it? I'm setting up a kiosk in chrome kiosk mode on a Windows 7 machine for our office. The main page launches a selected Google form in a pop-up window. The form confirmation page has the title "Thank You!" in the title bar. Is there a way for the script to auto detect this window and close it? This would be nice because the user would see that their response was submitted (for a second or two) but if they did not close the window it would not still be open when the next user goes to use the kiosk.
Another option might be if there is a way to use a Google script on the form to program the submit button to close the window. I'm not sure if that's possible.
Option Explicit
'On Error Resume Next
Dim objShell
Set objShell = CreateObject("WScript.Shell")
Do
If (objShell.AppActivate("Window Title Here") = False) Then
objShell.Run "mstsc.exe " & chr(34) & "c:\scripts\Remote Desktop.rdp" & chr(34)
WScript.Sleep 5000
Else
WScript.Sleep 3000
End If
Loop
If the window I need closed is active and then the following script is ran the window will close. It's almost like I need to piece the top and bottom code here together to achieve what I need, but I'm not sure how.
Dim oShell
Set oShell = CreateObject("WScript.Shell")
If oShell.AppActivate("Untitled - Notepad") Then
WScript.Sleep 500
oShell.SendKeys "%{F4}"
End If
I'm trying to find a script that will run on startup and wait for a window with a specific title to open and then close it once it is detected. It would be even better if I could control how long the window remains open once detected, but if I could just get it to close that would suffice.
I think I've found a good solution. I found this post and modified the answer. Does anyone see any issues with this?
' Will loop forever checking for the message every half a second
' When it finds the message it will close the window
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Untitled - Notepad")
If ret = True Then
wshShell.SendKeys "%{F4}" 'ALT F4
End If
WScript.Sleep 500
Loop
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Untitled - Notepad")
If ret = True Then
wshShell.SendKeys "%{F4}" 'ALT F4
End If
WScript.Sleep 500
Loop
The only trouble i see is the Alt+F4 will sequentially close windows until it will want to shut down windows itself. kinda makes me nervous even though this script will only detect the name you give it.
I tried the script and it works fine but what about using the escape key for certain windows?
The other thing i do not like is this is always running and taking resources. I slowed down the loop so it will at least be paused in the background most of the time..
I am trying to write a simple script that will send the key combo of CTRL+WINDOWS KEY+TAB. The code below sends the keys CTRL+ALT+TAB
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^%{TAB}"
However when I try to replace "%" (aka the ALT key) with LWIN (aka the Left Windows Key) it says Syntax Error.
I tried the following, but had no luck:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^{LWIN}{TAB}"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^{LWIN}+{TAB}"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys ^{LWIN}+{TAB}
I know it has something to do with being able to Hold certain keys while other keys are pressed but I can't seem to get it right.
The windows key can be pressed programmatically using CTRL+ESC. Is there a way to set this combination as a variable called LWIN and then use one of the above Scripts?
Just in case someone land here on these years...
A workaround (instead of sending keystrokes) is to call directly to the application:
Set objShell = CreateObject("Shell.Application")
objShell.WindowSwitcher
This will open Task Switcher Windows App. (Same as ⊞ windows+TAB)
try this code:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "+(^{LWIN}{TAB})"
I know you are looking for VBscript but it looks like that is unlikely (pure VBscript). Here is a post that did solve this via C#.
https://stackoverflow.com/a/10367832/1742115
This page tells how to call the C# DLL from your VBscript if you want to keep some of this in vbs.
I think your question is an example of an XY problem and what you actually want to do is to activate Flip 3D (Switch between windows). You can do this programmatically by executing the rundll32 DwmApi #105 command:
CreateObject("WScript.Shell").Run "rundll32 DwmApi #105"
How to mock the background window to active state using VB script?
There is some keycode command I need to send to some background window(I known the related VB function), but it will only work when the window is active. If the window is really active, I cant do anything in the foreground.
So is it there anyway to mock the window to active state?
Yes there is, see this example
Dim oShell
Set oShell = CreateObject("WScript.Shell")
'bring the window to front
'title must be exactly what you see in the titlebar of the window
oShell.AppActivate "title of your window"
WScript.Sleep 500 'give the window the time to activate, time is in miliseconds
oShell.SendKeys "{ENTER}" 'you can send keystrokes to this window
Set oShell = nothing
if you want to minimize that window you can send keystrokes to do that, but this is language dependent so first try them manually. The following sends the keystrokes Alt (%) space and N from miNimize. Try in your window to push the Alt-Space keys to get the menu that controls this
oShell.SendKeys "(% )N"
I tried making a script that should perform the following functions:
runs in background
focus moves to next tab if user press any key
run on windows platform.
I used the following code but its not working :(
Please correct me if I had made a error in code.
Set wshShell = WScript.CreateObject("WScript.Shell")
Dim a
Do
a = WScript.StdIn.Read(1)
wshShell.SendKeys "{TAB}"
Loop