It was a big step forward thrusting computer from command line execution to hot and happening menu driven applications that we see today. Many GUI were based on keyboards but they never outweighed their opponents based on mouse reason is simple, just imagine your Windows or Linux to be keyboard driven. Nothing less than a nightmare it seems.
Mouse is simple input device which allows you to point and execute interface and is faster than keyboards many times. Mouse generally provide random interface on other hand keyboards provide sequential interface.
This article will act as a complete source of Mouse Programming in C and covers it all aspects. If we still miss something please inform us and we will try to cover it next revised edition.
Mouse Programming is a topic which every c programmer from beginner to professional needs to have in his toolbox to have a cutting edge.
Mouse Programming will be used almost everywhere. It will embedded in games programming to commercial valued applications. So if you aim to easily understand other tutorials in this website you must master this topics and believe me its very easy.
- Requirements
First and Foremost requirement which I believe will surely be satisfied, you should have a mouse attached to your system.
This tutorial is written Turbo C++ 3.0 IDE and install in folder C:\TC. I recommend to use same IDE and settings to avoid any incompatibility.
You should have proper mouse driver for your hardware, this is also not a problem because almost 100% of operating System provide them through program MOUSE.COM or WITTYMS.COM.
That is all you need.
- Fundamental Knowledge
Before we start hardcore programming we must first understand some principles on which mouse programming is based.
First thing you must know how to tell a mouse to do anything. In actual we doesn't communicate with mouse directly but through the driver provide. We use interrupts to get access to this driver. Each device provide by computer has its own port and more or less we access these ports.
Each device has a unique port which is a hexadecimal value and value is designed to be machine independent enhancing portability of program.
Mouse has port attached to it 0X33 and similarly keyboard has attach to it port 0X60. We restrict ourself only to mouse port in this tutorial but keyboard is also used extensively in other programs especially ones related to games programming.
We also make use of address registers. These are basically Union of type REGS defined in dos.h. You don't need further knowledge of them for programming. Just remember we use two register to communicate to a device driver using two registers one for input and one for output.
We send value to device driver through the input register and receive information in it embedded in output register.
- AX Register
We can access various mouse functions using different values of AX input Register and passing those values to mouse port using a interrupt
The Functions are listed below - Here AX, BX, CX and DX are members of Union REGS and more or less integers.
Now we step by step study each of these services in following sections and build up our combined code.Input Function Performed Returns AX = 0 Get Mouse Status Output AX Value = FFFFh if mouse support is available; Output AX Value = 0 if mouse support is not available. AX = 1 Show Mouse Pointer Nothing AX = 2 Hide Mouse Pointer Nothing AX = 3 Mouse Position CX = Mouse X Coordinate; DX = Mouse Y Coordinate Ax = 3 Mouse Button Press BX = 0 No Key Is Pressed; BX = 1 Left Button is Pressed; BX = 2 Right Button is Pressed; BX = 3 Centre Button is Pressed Ax = 7; CX = MaxX1; DX =MaxX2 Set Horizontal Limit Nothing Ax = 8; CX = MaxX1; DX =MaxX2 Set Vertical Limit Nothing
- Detect Mouse
Before you start your embed mouse program you should always check whether the mouse programming is supported on not.
If some how mouse fails to initialise you should always make sure that either program terminates or employ a error handling approach that maybe shift to keyboard interface .
To do mouse programming you must include <dos.h>. We use a function called int86 to access interupts.
To detect mouse we use a function name detectmouse which has following code -Code: Select all
#include <dos.h> union REGS in, out; void detectmouse () { in.x.ax = 0; int86 (0X33,&in,&out); if (out.x.ax == 0) printf ("\nMouse Fail To Initialize"); else printf ("\nMouse Succesfully Initialize"); } int main () { detectmouse (); getch (); return 0; }
- Showing and Hiding Mouse
Now first we show mouse on screen. Mouse works both in text mode and graphic mode. In text mode it looks like a square while in graphics mode it looks like a pointer. Here is a screen shot for text mode. It was produced from adding a function showmousetext to above code so code becomes -Code: Select all
#include <dos.h> union REGS in, out; void detectmouse () { in.x.ax = 0; int86 (0X33,&in,&out); if (out.x.ax == 0) printf ("\nMouse Fail To Initialize"); else printf ("\nMouse Succesfully Initialize"); } void showmousetext () { in.x.ax = 1; int86 (0X33,&in,&out); } int main () { detectmouse (); showmouse (); getch (); return 0; }
Here is a screen shot for graphic mode - This is achieved using a function showmousegraphic added to above code while removing showmouse text from main.
Code: Select all
#include <dos.h> #include <graphics.h> union REGS in, out; void detectmouse () { in.x.ax = 0; int86 (0X33,&in,&out); if (out.x.ax == 0) printf ("\nMouse Fail To Initialize"); else printf ("\nMouse Succesfully Initialize"); } void showmousetext () { in.x.ax = 1; int86 (0X33,&in,&out); } void showmousegraphics () { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); in.x.ax = 1; int86 (0X33,&in,&out); getch (); closegraph (); } int main () { detectmouse (); showmousegraphics (); getch (); return 0; }
Next we do relatively simple task of hiding mouse using a function hidemouse as shown below -Code: Select all
#include <dos.h> #include <graphics.h> union REGS in, out; void detectmouse () { in.x.ax = 0; int86 (0X33,&in,&out); if (out.x.ax == 0) printf ("\nMouse Fail To Initialize"); else printf ("\nMouse Succesfully Initialize"); } void showmousetext () { in.x.ax = 1; int86 (0X33,&in,&out); } void showmousegraphics () { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); in.x.ax = 1; int86 (0X33,&in,&out); getch (); closegraph (); } void hidemouse () { in.x.ax = 2; int86 (0X33,&in,&out); } int main () { detectmouse (); showmousegraphics (); hidemouse (); getch (); return 0; }
- Detect Input
We will now work on a important aspect of mouse programming detecting clicks.
We make use of an additional function known as kbhit ( ). This functions returns zero till any keypress and when a key is press it returns 1.
We use kbhit to run a infinite while loop.
For detecting mouseclicks we use a function called detect which displays on screen the respective button clicked. Press any keyboad key to exit the loop.
Code: Select all
#include <dos.h> #include <graphics.h> union REGS in, out; void detectmouse () { in.x.ax = 0; int86 (0X33,&in,&out); if (out.x.ax == 0) printf ("\nMouse Fail To Initialize"); else printf ("\nMouse Succesfully Initialize"); } void showmousetext () { in.x.ax = 1; int86 (0X33,&in,&out); } void showmousegraphics () { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); in.x.ax = 1; int86 (0X33,&in,&out); getch (); closegraph (); } void hidemouse () { in.x.ax = 2; int86 (0X33,&in,&out); } void detect () { while (!kbhit () ) { in.x.ax = 3; int86 (0X33,&in,&out); if (out.x.bx == 1) printf ("Left"); if (out.x.bx == 2) printf ("Right"); if (out.x.bx == 3) printf ("Middle"); delay (100); // Otherwise due to quick computer response 100s of words will get print } } int main () { detectmouse (); showmousetext (); detect (); hidemouse (); getch (); return 0; }
- Mouse Coordinates
We can obtain the coordinates of the mouse using same service 3 but using different elements of the union .
This function has a prime use in games programming, application designing and GUI development. Different decisions are taken on same left button click, its the position of click that matters.
BX element of output registers stores the X Coordinate of the position of mouse at time of calling function.
CX element of output registers stores the Y Coordinate of the position of mouse at time of calling function.
Now we demonstrate the use of this function by modifying detect function above to display x,y and y coordinates on screen when left click is pressed.
Code will be as followed -Code: Select all
#include <dos.h> #include <graphics.h> union REGS in, out; void detectmouse () { in.x.ax = 0; int86 (0X33,&in,&out); if (out.x.ax == 0) printf ("\nMouse Fail To Initialize"); else printf ("\nMouse Succesfully Initialize"); } void showmousetext () { in.x.ax = 1; int86 (0X33,&in,&out); } void showmousegraphics () { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); in.x.ax = 1; int86 (0X33,&in,&out); getch (); closegraph (); } void hidemouse () { in.x.ax = 2; int86 (0X33,&in,&out); } void detect () { while (!kbhit () ) { int x,y; in.x.ax = 3; int86 (0X33,&in,&out); if (out.x.bx == 1) { x = out.x.cx; y = out.x.dx; printf ("\nLeft || X - %d Y - %d", x, y); } if (out.x.bx == 2) printf ("\nRight"); if (out.x.bx == 3) printf ("\nMiddle"); delay (10); // Otherwise due to quick computer response 100s of words will get print } } int main () { detectmouse (); showmousetext (); detect (); hidemouse (); getch (); return 0; }
- Restricting Mouse
We now restrict the mouse in particular rectangle .
We create a function called restrict which takes four parameters, two Cartesian points each containing one x coordinate and one y coordinate.
First point mentions the top of the rectangle while second point mention the bottom bottom point of rectangle.
This service has limited uses but can be quite handy in special circumstances, for eg - if you want to restrict your mouse in one particular size window in GUI or In Games Programming.
Now I give you final code of the tutorial -
Code: Select all
#include <dos.h> #include <graphics.h> union REGS in, out; void restrict (int x1,int y1,int x2, int y2) { in.x.ax = 7; in.x.cx = x1; in.x.dx = x2; int86 (0X33,&in,&out); in.x.ax = 8; in.x.cx = y1; in.x.dx = y2; int86 (0X33,&in,&out); } void detectmouse () { in.x.ax = 0; int86 (0X33,&in,&out); if (out.x.ax == 0) printf ("\nMouse Fail To Initialize"); else printf ("\nMouse Succesfully Initialize"); } void showmousetext () { in.x.ax = 1; int86 (0X33,&in,&out); } void showmousegraphics () { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); in.x.ax = 1; int86 (0X33,&in,&out); getch (); closegraph (); } void hidemouse () { in.x.ax = 2; int86 (0X33,&in,&out); } void detect () { while (!kbhit () ) { int x,y; in.x.ax = 3; int86 (0X33,&in,&out); if (out.x.bx == 1) { x = out.x.cx; y = out.x.dx; printf ("\nLeft || X - %d Y - %d", x, y); } if (out.x.bx == 2) printf ("\nRight"); if (out.x.bx == 3) printf ("\nMiddle"); delay (10); // Otherwise due to quick computer response 100s of words will get print } } int main () { detectmouse (); showmousetext (); restrict (100,100,500,500); // Change values here to create different mouse movement space. detect (); hidemouse (); getch (); return 0; }