none
How to convert the hotkey integer to keys? RRS feed

Answers

  •  The first example below shows how to get the hotkey as a combination of Keys from the Keys enumeration.  The second example shows how to get the hotkey as a String that can be used to display the hotkey.

    Imports Shell32 'Add a Reference to (Microsoft Shell Controls And Automation) in the (Com) tab of the (Add Reference) dialog window for this Namespace.
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Filename As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "The Shortcut File.url")
            Dim hotkey As Keys = GetShortcutHotkeyAsKeys(Filename)
            Label1.Text = hotkey.ToString
        End Sub
    
        Private Function GetShortcutHotkeyAsKeys(ShortcutFilename As String) As Keys
            Dim objShell As New Shell
            Dim objFolder As Folder = objShell.NameSpace(New IO.FileInfo(ShortcutFilename).DirectoryName)
            Dim Hotkey As Keys = Keys.None
            If objFolder IsNot Nothing Then
                Dim objFolderItem As FolderItem = objFolder.ParseName(IO.Path.GetFileName(ShortcutFilename))
                If objFolderItem IsNot Nothing Then
                    Dim objShellLink As ShellLinkObject = CType(objFolderItem.GetLink, ShellLinkObject)
                    If objShellLink IsNot Nothing Then
                        Dim nHotkey As Integer = objShellLink.Hotkey
                        Dim Lo As Byte = CByte((nHotkey >> 8) And &HFF)
                        Dim Hi As Byte = CByte(nHotkey And &HFF)
                        If (Lo And Modifiers.Shft) = Modifiers.Shft Then Hotkey = (Hotkey Or Keys.Shift)
                        If (Lo And Modifiers.Ctrl) = Modifiers.Ctrl Then Hotkey = (Hotkey Or Keys.Control)
                        If (Lo And Modifiers.Alt) = Modifiers.Alt Then Hotkey = (Hotkey Or Keys.Alt)
                        Hotkey = (Hotkey Or CType(Hi, Keys))
                    End If
                    ReleaseComObj(objShellLink)
                End If
                ReleaseComObj(objFolderItem)
            End If
            ReleaseComObj(objFolder)
            ReleaseComObj(objShell)
            Return Hotkey
        End Function
    
        Private Sub ReleaseComObj(ComObj As Object)
            Marshal.ReleaseComObject(ComObj)
            ComObj = Nothing
        End Sub
    
        Private Enum Modifiers As Byte
            Shft = 1
            Ctrl = 2
            Alt = 4
            Extended = 8
        End Enum
    End Class
    
     

     
    Imports Shell32 'Add a Reference to (Microsoft Shell Controls And Automation) in the (Com) tab of the (Add Reference) dialog window for this Namespace.
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Filename As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "The Shortcut File.url")
            Label1.Text = GetShortcutHotkeyAsString(Filename)
        End Sub
    
        Private Function GetShortcutHotkeyAsString(ShortcutFilename As String) As String
            Dim objShell As New Shell
            Dim objFolder As Folder = objShell.NameSpace(New IO.FileInfo(ShortcutFilename).DirectoryName)
            Dim HotkeyString As String = ""
            If objFolder IsNot Nothing Then
                Dim objFolderItem As FolderItem = objFolder.ParseName(IO.Path.GetFileName(ShortcutFilename))
                If objFolderItem IsNot Nothing Then
                    Dim objShellLink As ShellLinkObject = CType(objFolderItem.GetLink, ShellLinkObject)
                    If objShellLink IsNot Nothing Then
                        Dim nHotkey As Integer = objShellLink.Hotkey
                        Dim Lo As Byte = CByte((nHotkey >> 8) And &HFF)
                        Dim Hi As Byte = CByte(nHotkey And &HFF)
                        If (Lo And Modifiers.Shft) = Modifiers.Shft Then HotkeyString &= "Shft + "
                        If (Lo And Modifiers.Ctrl) = Modifiers.Ctrl Then HotkeyString &= "Ctrl + "
                        If (Lo And Modifiers.Alt) = Modifiers.Alt Then HotkeyString &= "Alt + "
                        HotkeyString &= CType(Hi, Keys).ToString
                    End If
                    ReleaseComObj(objShellLink)
                End If
                ReleaseComObj(objFolderItem)
            End If
            ReleaseComObj(objFolder)
            ReleaseComObj(objShell)
            Return HotkeyString
        End Function
    
        Private Sub ReleaseComObj(ComObj As Object)
            Marshal.ReleaseComObject(ComObj)
            ComObj = Nothing
        End Sub
    
        Private Enum Modifiers As Byte
            Shft = 1
            Ctrl = 2
            Alt = 4
            Extended = 8
        End Enum
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by gaxjyxq Wednesday, May 10, 2017 9:10 PM
    Wednesday, May 10, 2017 8:02 PM

