vbscriptkeyboard-shortcutsshortcutsendkeyslnk

Set keyboard combination for .lnk shortcut


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}"

Solution

  • 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