Set keyboard combination for .lnk shortcut

266 views Asked by At

I am trying to set keyboard combination for a .lnk shortcut using vbs. For some reason the 'shortcut key' section will not accept emulated letter key presses. All I get is pause key. For some reason Enter key as well as Tab, Esc work, meaning I can switch to next line or close shortcut properties altogether. Please adivise.

enter image description here

This is the VBS script I am using.

set WshSHell = Wscript.CreateObject("WScript.Shell")
WshShell.SendKeys "{1}"                              *for simplicity I named the shortcut "1"*                                    
WshShell.SendKeys "+{F10}"                           *emulates righ-click*
WshShell.SendKeys "{UP}"                             *select 'Properties'*
WshShell.Sendkeys"{ENTER}"
WScript.Sleep 500                                    *wait fo window to pop-up*
WshShell.SendKeys "{TAB 2}"                          *switch to 'shortcut key' line*
WshShell.SendKeys "{G}"                              *error*
WshShell.SendKeys "{ENTER}"
2

There are 2 answers

0
Geert Bellekens On BEST ANSWER

There's no need to use Sendkeys to do this. You can actually edit the properties of the shortcut, including the Hotkey in code.

From the Microsoft Documentation

WshShell = CreateObject("Wscript.shell")
strDesktop = WshShell.SpecialFolders("Desktop")
oMyShortcut = WshShell.CreateShortcut(strDesktop + "\Sample.lnk")
oMyShortcut.WindowStyle = 3 &&Maximized 7=Minimized 4=Normal
oMyShortcut.IconLocation = "C:\myicon.ico"
OMyShortcut.TargetPath = "%windir%\notepad.exe"
oMyShortCut.Hotkey = "ALT+CTRL+F"
oMyShortCut.Save
0
Nawad-sama On

I did some tweaking and it now suits my needs. Most of all I had to place 'set' at the begining of WshShell and first oMyDesktop lines and remove what seems to be a comment on 'WindiwStyle' line. Please see below.

set WshShell = CreateObject("Wscript.shell")
set oMyShortcut = WshShell.CreateShortcut("PATH\test.lnk")
oMyShortcut.WindowStyle = 4
oMyShortcut.IconLocation = "%SystemRoot%\System32\imageres.dll,63"
OMyShortcut.TargetPath = "PATH\test.bat"
oMyShortCut.Hotkey = "ALT+CTRL+K"
oMyShortCut.Save