Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to add a hot key for a program in VB 2005

Posted on 2010-09-08
12
Medium Priority
?
655 Views
Last Modified: 2012-05-10
Im trying to create a "HOT KEY" to display a new page in my software program. For some reason the code below doe not get activated when i hold down the hot key combination.
Private Sub frmSearch_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.H And e.Modifiers = Keys.Control Then
            frmnewwindow.show
            e.Handled = True
        End If

    End Sub

Open in new window

0
Comment
Question by:cmdolcet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33628198
try this

imports System.Windows.Form
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = System.Windows.Forms.Keys.F11 Then
            MsgBox("F11 Key was pressed")
        End If
    End Sub

'Or
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Return Then
            MsgBox("Enter Key was pressed")
        End If
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33628219
Also thereis good code in here
http://www.daniweb.com/forums/thread133925.html
0
 

Author Comment

by:cmdolcet
ID: 33628316
jtoutou: I tried your code and neither one worked??? I will look at the link you provided by do you ahve a reason why they didn;t work?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33628406
give this a try ...
it is ALT+E and Ctrl+F
..Also youmay read this
http://social.msdn.microsoft.com/Forums/en-US/vbide/thread/30b5caa4-35d5-4eec-b590-c2f041293fc9
Imports System.Runtime.InteropServices
 
Public Class Form1
 
    Public Const MOD_ALT As Integer = &H1 'Alt key
    Public Const MOD_Ctrl As Integer = &H2 'Ctrl 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.E)
 
        RegisterHotKey(Me.Handle, 200, MOD_Ctrl, Keys.F)
    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+E key combination")
                Case "200"
                    MessageBox.Show("You pressed Ctrl+F 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

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33629126
*Set the KeyPreview() Property of your FORM to True and your original code should work!...  =)
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33629155
ooooooo God!!!!!!!!   :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33629163
Lol...if he needs the "hotkey" to work OUTSIDE his app then he'll need your approach!  =)
*or a low-level keyboard hook...
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33629176
Ctrl-H is actually a BACKSPACE combo so if you're inside a TextBox it will do backspace entry for you.  Setting KeyPreview to True will allow you to trap the combo at the form level before it reaches the other controls.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33629181
You are absolutelly Right!!!Thank you Very much!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33629212
You can also leave KeyPreview() False and do it this way intead:
Public Class Form1

    Private Const WM_KEYDOWN As Integer = &H100

    Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
        Select Case m.Msg
            Case WM_KEYDOWN
                If My.Computer.Keyboard.CtrlKeyDown AndAlso (Not My.Computer.Keyboard.AltKeyDown) Then
                    Select Case m.WParam.ToInt32
                        Case Keys.H
                            If My.Computer.Keyboard.CtrlKeyDown AndAlso Not My.Computer.Keyboard.AltKeyDown AndAlso Not My.Computer.Keyboard.ShiftKeyDown Then
                                frmnewwindow.Show()
                            End If
                            Return True ' <-- if you want to supress the keystroke as well

                            ' etc...

                    End Select
                End If

        End Select

        Return False
    End Function

End Class

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33629235
Got crazy with the cut & paste in there...too many checks for Ctrl/Alt/Shift!  Hahaha....   =\

It should be:
Public Class Form1

    Private Const WM_KEYDOWN As Integer = &H100

    Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
        Select Case m.Msg
            Case WM_KEYDOWN
                Select Case m.WParam.ToInt32
                    Case Keys.H
                        If My.Computer.Keyboard.CtrlKeyDown AndAlso (Not My.Computer.Keyboard.AltKeyDown) AndAlso (Not My.Computer.Keyboard.ShiftKeyDown) Then
                            frmnewwindow.Show()
                            Return True ' <-- if you want to supress the keystroke as well

                            ' etc...
                        End If  
                End Select
        End Select

        Return False
    End Function

End Class

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33629243
@cmdolcet ...You Should Increase the points to maximum with "RESPECT" Accept @Idle's Solution!!!!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

705 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