8

I've recently been using the SendKeys function using Batch script.

I've understood how to input certain keys into a window, such as the tab key:

%SendKeys% "{TAB}"

Or the backspace key:

%SendKeys% "{BACKSPACE}"

But I have been trying to input the Windows key without pressing it.

Unfortunately, I do not know what the batch name for it is. I've tried:

WIN
WINDOWS
WINKEY
START
LWIN

But none have worked.

I've looked everywhere for this, and help would be greatly appreciated.

John Kens
  • 1,615
  • 2
  • 10
  • 28
Zinger
  • 413
  • 2
  • 5
  • 16
  • You have not said us where you got the SendKeys solution from, but if it was from [this answer](https://stackoverflow.com/a/17050135/778560), then in the same post there is [this link](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/8c6yea83(v=vs.84)) to all SendKeys keys names. And no, there is not a name for the Windows key... – Aacini Aug 16 '18 at 02:02
  • how do you use the sendkeys method in a shell script? do I have to instantiante sth? – crappidy Oct 12 '20 at 09:31

4 Answers4

7

There is currently no way to simulate the windows home logo in sendkey's, howevery this does not mean it's not possible.

If you take a look at the windows shortcut keys you will find you can simulate Open Start with the following key combinations: Ctrl + Esc.

To simulate this in batch, you can use: powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^{ESCAPE}') or in your case: %SendKeys% "^{ESCAPE}".

As stated in sendkeys:

  • "^" - Simulates a Ctrl key press.
  • "{ESCAPE}" - Simulates a Esc key press.
John Kens
  • 1,615
  • 2
  • 10
  • 28
4

You can make a program to simulate winkey being pressed.

WinKey+R.VB

Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Module SendWinKey

    Const KEYEVENTF_KEYDOWN As Integer = &H0
    Const KEYEVENTF_KEYUP As Integer = &H2

    Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)

       Public Sub Main()    
           keybd_event(CByte(Keys.LWin), 0, KEYEVENTF_KEYDOWN, 0) 'press the left Win key down
           keybd_event(CByte(Keys.R), 0, KEYEVENTF_KEYDOWN, 0) 'press the R key down
           keybd_event(CByte(Keys.R), 0, KEYEVENTF_KEYUP, 0) 'release the R key
           keybd_event(CByte(Keys.LWin), 0, KEYEVENTF_KEYUP, 0) 'release the left Win key
       End Sub
End Module

Place on your desktop.

Open a command prompt and type

C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%userprofile%\desktop\Win+R.vb" /out:"%userprofile%\Desktop\Win+R.exe" /target:winexe

A file called Win+R.exe will appear on your desktop. Move it anywhere into the path.

CatCat
  • 483
  • 4
  • 5
0

I currently have this in my code for using SendKeys and it works great to automate certain keyboard commands. This just automatically sets the default browser and default photo applications. I know this is a couple years late, but hopefully it help

control /name Microsoft.DefaultPrograms /page pageDefaultProgram
SET TempVBSFile=%tmp%\~tmpSendKeysTemp.vbs
IF EXIST "%TempVBSFile%" DEL /F /Q "%TempVBSFile%"
ECHO Set WshShell = WScript.CreateObject("WScript.Shell") >>"%TempVBSFile%"
ECHO Wscript.Sleep 2000                                   >>"%TempVBSFile%"
ECHO WshShell.SendKeys "{TAB 3}"                          >>"%TempVBSFile%"
ECHO Wscript.Sleep 1000                                   >>"%TempVBSFile%"
ECHO WshShell.SendKeys "{ENTER}"                          >>"%TempVBSFile%"
ECHO Wscript.Sleep 4000                                   >>"%TempVBSFile%"
ECHO WshShell.SendKeys "{TAB 3}"                          >>"%TempVBSFile%"
ECHO Wscript.Sleep 1000                                   >>"%TempVBSFile%"
ECHO WshShell.SendKeys "{ENTER}"                          >>"%TempVBSFile%"
ECHO Wscript.Sleep 4000                                   >>"%TempVBSFile%"
ECHO WshShell.SendKeys "%%{F4}"                           >>"%TempVBSFile%"
CSCRIPT //nologo "%TempVBSFile%"
echo Return here once you are done.
echo.
echo ^< Press ANY key to return to the Menu ^> & Pause > NUL)
CLS & Goto :Menu
-1

SOLVED - first create a file .vbs with the code

Set WshShell = WScript.CreateObject("WScript.Shell")`   
WshShell.AppActivate "notepad"
WshShell.SendKeys "^{pgdn}"

and after you must call that file from a .bat file