Announcement

Collapse
No announcement yet.

Adding hotkey to Semen's / Josés shortcut code

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

  • Adding hotkey to Semen's / Josés shortcut code

    after trying this code from semen which was nicely formatted and
    commented by josé
    http://www.powerbasic.com/support/pb...ad.php?t=24977

    i was wondering if it might be possible to also supply the hotkey,
    and maybe have it return a value if it couldn't assign the hotkey
    (because maybe the hotkey was already in use?)

    i had a look at the windowsapi help and it shows this:

    [now supported on windows nt]

    sets a hot key for a shell link object.

    hresult stdmethodcalltype sethotkey(

    ishelllink far *pshllnk,
    word whotkey
    );


    parameters

    pshllnk

    pointer to the ishelllink interface. in c++, this parameter is implicit.

    whotkey

    hot key. the virtual-key code is in the low-order byte, and the modifier flags are in the high-order byte. the modifier flags can be a combination of the values specified in the description of the ishelllink::gethotkey method.


    return values

    returns noerror if successful or an ole-defined error value otherwise.
    setting a hot key allows the user to activate the object by pressing a particular combination of keys.

    see also

    ishelllink, ishelllink::gethotkey


    but i'm not sure how to incorporate that into the code.

    can anyone help?

    lastly, is this routine compatible with all versions of windows?
    what about users not logged on as admin on nt and xp ?

    cheers

    ian

  • #2
    You can use this wrapper:
    Code:
    FUNCTION  IShellLink_SetHotKey (BYVAL pthis AS DWORD PTR, BYVAL pwHotkey AS WORD) AS LONG
      LOCAL HRESULT AS LONG
      CALL DWORD @@pthis[13] USING IShellLink_SetHotKey (pthis, pwHotkey) TO HRESULT
      FUNCTION = HRESULT
    END FUNCTION

    ------------------
    Website: http://com.it-berater.org
    SED Editor, TypeLib Browser, Wrappers for ADO, DAO, ODBC, OLE DB, SQL-DMO, WebBrowser Control, MSHTML, HTML Editing, CDOEX, MSXML, WMI, MSAGENT, Flash Player, Task Scheduler, Accesibility, Structured Storage, WinHTTP, Microsoft ActiveX Controls (Data Binding, ADODC, Flex Grid, Hierarchical Flex Grid, Masked Edit Control, DataList, DataCombo, MAPI, INET, MCI, Winsock, Common Dialog, MSChart, Outlook View Control), and Microsoft Scripting Components.
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Thanks José, i only just saw your post.

      I shall give it a go.

      Cheers

      Ian

      Comment


      • #4
        erm,,, I got so far then got stuck.
        I think the hotkey combination should be provided in a Word
        (which as i understand it is two bytes) I'm just not sure
        how to provide it in the function call.

        Here is what i have so far:

        Code:
        #Dim All
        #Compile Exe
        #Include "Win32Api.inc"
        
        ' =======================================================================================
        ' Constants
        ' =======================================================================================
        %CLSCTX_INPROC_SERVER = &H1    ' Component is allowed in the same process space.
        ' =======================================================================================
        
        ' =======================================================================================
        ' IIDs
        ' =======================================================================================
        $CLSID_ShellLink = Guid$("{00021401-0000-0000-C000-000000000046}")
        $IID_IShellLink = Guid$("{000214EE-0000-0000-C000-000000000046}")
        $IID_IPersistFile = Guid$("{0000010B-0000-0000-C000-000000000046}")
        ' =======================================================================================
        
        ' =======================================================================================
        ' Returns a pointer to a specified interface on an object to which a client currently
        ' holds an interface pointer. 
        ' =======================================================================================
        Function IUnknown_QueryInterface (ByVal pthis As Dword Ptr, ByRef riid As Guid, ByRef ppvObj As Dword) As Long
          Local HRESULT As Long
          Call Dword @@pthis[0] Using IUnknown_QueryInterface (pthis, riid, ppvObj) To HRESULT
          Function = HRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Decrements the reference count for the calling interface on a object. If the reference
        ' count on the object falls to 0, the object is freed from memory.
        ' =======================================================================================
        Function IUnknown_Release (ByVal pthis As Dword Ptr) As Dword
          Local DWRESULT As Dword
          Call Dword @@pthis[2] Using IUnknown_Release (pthis) To   DWRESULT
          Function = DWRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Sets the description string for a Shell link object.
        ' =======================================================================================
        Function IShellLink_SetDescription (ByVal pthis As Dword Ptr, ByRef pszName As Asciiz) As Long
          Local HRESULT As Long
          Call Dword @@pthis[7] Using IShellLink_SetDescription (pthis, pszName) To HRESULT
          Function = HRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Sets the name of the working directory for a Shell link object.
        ' =======================================================================================
        Function IShellLink_SetWorkingDirectory (ByVal pthis As Dword Ptr, ByRef pszDir As Asciiz) As Long
          Local HRESULT As Long
          Call Dword @@pthis[9] Using IShellLink_SetWorkingDirectory (pthis, pszDir) To HRESULT
          Function = HRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Sets the command-line arguments for a Shell link object.
        ' =======================================================================================
        Function IShellLink_SetArguments (ByVal pthis As Dword Ptr, ByRef pszArgs As Asciiz) As Long
          Local HRESULT As Long
          Call Dword @@pthis[11] Using IShellLink_SetArguments (pthis, pszArgs) To HRESULT
          Function = HRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Sets the show command for a Shell link object. The show command sets the initial show
        ' state of the window.
        ' =======================================================================================
        Function IShellLink_SetShowCmd (ByVal pthis As Dword Ptr, ByVal iShowCmd As Long) As Long
          Local HRESULT As Long
          Call Dword @@pthis[15] Using IShellLink_SetShowCmd (pthis, iShowCmd) To HRESULT
          Function = HRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Sets the path and file name of a Shell link object.
        ' =======================================================================================
        Function IShellLink_SetPath (ByVal pthis As Dword Ptr, ByRef pszFile As Asciiz) As Long
          Local HRESULT As Long
          Call Dword @@pthis[20] Using IShellLink_SetPath (pthis, pszFile) To HRESULT
          Function = HRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Sets the hotkey of a Shell link object.
        ' =======================================================================================
        Function  IShellLink_SetHotKey (ByVal pthis As Dword Ptr, ByVal pwHotkey As Word) As Long
          Local HRESULT As Long
          Call Dword @@pthis[13] Using IShellLink_SetHotKey (pthis, pwHotkey) To HRESULT
          Function = HRESULT
        End Function
         
        
        ' =======================================================================================
        ' Saves the object into the specified file.
        ' =======================================================================================
        Declare Function Proto_IPersistFile_Save (ByVal pthis As Dword Ptr, ByVal pszFileName As Dword, ByVal fRemember As Long) As Long
        ' =======================================================================================
        Function IPersistFile_Save (ByVal pthis As Dword Ptr, ByVal strFileName As String, ByVal fRemember As Long) As Long
          Local HRESULT As Long
          Local pszFileName As Dword
          If Len(strFileName) Then
             strFileName = UCode$(strFileName) & $Nul
             pszFileName = StrPtr(strFileName)
          End If
          Call Dword @@pthis[6] Using Proto_IPersistFile_Save (pthis, pszFileName, fRemember) To HRESULT
          Function = HRESULT
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Creates a shortcut
        ' =======================================================================================
        Function CreateLink ( _
           ByVal csidl As Long _         ' // Value specifying the folder for which to retrieve the location. 
         , szLinkName As Asciiz _        ' // Name of the shortcut
         , szExePath As Asciiz _         ' // Path of the executable file
         , szArguments As Asciiz _       ' // Arguments
         , szWorkingDir As Asciiz _      ' // Working directory
         , ByVal nShowCmd As Dword _     ' // Show command flag
         , szComment As Asciiz _         ' // Comment
         , szHotKey As Word _          ' // HotKey <--- is this correct?
         ) As Long
        
           Local hr As Long                         ' // HRESULT
           Local psl As Dword                       ' // IShellLink interface reference
           Local ppf As Dword                       ' // IPersistFile interrace reference
           Local CLSID_ShellLink As Guid            ' // ShellLink class identifier
           Local IID_IShellLink As Guid             ' // IShellLink interface identifier
           Local IID_IPersistFile As Guid           ' // IPersistFile interface identifier
           Local pidl As Dword                      ' // Item identifier list specifying the folder location
           Local szFileName As Asciiz * %MAX_PATH   ' // Name of the .LNK file
        
           ' // Fills the guids
           CLSID_ShellLink = $CLSID_ShellLink
           IID_IShellLink = $IID_IShellLink
           IID_IPersistFile = $IID_IPersistFile
        
           ' // Creates an instance of the IShellLink interface
           hr = CoCreateInstance(CLSID_ShellLink, ByVal %Null, %CLSCTX_INPROC_SERVER, IID_IShellLink, psl)
           If hr <> %S_OK Then Exit Function
           
           ' // Sets the properties of the shortcut
           hr = IShellLink_SetPath(psl, szExePath)
           hr = IShellLink_SetArguments(psl, szArguments)
           hr = IShellLink_SetWorkingDirectory(psl, szWorkingDir)
           hr = IShellLink_SetShowCmd(psl, nShowCmd)
           hr = IShellLink_SetDescription(psl, szComment)
           hr = IShellLink_SetHotKey(psl, szHotKey)
           ' // Retrieves a pointer to the IPersistFile interface
           hr = IUnknown_QueryInterface(psl, IID_IPersistFile, ppf)
           If hr = %S_OK Then
              ' // Retrieves an item identifier list specifying the desktop folder location
              hr = SHGetSpecialFolderLocation(%HWND_DESKTOP, csidl, pidl)
              If hr = %NOERROR Then
                 ' // Retrieves the path from the item identifier list
                 hr = SHGetPathFromIDList(ByVal pidl, szFileName)
                 ' // Frees the memory allocated for the item identifier list
                 CoTaskMemFree pidl
                 If IsTrue(hr) Then
                    ' // Full path
                    szFileName = szFileName & "\" & szLinkName & ".LNK"
                    ' // Saves the shortcut file
                    hr = IPersistFile_Save(ppf, szFileName, %TRUE)
                 End If
                 Function = %TRUE
              End If
              ' // Releases the IPersistFile interface
              IUnknown_Release ppf
           End If
           
           ' // Releases the IShellLink interface
           IUnknown_Release psl
        
        End Function
        ' =======================================================================================
        
        ' =======================================================================================
        ' Main
        ' =======================================================================================
        Function PBMain
        
           Local hr As Long
           hr = CreateLink(%CSIDL_DESKTOP, "MyApplication", CurDir$ & "\MyApp.exe", _
                               "My Arguments", CurDir$, %SW_MAXIMIZE, "Shortcut to MyApplication.exe", erm,,,, %myhotkeycomination :-)	) '<-- hotkey???
           If IsTrue (hr)Then
              MSGBOX "See Desktop"
           Else
              MSGBOX "CreateLink failed"
           End If
        
        End Function
        ' =======================================================================================
        I'm not sure how to assemble the hotkey combination for say Ctrl + Alt + D (just as an example)

        Any ideas?

        Comment


        • #5
          ' The virtual key code is in the low-order byte, and the modifier flags are in the
          ' high-order byte. The modifier flags can be a combination of the following values:
          '
          ' HOTKEYF_ALT
          ' ALT key
          ' HOTKEYF_CONTROL
          ' CTRL key
          ' HOTKEYF_EXT
          ' Extended key
          ' HOTKEYF_SHIFT
          ' SHIFT key

          Code:
          %HOTKEYF_SHIFT = &H01
          %HOTKEYF_CONTROL = &H02
          %HOTKEYF_ALT = &H04
          ' To set Ctrl+Alt+D as the hot key make a word as follows:

          Code:
          DIM wHotKey AS WORD
          wHotKey = MAKWRD(ASC("D"), %HOTKEYF_CONTROL OR %HOTKEYF_ALT)

          ------------------
          Website: http://com.it-berater.org
          SED Editor, TypeLib Browser, Wrappers for ADO, DAO, ODBC, OLE DB, SQL-DMO, WebBrowser Control, MSHTML, HTML Editing, CDOEX, MSXML, WMI, MSAGENT, Flash Player, Task Scheduler, Accesibility, Structured Storage, WinHTTP, Microsoft ActiveX Controls (Data Binding, ADODC, Flex Grid, Hierarchical Flex Grid, Masked Edit Control, DataList, DataCombo, MAPI, INET, MCI, Winsock, Common Dialog, MSChart, Outlook View Control), and Microsoft Scripting Components.
          Forum: http://www.jose.it-berater.org/smfforum/index.php

          Comment


          • #6
            José,

            Where do you get those equates from?
            %HOTKEYF_CONTROL
            %HOTKEYF_ALT

            I saw them in the win32help but they don't appear in my win32api.inc


            I guess i just set them up manually with the values that you've given here?

            I really am a newbie at all this Still, i'll get there in the end.

            Cheers

            Ian


            ------------------


            [This message has been edited by ian mcallister (edited February 28, 2006).]

            Comment


            • #7
              This all works fine now!

              I thought i'd post the finished result for anyone who might be interested.

              Thanks José !!

              Code:
              #Dim All
              #Compile Exe
              #Include "Win32Api.inc"
              
              ' =======================================================================================
              ' Constants
              ' =======================================================================================
              %CLSCTX_INPROC_SERVER = &H1    ' Component is allowed in the same process space.
              ' =======================================================================================
              
              ' =======================================================================================
              ' IIDs
              ' =======================================================================================
              $CLSID_ShellLink = Guid$("{00021401-0000-0000-C000-000000000046}")
              $IID_IShellLink = Guid$("{000214EE-0000-0000-C000-000000000046}")
              $IID_IPersistFile = Guid$("{0000010B-0000-0000-C000-000000000046}")
              ' =======================================================================================
              
              ' =======================================================================================
              ' Returns a pointer to a specified interface on an object to which a client currently
              ' holds an interface pointer. 
              ' =======================================================================================
              Function IUnknown_QueryInterface (ByVal pthis As Dword Ptr, ByRef riid As Guid, ByRef ppvObj As Dword) As Long
                Local HRESULT As Long
                Call Dword @@pthis[0] Using IUnknown_QueryInterface (pthis, riid, ppvObj) To HRESULT
                Function = HRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Decrements the reference count for the calling interface on a object. If the reference
              ' count on the object falls to 0, the object is freed from memory.
              ' =======================================================================================
              Function IUnknown_Release (ByVal pthis As Dword Ptr) As Dword
                Local DWRESULT As Dword
                Call Dword @@pthis[2] Using IUnknown_Release (pthis) To   DWRESULT
                Function = DWRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Sets the description string for a Shell link object.
              ' =======================================================================================
              Function IShellLink_SetDescription (ByVal pthis As Dword Ptr, ByRef pszName As Asciiz) As Long
                Local HRESULT As Long
                Call Dword @@pthis[7] Using IShellLink_SetDescription (pthis, pszName) To HRESULT
                Function = HRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Sets the name of the working directory for a Shell link object.
              ' =======================================================================================
              Function IShellLink_SetWorkingDirectory (ByVal pthis As Dword Ptr, ByRef pszDir As Asciiz) As Long
                Local HRESULT As Long
                Call Dword @@pthis[9] Using IShellLink_SetWorkingDirectory (pthis, pszDir) To HRESULT
                Function = HRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Sets the command-line arguments for a Shell link object.
              ' =======================================================================================
              Function IShellLink_SetArguments (ByVal pthis As Dword Ptr, ByRef pszArgs As Asciiz) As Long
                Local HRESULT As Long
                Call Dword @@pthis[11] Using IShellLink_SetArguments (pthis, pszArgs) To HRESULT
                Function = HRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Sets the show command for a Shell link object. The show command sets the initial show
              ' state of the window.
              ' =======================================================================================
              Function IShellLink_SetShowCmd (ByVal pthis As Dword Ptr, ByVal iShowCmd As Long) As Long
                Local HRESULT As Long
                Call Dword @@pthis[15] Using IShellLink_SetShowCmd (pthis, iShowCmd) To HRESULT
                Function = HRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Sets the path and file name of a Shell link object.
              ' =======================================================================================
              Function IShellLink_SetPath (ByVal pthis As Dword Ptr, ByRef pszFile As Asciiz) As Long
                Local HRESULT As Long
                Call Dword @@pthis[20] Using IShellLink_SetPath (pthis, pszFile) To HRESULT
                Function = HRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Sets the hotkey of a Shell link object.
              ' =======================================================================================
              Function  IShellLink_SetHotKey (ByVal pthis As Dword Ptr, ByVal pwHotkey As Word) As Long
                Local HRESULT As Long
                Call Dword @@pthis[13] Using IShellLink_SetHotKey (pthis, pwHotkey) To HRESULT
                Function = HRESULT
              End Function
               
              
              ' =======================================================================================
              ' Saves the object into the specified file.
              ' =======================================================================================
              Declare Function Proto_IPersistFile_Save (ByVal pthis As Dword Ptr, ByVal pszFileName As Dword, ByVal fRemember As Long) As Long
              ' =======================================================================================
              Function IPersistFile_Save (ByVal pthis As Dword Ptr, ByVal strFileName As String, ByVal fRemember As Long) As Long
                Local HRESULT As Long
                Local pszFileName As Dword
                If Len(strFileName) Then
                   strFileName = UCode$(strFileName) & $Nul
                   pszFileName = StrPtr(strFileName)
                End If
                Call Dword @@pthis[6] Using Proto_IPersistFile_Save (pthis, pszFileName, fRemember) To HRESULT
                Function = HRESULT
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Creates a shortcut
              ' =======================================================================================
              Function CreateLink ( _
                 ByVal csidl As Long _         ' // Value specifying the folder for which to retrieve the location. 
               , szLinkName As Asciiz _        ' // Name of the shortcut
               , szExePath As Asciiz _         ' // Path of the executable file
               , szArguments As Asciiz _       ' // Arguments
               , szWorkingDir As Asciiz _      ' // Working directory
               , ByVal nShowCmd As Dword _     ' // Show command flag
               , szComment As Asciiz _         ' // Comment
               , szHotKey As Word _          ' // HotKey
               ) As Long
              
                 Local hr As Long                         ' // HRESULT
                 Local psl As Dword                       ' // IShellLink interface reference
                 Local ppf As Dword                       ' // IPersistFile interrace reference
                 Local CLSID_ShellLink As Guid            ' // ShellLink class identifier
                 Local IID_IShellLink As Guid             ' // IShellLink interface identifier
                 Local IID_IPersistFile As Guid           ' // IPersistFile interface identifier
                 Local pidl As Dword                      ' // Item identifier list specifying the folder location
                 Local szFileName As Asciiz * %MAX_PATH   ' // Name of the .LNK file
              
                 ' // Fills the guids
                 CLSID_ShellLink = $CLSID_ShellLink
                 IID_IShellLink = $IID_IShellLink
                 IID_IPersistFile = $IID_IPersistFile
              
                 ' // Creates an instance of the IShellLink interface
                 hr = CoCreateInstance(CLSID_ShellLink, ByVal %Null, %CLSCTX_INPROC_SERVER, IID_IShellLink, psl)
                 If hr <> %S_OK Then Exit Function
                 
                 ' // Sets the properties of the shortcut
                 hr = IShellLink_SetPath(psl, szExePath)
                 hr = IShellLink_SetArguments(psl, szArguments)
                 hr = IShellLink_SetWorkingDirectory(psl, szWorkingDir)
                 hr = IShellLink_SetShowCmd(psl, nShowCmd)
                 hr = IShellLink_SetDescription(psl, szComment)
                 hr = IShellLink_SetHotKey(psl, szHotKey)
                 ' // Retrieves a pointer to the IPersistFile interface
                 hr = IUnknown_QueryInterface(psl, IID_IPersistFile, ppf)
                 If hr = %S_OK Then
                    ' // Retrieves an item identifier list specifying the desktop folder location
                    hr = SHGetSpecialFolderLocation(%HWND_DESKTOP, csidl, pidl)
                    If hr = %NOERROR Then
                       ' // Retrieves the path from the item identifier list
                       hr = SHGetPathFromIDList(ByVal pidl, szFileName)
                       ' // Frees the memory allocated for the item identifier list
                       CoTaskMemFree pidl
                       If IsTrue(hr) Then
                          ' // Full path
                          szFileName = szFileName & "\" & szLinkName & ".LNK"
                          ' // Saves the shortcut file
                          hr = IPersistFile_Save(ppf, szFileName, %TRUE)
                       End If
                       Function = %TRUE
                    End If
                    ' // Releases the IPersistFile interface
                    IUnknown_Release ppf
                 End If
                 
                 ' // Releases the IShellLink interface
                 IUnknown_Release psl
              
              End Function
              ' =======================================================================================
              
              ' =======================================================================================
              ' Main
              ' =======================================================================================
              
              %HOTKEYF_SHIFT = &H01
              %HOTKEYF_CONTROL = &H02
              %HOTKEYF_ALT = &H04
              
              Function PBMain
                 Dim wHotKey As Word
                  wHotKey = MakWrd(Asc("D"), %HOTKEYF_CONTROL Or %HOTKEYF_ALT)
               
              
                 Local hr As Long
                 hr = CreateLink(%CSIDL_DESKTOP, "MyApplication", CurDir$ & "\MyApp.exe", _
                                     "My Arguments", CurDir$, %SW_SHOWNORMAL, "Shortcut to MyApplication.exe", wHotKey ) '<-- hotkey???
              'note the first bit "MyApplication" is what appears as text under the shortcut icon
              ' %SW_SHOWNORMAL causes the window to open nice and normally
              'when you right-click on the shortcut and choose 'properties' you'll see:
              ' "My Arguments" appear as command line arguments 
              ' "Shortcut to my application.exe" appears as comments
              'and you should see that the shortcut key Ctrl Alt + 'D' has been set.
              
                 If IsTrue (hr)Then
                    MSGBOX "See Desktop"
                 Else
                    MSGBOX "CreateLink failed"
                 End If
              
              End Function
              ' =======================================================================================
              [This message has been edited by ian mcallister (edited February 28, 2006).]

              Comment


              • #8
                This is very cool, but what do you use it for. Can you
                give me an example situation where you would use it?

                ------------------
                Kind Regards
                Mike

                Comment


                • #9
                  I've got a use for it right now.

                  Programmaticaly creating a desktop link to a file that the program has
                  just created, in this case a report.

                  ------------------

                  Comment

                  Working...
                  X