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,
LVL 1
mjdealeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjdealeAuthor Commented:
Thank you for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.