VB Shortcut Key For Method

Posted on 2010-01-07
Last Modified: 2013-12-26
I 'm using Visual Basic 2008 and I have a program that sits in the tray and I want to execute a method by pressing shift + f5.  How can I do this? Basically I want the keyboard shortcut to work even when the program is not in focus and sitting in the tray.
Question by:disrupt
    LVL 33

    Accepted Solution

    You need to look into the Windows API's RegisterHotKey and UnregisterHotKey.

    I've done this by creating my own class that inherits from NativeWindow. I have C# code if you're interested.
    LVL 16

    Author Comment

    sure would like to see how it's done in c#
    LVL 16

    Author Comment

    I was able to find it looking for Windows API's RegisterHotKey thanks !
    LVL 33

    Expert Comment

    by:Todd Gerbert
    Well, here's some VB code for ya.  Wasn't too complicated so I was able to move into VB.Net, but I'm not particularly comfortable with VB.Net, so it may need a little tweaking.

    You can use this class to register a global hotkey, and it will raise an event.  Use it like this:

    Public Class Form1
        Private WithEvents ghk As GlobalHotkey
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ghk = New GlobalHotkey()
            ghk.SetHotKey(Keys.F5, GlobalHotkey.KeyModifiers.MOD_SHIFT)
        End Sub

        Private Sub ghk_HotKeyPress(ByVal sender As Object, ByVal e As System.EventArgs) Handles ghk.HotKeyPress
            MessageBox.Show("F5 KeyPress Detected")
        End Sub
    End Class
    Public Class GlobalHotkey
        Inherits NativeWindow
        Implements IDisposable
        Private Const WM_HOTKEY As Integer = &H312
        Public Event HotKeyPress As EventHandler
        <Flags()> _
        Public Enum KeyModifiers As Integer
            None = &H0
            MOD_ALT = &H1
            MOD_CONTROL = &H2
            MOD_SHIFT = &H4
            MOD_WIN = &H8
        End Enum
        Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As KeyModifiers, ByVal vk As Integer) As Boolean
        Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
        Private cp As CreateParams
        Private hotKeyHandle As IntPtr
        Private hotKeyId As Integer
        Private hKey As Boolean = False
        Public Sub New()
            cp = New CreateParams()
            hotKeyId = Me.GetHashCode()
            hotKeyHandle = Me.Handle
        End Sub
        Public Function SetHotKey(ByVal Key As System.Windows.Forms.Keys, ByVal Modifiers As KeyModifiers) As Boolean
            hKey = RegisterHotKey(hotKeyHandle, hotKeyId, Modifiers, Key)
            Return hKey
        End Function
        Public Function UnsetHotKey() As Boolean
            If hKey Then
                Return UnregisterHotKey(hotKeyHandle, hotKeyId)
                Return True
            End If
        End Function
        Protected Overridable Sub OnHotKeyPress(ByVal e As EventArgs)
            RaiseEvent HotKeyPress(Me, e)
        End Sub
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_HOTKEY Then
                OnHotKeyPress(New EventArgs())
            End If
        End Sub
    #Region "IDisposable Support"
        Private disposedValue As Boolean = False ' To detect redundant calls
        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                End If
                Catch ex As Exception
                End Try
            End If
            Me.disposedValue = True
        End Sub
        Protected Overrides Sub Finalize()
        End Sub
        Public Sub Dispose() Implements IDisposable.Dispose
        End Sub
    #End Region
    End Class

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now