All replies

  • If you mean Keys enumeration, then check this example:

    Dim i = 1867
    Dim key As Keys = CType((i And &HFF) Or If((i And 1) <> 0, Keys.LShiftKey, 0) Or If((i And 2) <> 0, Keys.LControlKey, 0) Or If((i And 4) <> 0, Keys.LMenu, 0), Keys)
    


    • Edited by Viorel_MVP Wednesday, May 10, 2017 6:27 PM
    Wednesday, May 10, 2017 6:27 PM
  •  The first example below shows how to get the hotkey as a combination of Keys from the Keys enumeration.  The second example shows how to get the hotkey as a String that can be used to display the hotkey.

    Imports Shell32 'Add a Reference to (Microsoft Shell Controls And Automation) in the (Com) tab of the (Add Reference) dialog window for this Namespace.
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Filename As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "The Shortcut File.url")
            Dim hotkey As Keys = GetShortcutHotkeyAsKeys(Filename)
            Label1.Text = hotkey.ToString
        End Sub
    
        Private Function GetShortcutHotkeyAsKeys(ShortcutFilename As String) As Keys
            Dim objShell As New Shell
            Dim objFolder As Folder = objShell.NameSpace(New IO.FileInfo(ShortcutFilename).DirectoryName)
            Dim Hotkey As Keys = Keys.None
            If objFolder IsNot Nothing Then
                Dim objFolderItem As FolderItem = objFolder.ParseName(IO.Path.GetFileName(ShortcutFilename))
                If objFolderItem IsNot Nothing Then
                    Dim objShellLink As ShellLinkObject = CType(objFolderItem.GetLink, ShellLinkObject)
                    If objShellLink IsNot Nothing Then
                        Dim nHotkey As Integer = objShellLink.Hotkey
                        Dim Lo As Byte = CByte((nHotkey >> 8) And &HFF)
                        Dim Hi As Byte = CByte(nHotkey And &HFF)
                        If (Lo And Modifiers.Shft) = Modifiers.Shft Then Hotkey = (Hotkey Or Keys.Shift)
                        If (Lo And Modifiers.Ctrl) = Modifiers.Ctrl Then Hotkey = (Hotkey Or Keys.Control)
                        If (Lo And Modifiers.Alt) = Modifiers.Alt Then Hotkey = (Hotkey Or Keys.Alt)
                        Hotkey = (Hotkey Or CType(Hi, Keys))
                    End If
                    ReleaseComObj(objShellLink)
                End If
                ReleaseComObj(objFolderItem)
            End If
            ReleaseComObj(objFolder)
            ReleaseComObj(objShell)
            Return Hotkey
        End Function
    
        Private Sub ReleaseComObj(ComObj As Object)
            Marshal.ReleaseComObject(ComObj)
            ComObj = Nothing
        End Sub
    
        Private Enum Modifiers As Byte
            Shft = 1
            Ctrl = 2
            Alt = 4
            Extended = 8
        End Enum
    End Class
    
     

     
    Imports Shell32 'Add a Reference to (Microsoft Shell Controls And Automation) in the (Com) tab of the (Add Reference) dialog window for this Namespace.
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Filename As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "The Shortcut File.url")
            Label1.Text = GetShortcutHotkeyAsString(Filename)
        End Sub
    
        Private Function GetShortcutHotkeyAsString(ShortcutFilename As String) As String
            Dim objShell As New Shell
            Dim objFolder As Folder = objShell.NameSpace(New IO.FileInfo(ShortcutFilename).DirectoryName)
            Dim HotkeyString As String = ""
            If objFolder IsNot Nothing Then
                Dim objFolderItem As FolderItem = objFolder.ParseName(IO.Path.GetFileName(ShortcutFilename))
                If objFolderItem IsNot Nothing Then
                    Dim objShellLink As ShellLinkObject = CType(objFolderItem.GetLink, ShellLinkObject)
                    If objShellLink IsNot Nothing Then
                        Dim nHotkey As Integer = objShellLink.Hotkey
                        Dim Lo As Byte = CByte((nHotkey >> 8) And &HFF)
                        Dim Hi As Byte = CByte(nHotkey And &HFF)
                        If (Lo And Modifiers.Shft) = Modifiers.Shft Then HotkeyString &= "Shft + "
                        If (Lo And Modifiers.Ctrl) = Modifiers.Ctrl Then HotkeyString &= "Ctrl + "
                        If (Lo And Modifiers.Alt) = Modifiers.Alt Then HotkeyString &= "Alt + "
                        HotkeyString &= CType(Hi, Keys).ToString
                    End If
                    ReleaseComObj(objShellLink)
                End If
                ReleaseComObj(objFolderItem)
            End If
            ReleaseComObj(objFolder)
            ReleaseComObj(objShell)
            Return HotkeyString
        End Function
    
        Private Sub ReleaseComObj(ComObj As Object)
            Marshal.ReleaseComObject(ComObj)
            ComObj = Nothing
        End Sub
    
        Private Enum Modifiers As Byte
            Shft = 1
            Ctrl = 2
            Alt = 4
            Extended = 8
        End Enum
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by gaxjyxq Wednesday, May 10, 2017 9:10 PM
    Wednesday, May 10, 2017 8:02 PM
  • IR,

    Good to see you again. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 10, 2017 8:19 PM
  • IR,

    Good to see you again. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering


     Thanks Frank.  I have not been quite in the right frame of mind to do a lot lately.  8)

    If you say it can`t be done then i`ll try it

    Wednesday, May 10, 2017 8:33 PM


  •  Thanks Frank.  I have not been quite in the right frame of mind to do a lot lately.  8)

    If you say it can`t be done then i`ll try it

    I hear ya.

    I hope you'll stick around more. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 10, 2017 8:38 PM