Announcement

Collapse
No announcement yet.

Hotkey in Shortcut Link

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hotkey in Shortcut Link

    Good evening, everyone.

    Sorry to bother you all; just have a quick question. I'm trying to create a link to my program on the desktop, and I can get everything to work except for the hotkey field. That particular command only sets a hotkey of "T", and ignores the high-order part of the word that specifies the modifiers to use. Hopefully I'm doing something simple wrong?

    Here's the code:

    Code:
    #COMPILE EXE
    #DIM ALL
    #REGISTER NONE
    #INCLUDE "Win32Api.Inc"
    
    %sLoc_DESKTOP = &H10 ' On Desktop
    %sLoc_PROGRAMS = &H2
    %HOTKEYF_SHIFT = &H01
    %HOTKEYF_CONTROL = &H02
    %HOTKEYF_ALT = &H04
    
    DECLARE FUNCTION CoInitialize LIB "ole32.dll" ALIAS "CoInitialize" (BYVAL pvReserved AS DWORD) AS DWORD
    DECLARE FUNCTION CoCreateInstance LIB "ole32.dll" ALIAS "CoCreateInstance" (rclsid AS STRING * 16, BYVAL pUnkOuter AS ANY, BYVAL dwClsContext AS DWORD, riid AS STRING * 16, ppv AS DWORD) AS DWORD
    DECLARE SUB CoUninitialize LIB "ole32.dll" ALIAS "CoUninitialize"
    ' DECLARE SUB CoTaskMemFree LIB "ole32.dll" ALIAS "CoTaskMemFree" (pv AS DWORD)
    DECLARE FUNCTION Sub1 (p1 AS ANY) AS DWORD
    DECLARE FUNCTION Sub2 (p1 AS ANY, p2 AS ANY) AS DWORD
    DECLARE FUNCTION Sub3 (p1 AS ANY, p2 AS ANY, p3 AS ANY) AS DWORD
    
    SUB CreateLink (BYVAL CSIDL AS LONG, LnkName AS ASCIIZ, _
    ExePath AS ASCIIZ, WorkDir AS ASCIIZ, _
    BYVAL HotKey AS STRING, Comment AS ASCIIZ)
    
    LOCAL TmpAsciiz AS ASCIIZ*256, TmpWide AS ASCIIZ * 512,_
    psl AS DWORD PTR, ppf AS DWORD PTR, pp AS DWORD PTR, lResult AS DWORD,_
    CLSID_ShellLink AS STRING * 16, IID_IShellLink AS STRING * 16,_
    CLSCTX_INPROC_SERVER AS DWORD, IID_Persist AS STRING * 16
    
    CLSID_ShellLink = MKL$(&H00021401) + CHR$(0, 0, 0, 0, &HC0, 0, 0, 0, 0, 0, 0, &H46)
    IID_IShellLink = MKL$(&H000214EE) + CHR$(0, 0, 0, 0, &HC0, 0, 0, 0, 0, 0, 0, &H46)
    IID_Persist = MKL$(&H0000010B) + CHR$(0, 0, 0, 0, &HC0, 0, 0, 0, 0, 0, 0, &H46)
    CLSCTX_INPROC_SERVER = 1
    CoInitialize %Null
    IF ISFALSE(CoCreateInstance (CLSID_ShellLink, %Null, CLSCTX_INPROC_SERVER, IID_IShellLink, psl)) THEN
    
    'Fill shortcut with information
    [email protected]+80: CALL DWORD @pp USING Sub2 (BYVAL psl, ExePath)
    [email protected]+36: CALL DWORD @pp USING Sub2 (BYVAL psl, WorkDir)
    [email protected]+52: CALL DWORD @pp USING Sub2 (BYVAL psl, BYVAL MAKDWD(ASC(Hotkey), %HotKeyF_Alt OR %HotKeyF_Control))
    [email protected]+28: CALL DWORD @pp USING Sub2 (BYVAL psl, Comment)
    
    'Write shortcut
    pp = @psl: CALL DWORD @pp USING Sub3 (BYVAL psl, IID_Persist, ppf) TO lResult
    IF lResult = 0 THEN
    DIM pidl AS DWORD
    TmpAsciiz = CURDIR$
    IF ISFALSE(SHGetSpecialFolderLocation(BYVAL %HWND_DESKTOP, BYVAL CSIDL,_
    BYVAL VARPTR(pidl))) THEN
    SHGetPathFromIDList BYVAL pidl, TmpAsciiz
    CoTaskMemFree BYVAL pidl
    END IF
    TmpAsciiz = TmpAsciiz + "\" + LnkName + ".Lnk"
    MultiByteToWideChar %CP_ACP, 0, TmpAsciiz, -1, TmpWide, %MAX_PATH
    pp = @ppf + 24: CALL DWORD @pp USING Sub3 (BYVAL ppf, TmpWide, BYVAL _
    %True)
    pp = @ppf + 8: CALL DWORD @pp USING Sub1 (BYVAL ppf)
    END IF
    pp = @psl + 8: CALL DWORD @pp USING Sub1 (BYVAL psl)
    END IF
    CoUninitialize
    END SUB
    
    FUNCTION PBMAIN
    CreateLink %sLoc_DeskTop,_
    "Test Program",_
    "C:\PB\Proj.EXE",_
    "C:\PB",_
    "T",_
    "My Comment"
    END FUNCTION
    Thanks for your help, and have a great night!

    Danny.

  • #2
    > I'm doing something simple wrong?
    Of course.
    HRESULT STDMETHODCALLTYPE SetHotkey(WORD wHotkey);
    wHotkey - New hot key. The virtual key code is in the low-order byte, and the modifier flags are in the high-order byte

    Means: something like this
    ByVal Asc(Hotkey) + 256 * (%HotKeyF_Alt + %HotKeyF_Control)

    ------------------
    E-MAIL: [email protected]

    Comment


    • #3
      Hi Semen:
      Ah! I never would have figured that out. Here I was puzzling over why Windows wouldn't accept the dWord I'de so carefully made for it. It never even wanted one in the first place. I guess I don't need to mention that your solution worked flawlessly, eh?
      Thanks once again for all your trouble.

      Danny.

      Comment

      Working...
      X