Fourth mouse button






2.45/5 (7 votes)
Apr 1, 2006
3 min read

47553

535
How to assign different actions to the fourth mouse button
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