[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


VB Shortcut Key For Method

Posted on 2010-01-07
Medium Priority
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
  • 2
  • 2
LVL 33

Accepted Solution

Todd Gerbert earned 2000 total points
ID: 26205380
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

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

Author Comment

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

Expert Comment

by:Todd Gerbert
ID: 26207042
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


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

834 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