Click here to Skip to main content
15,914,014 members
Articles / Programming Languages / Visual Basic
Article

Fourth mouse button

Rate me:
Please Sign up or sign in to vote.
2.45/5 (7 votes)
1 Apr 20063 min read 47.2K   534   11   8
How to assign different actions to the fourth mouse button

Sample Image - FourthMouseButton.jpg

Introduction

I have a mouse with four buttons! This was an invitation to find a way to program what its fourth button does. The driver program bundled with the device had bugs, and it was a tempting idea to combine mouse hooks and keyboard key press simulation. Here, I present the result of my efforts in this regard.

Background

Let's break down our mission to two separate parts. (1) To capture the mouse button presses, check if it is the fourth mouse button pressed or not, and call a method on the occasions where that button is pressed; (2) To code the method which should be called by the first part. For our ease, I have chosen this method to have limited capabilities (although you can extend it the way you want.) I'm going to make this method simply simulate a key press. This key can be any member of the Keys collection (from a letter key like "a" to a more complex key like "BrowserHome".) For this example, I'm going to choose only four of these keys, which are more appropriate to be assigned to the fourth mouse button. These are: Browser Back, Browser Forward, Page Down, and Page Up.

Using the code

After downloading and extracting the ZIP file attached to this article (linked above), you will notice two files with the names LowLevelMouseHook.vb and Keyboard.vb, which are responsible for the first and second part of our mission, respectively.

As you may know, every time the user moves his mouse, presses a button on it, or rotates its wheel, a message is sent from Windows to the application which is active. We should create a mouse hook, which is a way to intercept this message transfer. This way, each message is sent to our application, before it proceeds to the active application. If that message is not a fourth mouse button press message, we simply let it proceed to the active application (by calling CallNextHookEx() function.) If this is a fourth mouse button press message (which can be either a fourth-mouse-button-down or a fourth-mouse-button-up message) we don't allow it to proceed to the active application. Instead, we will send the message we prefer. This is handel ed by the other piece of code, using the SimulateKeyDown() and SimulateKeyUp() methods. Here is the block of code used for this reason:

VB
//...
    Private Function LowLevelMouseProc( _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As MSLLHOOKSTRUCT) As Integer

        If (nCode = HC_ACTION) Then
            Select Case wParam
                Case WM_FOURTHMOUSEUP
                    SimulateKeyDown(theKey)
                    Return 1
                Case WM_FOURTHMOUSEDOWN
                    Return 1
                Case Else
                    Return CallNextHookEx(hhkLowLevelMouse, nCode, wParam, lParam)
            End Select
        End If
    End Function
//...

Using the demo project

It is straightforward. Simply choose what your fourth mouse button should do, from the drop down list, then minimize the mouse.exe program, and test your fourth mouse button when using another application (like a web browser.)

Points of Interest

When reviewing the code you may notice some blocks of code that are not used by the sample project indeed. They appear there to help you extend the program later.

Acknowledgements

I would like to thank two nice people names Mikey and Bob for their help in preparing the code and fixing the bugs.

History

March 1 2006: The first version uploaded

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFifth Mouse Button Pin
Rui Fernandes21-May-08 5:58
Rui Fernandes21-May-08 5:58 
GeneralFifth Mouse Button Pin
Rui Fernandes21-May-08 5:57
Rui Fernandes21-May-08 5:57 
General4th and 5th button Pin
Fabian Tang28-Jun-06 7:22
Fabian Tang28-Jun-06 7:22 
GeneralRe: 4th and 5th button Pin
Hojjat Salmasian3-Jul-06 6:58
Hojjat Salmasian3-Jul-06 6:58 
GeneralMouse Driver Pin
The_Mega_ZZTer1-Apr-06 13:22
The_Mega_ZZTer1-Apr-06 13:22 
GeneralRe: Mouse Driver Pin
Hojjat Salmasian1-Apr-06 18:50
Hojjat Salmasian1-Apr-06 18:50 
GeneralRe: Mouse Driver Pin
The_Mega_ZZTer1-Apr-06 19:44
The_Mega_ZZTer1-Apr-06 19:44 
GeneralRe: Mouse Driver Pin
Hojjat Salmasian2-Apr-06 4:31
Hojjat Salmasian2-Apr-06 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.