• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 986
  • Last Modified:

How can an application Global HotKey ?

Hi guys
I am trying to write Hotkeys that will be gloably functional to my application. it's a vb.net application. I have found global hot keys that overwrite windows Hotkeys, which i don't want to do, because the user of my application may want to use the windows hotkeys to do other tasks.
Is there anyone who has a solution to this? Thank you,
0
mjdeale
Asked:
mjdeale
  • 7
  • 7
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So you want a "hot key" that exists only in your application?

What version Vb.Net?   2005?  or 2003 (or below)?

Here is an example using VB.Net 2005:

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.F1
                            Debug.Print("Ctrl F1")
                            Return True

                        Case Keys.F2
                            Debug.Print("Ctrl F2")
                            Return True

                        Case Keys.F3
                            Debug.Print("Ctrl F3")
                            Return True

                        ' etc...

                    End Select
                End If

        End Select
    End Function

End Class
0
 
mjdealeAuthor Commented:
It's not almost there, becausei have multiple windows and i want to use the same hotkey sets for the all the windows.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want it to work across all forms in an application then use IMessageFilter:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(New MyMessageFilter)
    End Sub

    Public Class MyMessageFilter
        Implements IMessageFilter

        Private Const WM_KEYDOWN As Integer = &H100

        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage
            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.F1
                                Debug.Print("Ctrl F1")
                                Return True

                            Case Keys.F2
                                Debug.Print("Ctrl F2")
                                Return True

                            Case Keys.F3
                                Debug.Print("Ctrl F3")
                                Return True

                            ' etc...

                        End Select
                    End If

            End Select

            Return False
        End Function
    End Class

End Class
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mjdealeAuthor Commented:
How To Know The Modifiers? if i can get the key by m.WParam.ToInt32, how can i know the modifiers without setting a constant variable. because it seems that Alt is the integer 260 and i don't know all the constant
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thus my use of My.Computer.Keyboard.CtrlKeyDown() and My.Computer.Keyboard.AltKeyDown()...

That way you can just look for the key you are interested in and check the state of the Ctrl or Alt key when you see it.

So if you are looking for "Alt-B" then look for Keys.B and then use My.Computer.Keyboard.AltKeyDown() to see if Alt is down.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Unless you are using VB.Net 2003?
0
 
mjdealeAuthor Commented:
yeah, i agree but, the think is whenever you set the constant
Private Const WM_KEYDOWN As Integer = &H100
the constant has a value of 256 as integer. to retrieve a alt, you have to have a 260 constant. i am using vb.net 2005 team suite. and i don't know if all the WM_KeyDown values are the same.
because i discovered that the Alt is not &H100 is something else and i think it's &H103 and the others i don't know. Plus there the combination ALT + CTRL + SHift and so on. so how can i deal with this?
0
 
mjdealeAuthor Commented:
I tried to look to see if alt is down it does not work.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
WM_KEYDOWN is the MESSAGE and won't change for different keys...

So "m.Msg" will be equal to WM_KEYDOWN.

The actual key value is in the "wParam" parameter of "m":

    Select Case m.WParam.ToInt32

Tell me what key combination you are looking for and I'll show you the code.
0
 
mjdealeAuthor Commented:
I am Looking For Any Keys but the modifiers can be
Alt
Ctrl
Shift
ALT + CRtl
ALT + Shift
CRtl+ Shift
ALT + CRTL + SHift
Actually WM_KeyDown = &H100 does not detect the alt press key
i found that WM_KeyDOWN = &H104 will retrieve it
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok....&H104 is actually WM_SYSKEYDOWN:

    Private Const WM_SYSKEYDOWN = &H104

That will give the event when Alt is pressed.  It is considered a "system key" because it normally brings up the applications menu system.

So you would modify it like this:

    Public Class MyMessageFilter
        Implements IMessageFilter

        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_SYSKEYDOWN As Integer = &H104

        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage
            Select Case m.Msg
                Case WM_SYSKEYDOWN
                    Select Case m.WParam.ToInt32
                        Case Keys.Menu
                            Debug.Print("Alt")

                    End Select

                Case WM_KEYDOWN
                    Select Case m.WParam.ToInt32
                        Case Keys.ControlKey
                            Debug.Print("Control")

                        Case Keys.ShiftKey
                            Debug.Print("Shift")

                    End Select

            End Select

            Return False
        End Function

    End Class

But as to this question:

    "Plus there the combination ALT + CTRL + SHift and so on. so how can i deal with this?"

That depends on WHEN you want to check for this combination.  Do you want to know if those keys are down when another key (such as "A") is pressed?

Or do you want to know if these are down when something is clicked for instance?

0
 
mjdealeAuthor Commented:
actually i found it
here is what i am looking for:

Public Class ApplicationMessageFilter
    Implements IMessageFilter
    Private Const WM_KEYDOWN As Integer = &H100
    Private Const WM_ALTKEYDOWN As Integer = &H104

    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage
        Select Case m.Msg
            Case WM_ALTKEYDOWN
                If My.Computer.Keyboard.AltKeyDown And Not (My.Computer.Keyboard.ShiftKeyDown) And Not (My.Computer.Keyboard.CtrlKeyDown) Then
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox("Alt + " & CType(aKey, Keys).ToString("g"))
                    End If
                ElseIf My.Computer.Keyboard.ShiftKeyDown And Not (My.Computer.Keyboard.CtrlKeyDown) And My.Computer.Keyboard.AltKeyDown Then
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox("ALT + Shift + " & CType(aKey, Keys).ToString("g"))
                    End If
                End If
            Case WM_KEYDOWN
                If My.Computer.Keyboard.CtrlKeyDown And Not (My.Computer.Keyboard.ShiftKeyDown) And Not (My.Computer.Keyboard.AltKeyDown) Then
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox("Control + " & CType(aKey, Keys).ToString("g"))
                    End If
                ElseIf My.Computer.Keyboard.ShiftKeyDown And Not (My.Computer.Keyboard.CtrlKeyDown) And Not (My.Computer.Keyboard.AltKeyDown) Then
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox("Shift + " & CType(aKey, Keys).ToString("g"))
                    End If
                ElseIf My.Computer.Keyboard.ShiftKeyDown And My.Computer.Keyboard.CtrlKeyDown And Not (My.Computer.Keyboard.AltKeyDown) Then
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox("Shift + Ctrl + " & CType(aKey, Keys).ToString("g"))
                    End If
                ElseIf My.Computer.Keyboard.CtrlKeyDown And Not (My.Computer.Keyboard.ShiftKeyDown) And My.Computer.Keyboard.AltKeyDown Then
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox("ALT + Ctrl + " & CType(aKey, Keys).ToString("g"))
                    End If
                ElseIf My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.ShiftKeyDown And My.Computer.Keyboard.AltKeyDown Then
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox("ALT + Ctrl + Shift + " & CType(aKey, Keys).ToString("g"))
                    End If
                Else
                    Dim aKey As Keys = m.WParam.ToInt32
                    If aKey <> Keys.ShiftKey And aKey <> Keys.Alt And aKey <> Keys.Menu And aKey <> Keys.ControlKey Then
                        MsgBox(CType(aKey, Keys).ToString("g"))
                        Return True
                    End If
                End If
        End Select

        Return False
    End Function
End Class


This works for me unless you have an easier way of doing it
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
 
mjdealeAuthor Commented:
Thank you for your help.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now