C Programming Reference

C Programming Reference >> C Advance Programming Tutorials >> 

Mouse Programming In Cequisite Knowledege 

02/01/07 - 22310 Views - Ratings :   4.83 of 5 / 862 Votes

Level - Beginner

In this tutorial you will learn how to do mouse programming through C. Mouse Programming in C is simple and beginners can give it a shot if they undestand carry on otherwise read the basic tutorials first and then comeback and learn how to do Mouse Programming using C. Before we get our hands dirty we begin with a breif intro.

Mouse has revolutionized the Computer World . It was introduced with GUI which stands for Graphical User Interface.

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 thier 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 begginer to professional needs to have in his toolbox to have a cutting edge.

Mouse Programming will be used almost everywhere. It will embeded in games programming to commerical 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.

Contents -

1. Requirements
2. Fundamental Knowledge.
3. AX Register
4. Detecting Mouse
5. Showing & Hiding Mouse
6. Detecting Input
7. Mouse Coordinates
8. Restricting Mouse
9. What Next?

1. Requirements

First and Formeost requirement which i believe will surely be satisfyied, 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 uncompatibility and even if you face any problem we provide an 24 X 7 Email Support to help you out.

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.

Back To Top

2. Fundamental Knowledge

Before we start harcore 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 doesnot 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 dont 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 recieve information in it embedded in output register.

 

Back To Top

3. 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.

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                 Set Horizontal Limit            Nothing
CX = MaxX1 
DX =MaxX2            


Ax = 8                 Set Vertical Limit              Nothing
CX = MaxX1 
DX =MaxX2            


Now we step by step study each of these services in following sections and build up our combined code.

Back To Top

4. Detect Mouse

Before you start your embed mouse program you should always check wether the mouse programming is supported on not.

If some how mouse fails to initalise 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 -

#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;
}
 

Back To Top

5. 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.

Mouse Programming in Text Mode

It was produced from adding a function showmousetext to above code so code becomes -

#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 -


Mouse Programming in Graphics Mode


This is achieved using a function showmousegraphic added to above code while removing showmouse text from main.

#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 realtively simple task of hiding mouse using a function hidemouse as shown below - 
#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;
}
Back To Top

6. Detect Input

We will now work on a important aspect of mouse programming detecting clicks.

We make use of an aditional 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.

#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;
}
Back To Top

7. Mouse Coordinates

We can obtain the coordinates of the mouse using same service 3 but using different elments 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 postion of click that matters.

BX element of output registers stores the X Coordinate of the postion of mouse at time of calling function.

CX element of output registers stores the Y Coordinate of the postion 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 -

#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;
}

Back To Top

8. Restricting Mouse

We now restrict the mouse in particular rectangle .

We create a function called restrict which takes four paramters, 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 -

#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;
}

Back To Top

9. What Next ?

Now first thing you must do is congratulate yourself because you just added a super weapon to you arsenal.

Now there is no stopping you. Let your creativity flow and world class products come out of your computer.

To get you started here what you can do with mouse programming -

1. Games Programming

2. Commercial Applications Development.

3. Graphical User Interface

4. Utitlity Softwares such as Paint, Word Processors , Spread Sheets and so on....

Before you get your hands dirty we recommend you go through our game building and application building Tutorials. Most of them are based mouse programming.

Also if you still want to hone your mouse programming skills further go through our advance tutorials such as mouse cursors, mouse drawing, menu through mouse and more...

Back To Top

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code : Code

 

© 2006 cencyclopedia.com