Click here to Skip to main content
15,884,425 members
Articles / Programming Languages / C++
Article

How to use your extra mouse buttons in games or apps

Rate me:
Please Sign up or sign in to vote.
4.21/5 (11 votes)
20 Feb 2008CPOL2 min read 49K   17   4
So you have your brand new El-Cheapo mouse with a zillion of additional mouse buttons yet you can't use them in games. Lets see one solution to this problem

Introduction

As a fan of World Of Warcraft game I recently went shoping for mouse with extra buttons. The idea was that binding extra spells in game to those buttons will radically improve my reaction times. Such was the theory. But as I unpacked my brand new mouse and started so called mouse configuration panel i found out that only 2 of additional 6 extra buttons were programmable . And even those 2 had just limited assignable functionality such as "start email client" etc. Errrr

Background

But I am curious person and i started to study this driver software. Soon i discovered that majority of the cheap mouses use the same drivers. So if you see KMProcess running you are a lucky guy. By launching usefull microsoft Spy++ I also soon found out that for each extra mouse button pressed this universal mouse driver generates custom WM_USER based message. So the idea was to capture those messages and react by emulating driver level keyboard imput so those buttons will be seen as regullar bindable keyboard buttons to games.

If you don't have KMProcess running don't worry. There is second and what is more important vendor / driver independent method. I will add code sample for this method soon so stay in touch

Using the code

The code for KMProcess method itself is very simple. It captures the internal custom mouse button mesages and translates it to F1-F6 key presses. But nothing holds you from lets-say executing whatever different action you wish. You can even simulate mouse movements or multiple key sequences to games by using function SendInput() instead of used keybd_event()
#include <windows.h>
#include <stdio.h>

HHOOK  hook       =  0;
char   target[32] = {0};
int    last       =  0; 

void btn(int i,int up) { 
    if(3*up+i != last)  keybd_event(VK_F1+i,MapVirtualKey(VK_F1+i,0),up,0);
    last = 3*up+i;
}

LRESULT  CALLBACK proc(int disabled,WPARAM wp,LPARAM lp) {
    int *m = (int *)lp;
    if (!disabled) {
        if( m[1] == 0x0465 && m[2] == 0x0000 && m[3] == 2 )   btn(0,0); 
        if( m[1] == 0x0464 && m[2] == 0x0000 && m[3] == 2 )   btn(0,2); 
        if( m[1] == 0x0465 && m[2] == 0x0000 && m[3] == 4 )   btn(1,0); 
        if( m[1] == 0x0464 && m[2] == 0x0000 && m[3] == 4 )   btn(1,2); 
        if( m[1] == 0x1f58 && m[2] == 0x1b70 && m[3] == 0 )   btn(2,0); 
        if( m[1] == 0x1f58 && m[2] == 0x1b70 && m[3] == 1 )   btn(2,2); 
        if( m[1] == 0x1f58 && m[2] == 0x1b71 && m[3] == 0 )   btn(3,0); 
        if( m[1] == 0x1f58 && m[2] == 0x1b71 && m[3] == 1 )   btn(3,2); 
        if( m[1] == 0x1f58 && m[2] == 0x1b72 && m[3] == 0 ) { btn(4,0); Sleep(10); btn(4,2); } 
        if( m[1] == 0x1f58 && m[2] == 0x1b73 && m[3] == 0 ) { btn(5,0); Sleep(10); btn(5,2); } 
    }
    return CallNextHookEx(hook,disabled,wp,lp);
}

extern "C" __declspec(dllexport) void inst(HWND, HINSTANCE, char* cmd){
    if(!cmd||hook) return;  strncpy(target,cmd,sizeof(target));
    hook   = SetWindowsHookEx(WH_GETMESSAGE,proc,GetModuleHandle("hook"),0);
    MSG msg; while(GetMessage(&msg,0,0,0)) { TranslateMessage( &msg ); DispatchMessage(  &msg ); Sleep(10); }
}

long __stdcall DllMain( HINSTANCE dll,DWORD  reason){ 
    return strstr(GetCommandLine(),target) ? 1 : 0;
}

Points of Interest

Just build dll in whatever developmnet environment you have copy it do windows dir and install it from Start->Run.. Dialog

rundll32 hook,inst KMProcess

or from command line

start rundll32 hook,inst KMProcess

where hook is the name of dll and KMProcess is the name of the mouse driver process

Also if you are a not programmer don't worry. Just extract included zip file and run bat file.

History

As soon as I will have time I will add second method plus version with some nice Gui allowing to record and play mouse and keyboard macros on any mouse button press.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Slovakia Slovakia
Past Projects:
[Siemens.sk]Mobile network software: HLR-Inovation for telering.at (Corba)
Medical software: CorRea module for CT scanner
[cauldron.sk]Computer Games:XboxLive/net code for Conan, Knights of the temple II, GeneTroopers, CivilWar, Soldier of fortune II
[www.elveon.com]Computer Games:XboxLive/net code for Elveon game based on Unreal Engine 3
ESET Reasearch.
Looking for job

Comments and Discussions

 
QuestionIs the file removed? Pin
Shiine26-Oct-09 6:46
Shiine26-Oct-09 6:46 
Questionhow to unistall ? Pin
grizleygrizley4-Sep-08 3:13
grizleygrizley4-Sep-08 3:13 
QuestionCould this be used for joystick buttons also? Pin
davidMelloWorkAddress26-Feb-08 1:57
davidMelloWorkAddress26-Feb-08 1:57 
AnswerRe: Could this be used for joystick buttons also? Pin
David Gvozdenovic26-Aug-12 6:44
David Gvozdenovic26-Aug-12 6:44 

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.