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

x
?
Solved

VB Shortcut Key For Method

Posted on 2010-01-07
4
Medium Priority
?
181 Views
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.
0
Comment
Question by:disrupt
  • 2
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
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.
0
 
LVL 16

Author Comment

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

Author Comment

by:disrupt
ID: 26206757
I was able to find it looking for Windows API's RegisterHotKey thanks !
0
 
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()

        Me.CreateHandle(cp)
        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)
        Else
            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)
        MyBase.WndProc(m)

        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
                Me.DestroyHandle()
            End If
            Try
                UnsetHotKey()
            Catch ex As Exception

            End Try
        End If
        Me.disposedValue = True
    End Sub

    Protected Overrides Sub Finalize()
        Dispose(False)
        MyBase.Finalize()
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

Open in new window

0

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