Wednesday 30 November 2011

Implementing Mouse in Turbo c

Hi friends,
Here I am describing the implementation of mouse in Turbo c compiler.
Turbo c is an old c compiler works based on  Dos operating System.It works
in the text mode.But have little graphical functions.
   Now Turbo C is not using widely,because of it's poor graphical performance and
dos based working.But for students who learn c programming Turbo c is the better choice.
Here is a sample program which implement mouse and handle it well.

int86() is the core function using to implement mouse.It is defined in header file 'dos.h'.

#include<conio.h>
#include<mouse.h>
#include<stdio.h>
union REGS in,out;
  void restrictmouse()
  {
in.x.ax=7;
in.x.cx=0;
in.x.dx=639;
int86(0x33,&in,&out);
in.x.ax=8;
in.x.cx=0;
in.x.dx=479;
int86(0x33,&in,&out);
  }


     void showmouse()
    {
in.x.ax=1;
int86(0x33,&in,&out);
    }

void main()
{
restrictmouse();
do
{
showmouse();
in.x.ax=3;
int86(0x33,&in,&out);
if(out.x.bx==1)
printf("Left button clicked");
if(out.x.bx==2)
printf("Right button clicked");
if(out.x.bx==3)
printf("Middle button clicked");
}while(!kbhit());
}

REGS is an union predefined in Turbo c library.We declare two variables
of REGS type.It defines so many member variables.
x.ax defines which opration to be done by mouse.
For instance if in.x.ax=1 and call int86(0x33,&in,&out) the meanig is
show mouse pointer.
if in.x.ax=0 Detect mouse
in.x.ax=2 Show mouse video
in.x.ax=3 Get x and y cordinates of mouse pointer and button clicked in out.
That is x=out.x.cx
        y=out.x.dx
Button clicked is : out.x.bx

if in.x.ax=4 it mean hide mouse
7&8 for restrict mouse pointer,Restric mouse is means that define the
area in which the mouse pointer will be displayed.
7 is for restrict in x direction and 8 for y direction.
in.x.ax=7;
in.x.cx=x1;
in.x.dx=x2;

in.x.ax=8;
in.x.cx=y1;
in.x.dx=y2;

in.x.ax=7;
in.x.cx=0;
in.x.dx=639;
will restrict the mouse to all 640 pixels in x direction.

in.x.ax=8
in.x.cx=0;
in.x.dx=479;
will restrict the mouse to all 480 pixels in y direction.

Hope that this post will be helpful to you.Continue reading, the coming
post will be more helpful.  Thanks by Sukesh B R

3 comments:

  1. I m using this mouse programming for a long time. I want to know what it do in background and how to get input from keyboard in the loop.
    Please mail me at gourav1607@gmail.com
    thanks in advance.

    ReplyDelete
    Replies
    1. To get keyboard output use getch() like this:-
      include c=getch(); just belove the }while(!kbhit()); statement.the kbhit() function is
      searching for keypress.when a key is pressed the loop will terminate.The pressed key
      can be retrieve using c=getch(). getch() returns the ascii value of the key pressed.So
      c get the ascii value like 13 for enter 27 for escape etc.
      For read more input from keyboard make the mouse handling loop as a thread.It will
      work for you.

      Delete
    2. As far as i know, threading doesn't work in c++ :)

      Delete

Search This Blog