Click here to Skip to main content
Licence 
First Posted 1 Apr 2006
Views 31,437
Bookmarked 11 times

Fourth mouse button

By | 1 Apr 2006 | Article
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:

//...
    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

About the Author

Hojjat Salmasian

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFifth Mouse Button PinmemberRui Fernandes5:58 21 May '08  
GeneralFifth Mouse Button PinmemberRui Fernandes5:57 21 May '08  
General4th and 5th button PinmemberFabian Tang7:22 28 Jun '06  
GeneralRe: 4th and 5th button PinmemberHojjat Salmasian6:58 3 Jul '06  
GeneralMouse Driver PinmemberThe_Mega_ZZTer13:22 1 Apr '06  
Try not using the bundled drivers and let Windows select one for you. Every mouse I've used has worked fine with generic drivers, including my current 5-button 3-axis one.
GeneralRe: Mouse Driver PinmemberHojjat Salmasian18:50 1 Apr '06  
GeneralRe: Mouse Driver PinmemberThe_Mega_ZZTer19:44 1 Apr '06  
GeneralRe: Mouse Driver PinmemberHojjat Salmasian4:31 2 Apr '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 1 Apr 2006
Article Copyright 2006 by Hojjat Salmasian
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid