Click here to Skip to main content
6,635,160 members and growing! (15,989 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Multimedia     Intermediate License: The Code Project Open License (CPOL)

How to use your extra mouse buttons in games or apps

By Ladislav Nevery

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
C++, Windows
Posted:20 Feb 2008
Updated:20 Feb 2008
Views:17,645
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.39 Rating: 4.22 out of 5

1

2
2 votes, 20.0%
3
4 votes, 40.0%
4
4 votes, 40.0%
5

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)

About the Author

Ladislav Nevery


Member
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 [new game based on Unreal Engine 3]
Occupation: Software Developer (Senior)
Location: Slovakia Slovakia

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
QuestionIs the file removed? PinmemberShiine7:46 26 Oct '09  
Generalhow to unistall ? Pinmembergrizleygrizley4:13 4 Sep '08  
QuestionCould this be used for joystick buttons also? PinmemberdavidMelloWorkAddress2:57 26 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Feb 2008
Editor:
Copyright 2008 by Ladislav Nevery
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project