Solved

How can an application Global HotKey ?

Posted on 2007-03-30
14
955 Views
Last Modified: 2012-06-27
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
Comment
Question by:mjdeale
  • 7
  • 7
14 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18826366
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
 
LVL 1

Author Comment

by:mjdeale
ID: 18826643
It's not almost there, becausei have multiple windows and i want to use the same hotkey sets for the all the windows.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18826856
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
 
LVL 1

Author Comment

by:mjdeale
ID: 18837018
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837054
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837058
Unless you are using VB.Net 2003?
0
 
LVL 1

Author Comment

by:mjdeale
ID: 18837450
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:mjdeale
ID: 18837480
I tried to look to see if alt is down it does not work.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837516
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
 
LVL 1

Author Comment

by:mjdeale
ID: 18837645
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837791
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
 
LVL 1

Author Comment

by:mjdeale
ID: 18837819
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18837899
0
 
LVL 1

Author Comment

by:mjdeale
ID: 18837972
Thank you for your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now