Click here to Skip to main content
Click here to Skip to main content

How to use your extra mouse buttons in games or apps

, 20 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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 <span class="code-keyword"><windows.h></span>
#include <span class="code-keyword"><stdio.h></span>

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)

Share

About the Author

Ladislav Nevery
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

 
Suggestionhttp://automatemytasks.weebly.com PinmemberMember 1093440018-Jul-14 3:56 
QuestionIs the file removed? PinmemberShiine26-Oct-09 7:46 
Questionhow to unistall ? Pinmembergrizleygrizley4-Sep-08 4:13 
QuestionCould this be used for joystick buttons also? PinmemberdavidMelloWorkAddress26-Feb-08 2:57 
AnswerRe: Could this be used for joystick buttons also? PinmemberDavid Gvozdenovic26-Aug-12 7:44 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 20 Feb 2008
Article Copyright 2008 by Ladislav Nevery
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid