Documente Academic
Documente Profesional
Documente Cultură
8 July 2009
Mouse Programming In C
1. Requirements 2. Fundamental Knowledge. 3. AX Register 4. Detecting Mouse 5. Showing & Hiding Mouse 6. Detecting Input 7. Mouse Coordinates 8. Restricting Mouse
1. Requirements
A mouse attached to your system. 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.
Fundamental Knowledge
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 Mouse has port attached to it 0X33 and similarly keyboard has attach to it port 0X60
We also make use of address registers. These are basically union of type REGS defined in dos.h. Just remember we use two register to communicate to a device driver using two registers one for input and one for output.
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 AX, BX, CX and DX are members of Union REGS and more or less integers.
Input AX = 0
not
AX = 1 AX = 2 AX = 3
Nothing Nothing CX = Mouse X Coordinate DX = Mouse Y Coordinate BX = 0 No Key Is Pressed BX = 1 Left Button is Pressed BX = 2 Right Button is Pressed BX = 3 Centre Button is Pressed Nothing
Ax = 3
Nothing
4. Detect Mouse
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 -
Detect Mouse
#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; }
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
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 CX element of output registers stores the Y Coordinate of the position of mouse
Mouse Coordinates
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 }
What Next ?
Now first thing you must do is congratulate yourself because you just added a super weapon to you arsenal. To get you started here what you can do with mouse programming Games Programming Commercial Applications Development. Graphical User Interface 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...