02- February 01 2017 Edited
[Answer]: just me's code is a better answer, from qwerty12's last post:
qwerty12 wrote:Use just me's code. It's simpler, safer (no pointer arithmetic) and doesn't have the Unicode-only restriction I imposed.
Code: Select all
#NoEnv
Target := A_ScriptDir . "\TestLnk.txt"
LnkFile := A_Desktop . "\TestLnk.lnk"
FileOpen(Target, "w").Close()
FileCreateShortcut, %Target%, %LnkFile%, , , LnkFile Test, , b
MsgBox, 0, Hotkey, % ShortcutGetHotkey(LnkFile)
ShortcutSetHotkey(LnkFile, "A", "Ctrl+Alt+Shift")
MsgBox, 0, Hotkey, % ShortcutGetHotkey(LnkFile)
ExitApp
; ======================================================================================================================
ShortcutGetHotkey(ShortcutPath) {
SplitPath, ShortcutPath, Name, Dir
Shell := ComObjCreate("Shell.Application")
ShellLink := Shell.NameSpace(Dir).ParseName(Name).GetLink
HK := ShellLink.Hotkey
Key := GetKeyName(Format("VK{:02X}", HK & 0xFF))
HK >>= 8
Mods := (HK & 2 ? "Ctrl+" : "") . (HK & 4 ? "Alt+" : "") . (HK & 1 ? "Shift+" : "")
Return (Mods . Key)
}
; ======================================================================================================================
ShortcutSetHotkey(ShortcutPath, Key, Modifiers := "") {
Static ModValues := {Alt: 4, Ctrl: 2, Shift: 1}
Key := GetKeyVK(Key)
If (Key = 0)
Return False
ModArray := StrSplit(Modifiers, "+")
If (ModArray.MaxIndex() < 2) || (ModArray.MaxIndex() > 3)
Return False
Mods := 0
For Each, Mod In ModArray
Mods |= (M := ModValues[Mod]) ? M : 0
If Mods In 0,1,2,4
Return False
SplitPath, ShortcutPath, Name, Dir
Shell := ComObjCreate("Shell.Application")
ShellLink := Shell.NameSpace(Dir).ParseName(Name).GetLink
ShellLink.Hotkey := (Key | (Mods << 8))
ShellLink.Save
Return True
}
______
01- January 30 2017 Edited
[Answer]: qwerty12:
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; From deliberately using IID_ShellLinkW to the string manipulation, this assumes a Unicode build of AutoHotkey throughout.
; Credits:
; * https://github.com/maul-esel/AHK-Util-Funcs/blob/master/SUCCEEDED.ahk
; * https://msdn.microsoft.com/en-us/library/windows/desktop/bb774926(v=vs.85).aspx
; * the AutoHotkey source: https://github.com/Lexikos/AutoHotkey_L/blob/HEAD/source/script_autoit.cpp#L1240
; * comctl32.dll
SUCCEEDED(hr) {
return hr != "" && hr >= 0x00
}
LOBYTE(w) {
return w & 0xff
}
HIBYTE(w) {
return (w >> 8) & 0xff
}
GetShortcutHotkey(lnkFile)
{
wHotkey := 0
if (FileExist(lnkFile)) {
try ShellLinkW := ComObjCreate(CLSID_ShellLink := "{00021401-0000-0000-C000-000000000046}", IID_ShellLinkW := "{000214F9-0000-0000-C000-000000000046}")
if (ShellLinkW) {
try PersistFile := ComObjQuery(ShellLinkW, IID_IPersistFile := "{0000010b-0000-0000-C000-000000000046}") ; IPersistFile::Load
if (PersistFile) {
if (SUCCEEDED(DllCall(NumGet(NumGet(PersistFile+0)+5*A_PtrSize), "Ptr", PersistFile, "WStr", lnkFile, "UInt", 0)))
DllCall(NumGet(NumGet(ShellLinkW+0)+12*A_PtrSize), "Ptr", ShellLinkW, "UShort*", wHotkey) ; IShellLink::GetHotkey
ObjRelease(PersistFile)
}
ObjRelease(ShellLinkW)
}
}
return wHotkey
}
HotkeyToString(hk)
{
if (!hk) {
msg := "None"
} else {
HOTKEYF_ALT := 0x04, HOTKEYF_CONTROL := 0x02, HOTKEYF_EXT := 0x08, HOTKEYF_SHIFT := 0x01
cbMsg := VarSetCapacity(msg, 128 * 2, 0)
wMod := HIBYTE(hk)
if (wMod & HOTKEYF_CONTROL)
msg .= GetKeyName("Ctrl")
if (wMod & HOTKEYF_SHIFT)
msg .= (msg ? " + " : "") . GetKeyName("Shift")
if (wMod & HOTKEYF_ALT)
msg .= (msg ? " + " : "") . GetKeyName("Alt")
if ((wVk := DllCall("MapVirtualKeyW", "UInt", LOBYTE(hk), "UInt", 0) << 16)) {
if (wMod & HOTKEYF_EXT)
wVk |= 0x01000000
if (msg)
msg .= " + "
currCchMsg := StrLen(msg)
if ((ret := DllCall("GetKeyNameTextW", "Int", wVk, "Ptr", &msg+(currCchMsg*2), "Int", (cbMsg/2) - currCchMsg))) {
if ((offset := (currCchMsg+ret) * 2) > cbMsg)
ExitApp 1 ; die
NumPut(0, msg, offset, "UChar"), VarSetCapacity(msg, -1)
}
}
}
return msg
}
shortcutHk := GetShortcutHotkey("<shortcut goes here>")
if (False) {
; Using the hotkey GUI control to show the result:
Gui, Add, Hotkey, vChosenHotkey hwndhwndChosenHotkey
SendMessage 0x0401, %shortcutHk%, 0,, ahk_id %hwndChosenHotkey% ; HKM_SETHOTKEY
Gui, show
} else {
MsgBox % HotkeyToString(shortcutHk)
}
Fill "<shortcut goes here>" with your link, so you can test qwerty12's code, like this "C:\Path to the link file\that\you want to test\my lnk test file.lnk"
______qwerty12 wrote:Code: Select all
shortcutHk := GetShortcutHotkey("<shortcut goes here>")
I found something on autoit but it is way above my knowledge.
There is nothing simple to do that, really ?
Anyway I didn't found much help on it searching google: there is almost nothing!
(Or maybe, I didn't succeed at it . . .)
Here it is:
1- The autoit page:
autoit page: get the "shortcut keys" of a shortcut (.lnk file)
2-The Google search pages:
Google search: get the "shortcut keys" of a .lnk file?
Google search: how to retrieve keyboard shortcut keys from windows .lnk file?
This could be helpful to some of you.
Maybe some skilled coders could give an answer or tracks to follow.