Thursday , September 21 2017
Home / C / C Program to get current position of a mouse pointer

C Program to get current position of a mouse pointer

This is a C program to get the Current Mouse pointer location or position as a output in C Graphics. When you move your cursor the Mouse location will change with the pointer itself.

This is a Program in C Graphics which used 3 user defined functions.

  1. hidemouseptr(); -> Used to hide the Mouse Pointer
  2. showmouseptr(); -> To Show the mouse pointer location
  3. getmouseptr(); -> Fetches the current location of mouse pointer

 

  • #include<graphics.h>
    #include<conio.h>
    #include<stdio.h>
    #include<dos.h>
     
    int initmouse();
    void showmouseptr(); //Shows the current mouse pointer position
    void hidemouseptr(); //hides the mouse pointer position
    void getmouseposition(int*,int*,int*);  // fetches the mouse pointer variables and location from graphics in C library
    union REGS i, o;
     
    main()
    {
       int gd = DETECT, gm, status, button, x, y, pos_x, pos_y;
       char array[50];
     
       initgraph(&gd,&gm,"C:\\TC\\BGI");
       settextstyle(DEFAULT_FONT,0,2);
     
       status = initmouse();
     
       if ( status == 0 )
          printf("Mouse support not available.\n");
       else
       {
          showmouseptr();
     
          getmouseposition(&button,&x,&y);
     
          pos_x = x;
          pos_y = y;
     
          while(!kbhit())
          {
             getmouseposition(&button,&x,&y);
     
             if( x == pos_x && y == pos_y )
             {}
             else
             {
                cleardevice();
                sprintf(array,"X = %d, Y = %d",x,y);
                outtext(array);
                pos_x = x;
                pos_y = y;
             }
          }
       }
     
       getch();
       return 0;
    }
     
    int initmouse()
    {
       i.x.ax = 0;
       int86(0X33,&i,&o);
       return ( o.x.ax );
    }
     
    void showmouseptr()
    {
       i.x.ax = 1;
       int86(0X33,&i,&o);
    }
     
    void getmouseposition(int *button, int *x, int *y)
    {
       i.x.ax = 3;
       int86(0X33,&i,&o);
     
       *button = o.x.bx;
       *x = o.x.cx;
       *y = o.x.dx;
    }
  • /* get_cur_pos.c */
    #include <stdio.h>
    #include <unistd.h>
    #include <termios.h>
    int main()
    {
    	char buf[8];
    	int curline;
    	char cmd[]="echo -e '\033[6n'";
    	struct termios save,raw;
    	tcgetattr(0,&save);
    	cfmakeraw(&raw); tcsetattr(0,TCSANOW,&raw);
    	if (isatty(fileno(stdin))) 
    	{
    		write(1,cmd,sizeof(cmd));
    		read (0 ,buf ,sizeof(buf));
    
    		/* It doesn't work!!?
    		sscanf(buf,"%d",curline);
    		printf("\n\rCurrent Line: %d\n\r" , curline);
    		*/
    
    		printf("\n\rCurrent Line: %c%c\n\r" , buf[2] ,buf[3] );
    	}
    	tcsetattr(0,TCSANOW,&save);
    	return 0;
    }
Also the Code can be done in Matlabs , Java and other languages to which will be provided soon. So Enjoy the coding!

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

Leave a Reply

Your email address will not be published. Required fields are marked *