Answered Registering 2 Hotkeys?

  • Tuesday, May 13, 2008 10:06 AM
     
     
    Hey, is it possible to register 2  hotkeys? If so how?

    Thankyou.

All Replies

  • Thursday, May 15, 2008 10:45 AM
     
     Answered

    Hi Travy92,

     

    You can P/Invoke windows API RegisterHotKey to register hotkeys such as ALT+D.

    Code Snippet

    Imports System.Runtime.InteropServices

     

    Public Class Form2

     

        Public Const MOD_ALT As Integer = &H1 'Alt key

        Public Const WM_HOTKEY As Integer = &H312

     

        Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer

        Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

       

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Call RegisterHotKey(Me.Handle, 9, MOD_ALT, Keys.D)

        End Sub

     

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

            If m.Msg = WM_HOTKEY Then

                MessgeBox.Show("You pressed ALT+D key combination")

            End If

            MyBase.WndProc(m)

        End Sub

     

        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

            Call UnregisterHotKey(Me.Handle, 9)

        End Sub

     

    End Class

    By the way, here are Virtual-Key Codes.

    http://msdn2.microsoft.com/en-us/library/ms927178.aspx

     

    Trackbac: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3333078&SiteID=1

     

     

    Best regards,

    Martin

  • Thursday, May 15, 2008 12:35 PM
     
     
    Hello! Thankyou for this. Also is it possible to register 2 hotkeys? like Alt+D and Alt+C?

    Thanks once again.
  • Friday, May 16, 2008 3:16 AM
     
     Answered

     Travy92 wrote:
    Also is it possible to register 2 hotkeys? like Alt+D and Alt+C?

     

    Hi Travy92,

     

    You can register multiple hotkeys such as Alt+D, Alt+C, etc. like this:

    Code Snippet

    Imports System.Runtime.InteropServices

     

    Public Class Form1

     

        Public Const MOD_ALT As Integer = &H1 'Alt key

        Public Const WM_HOTKEY As Integer = &H312

     

        <DllImport("User32.dll")> _

      Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer

        End Function

     

        <DllImport("User32.dll")> _

      Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

        End Function

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.D)

            RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.C)

        End Sub

     

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

            If m.Msg = WM_HOTKEY Then

                Dim id As IntPtr = m.WParam

                Select Case (id.ToString)

                    Case "100"

                        MessageBox.Show("You pressed ALT+D key combination")

                    Case "200"

                        MessageBox.Show("You pressed ALT+C key combination")

                End Select

            End If

            MyBase.WndProc(m)

        End Sub

       

        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

            UnregisterHotKey(Me.Handle, 100)

            UnregisterHotKey(Me.Handle, 200)

        End Sub

     

    End Class

     

    Best regards,

    Martin Xie

  • Friday, May 16, 2008 1:28 PM
     
     
    Thankyou! Hmm.. I tried this code:


    Code Snippet

    Public Class Hider
        Inherits System.Windows.Forms.Form

        Private Const SW_HIDE As Integer = 0
        Private Const SW_NORMAL As Integer = 1

        Private HotKeyID As Short
        Private HotKeyID2 As Short
        Private Const WM_HOTKEY As Integer = 786
        Private Const ModifierKey_ALT As Integer = 1

        Protected Friend Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Keys) As Integer
        Protected Friend Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

        Dim app_hwnd As Integer
        Dim wp As WINDOWPLACEMENT
        Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer
        Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer
        Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

        Private Structure POINTAPI
            Dim X As Integer
            Dim Y As Integer
        End Structure

        Private Structure RECT
            Dim Left_Renamed As Integer
            Dim Top_Renamed As Integer
            Dim Right_Renamed As Integer
            Dim Bottom_Renamed As Integer
        End Structure

        Private Structure WINDOWPLACEMENT
            Dim length As Integer
            Dim flags As Integer
            Dim showCmd As Integer
            Dim ptMinPosition As POINTAPI
            Dim ptMaxPosition As POINTAPI
            Dim rcNormalPosition As RECT
        End Structure

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox1.Items.Clear()

            RegisterHotKey(Me.Handle, HotKeyID, ModifierKey_ALT, Keys.H)
            RegisterHotKey(Me.Handle, HotKeyID2, ModifierKey_ALT, Keys.S)
        End Sub

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_HOTKEY Then
                Dim id As IntPtr = m.WParam
                Select Case (id.ToString)
                    Case HotKeyID
                        app_hwnd = FindWindow(vbNullString, ComboBox1.Text)
                        ComboBox1.Items.Add(ComboBox1.Text)
                        wp.length = Len(wp)
                        GetWindowPlacement(app_hwnd, wp)
                        wp.showCmd = SW_HIDE
                        SetWindowPlacement(app_hwnd, wp)
                        Hide()
                    Case HotKeyID2
                        app_hwnd = FindWindow(vbNullString, ComboBox1.Text)
                        wp.length = Len(wp)
                        GetWindowPlacement(app_hwnd, wp)
                        wp.showCmd = SW_NORMAL
                        SetWindowPlacement(app_hwnd, wp)
                        Show()
                End Select

            End If
            MyBase.WndProc(m)
        End Sub


    But for some reason its not working...?

    Any help?

    Thanks.

  • Monday, May 19, 2008 6:27 AM
     
     Answered

    Hi Travy,

     

    Please try the following code sample which works fine on my machine.

    Code Snippet

    Public Class Form1

     

        Private Const SW_HIDE As Integer = 0

        Private Const SW_NORMAL As Integer = 1

     

        ' Please set values

        Private HotKeyID As Short = 100

        Private HotKeyID2 As Short = 200

     

        Private Const WM_HOTKEY As Integer = 786

        Private Const ModifierKey_ALT As Integer = 1

     

        Protected Friend Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Keys) As Integer

        Protected Friend Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

     

        Dim app_hwnd As Integer

        Dim wp As WINDOWPLACEMENT

     

        Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

        Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

        Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer

        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

     

        Private Structure POINTAPI

            Dim X As Integer

            Dim Y As Integer

        End Structure

     

        Private Structure RECT

            Dim Left_Renamed As Integer

            Dim Top_Renamed As Integer

            Dim Right_Renamed As Integer

            Dim Bottom_Renamed As Integer

        End Structure

     

        Private Structure WINDOWPLACEMENT

            Dim length As Integer

            Dim flags As Integer

            Dim showCmd As Integer

            Dim ptMinPosition As POINTAPI

            Dim ptMaxPosition As POINTAPI

            Dim rcNormalPosition As RECT

        End Structure

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            ComboBox1.Items.Clear()

           

            RegisterHotKey(Me.Handle, HotKeyID, ModifierKey_ALT, Keys.H) ' Alt + H

            RegisterHotKey(Me.Handle, HotKeyID2, ModifierKey_ALT, Keys.S) ' Alt + S

        End Sub

     

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

            If m.Msg = WM_HOTKEY Then

                Dim id As IntPtr = m.WParam

                Select Case (id.ToString)

                    Case HotKeyID

                        MessageBox.Show("You pressed ALT+H key combination") ' Verify

                        app_hwnd = FindWindow(vbNullString, ComboBox1.Text)

                        ComboBox1.Items.Add(ComboBox1.Text)

                        wp.length = Len(wp)

                        GetWindowPlacement(app_hwnd, wp)

                        wp.showCmd = SW_HIDE

                        SetWindowPlacement(app_hwnd, wp)

                        Hide()

                    Case HotKeyID2

                        MessageBox.Show("You pressed ALT+S key combination") ' Verify

                        app_hwnd = FindWindow(vbNullString, ComboBox1.Text)

                        wp.length = Len(wp)

                        GetWindowPlacement(app_hwnd, wp)

                        wp.showCmd = SW_NORMAL

                        SetWindowPlacement(app_hwnd, wp)

                        Show()

                End Select

     

            End If

            MyBase.WndProc(m)

        End Sub

     

    End Class

     

    Regards,

    Martin

  • Monday, May 19, 2008 7:09 AM
     
     
    Thanks.
  • Wednesday, January 19, 2011 3:29 PM
     
     

    Hi Martin,

     

    I am in baffled in terms of registering the hotkey. I hope to get some advise upon this matter.

    I would like to register hot key (assume F8).  When user run the program, it will not show the interface but it will run in background (show icon in system tray). If user press F8, the program will perform button click in form1.  Based on this, I don't know how should I register my hotkey. As I can see from your example, most of registering part of hotkey is based on form loading. Can you advise me?

     

    Thank you 


    zafi