Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Tutorial - Mouse
Author:Arkon
Category:DOS
Uploaded at:29-May-03
Views:9314
There are two ways of handling the mouse: Using an Interrupt (0x33 - Hex) or Reading Directly from the Serial Port. Only the first way is discussed here because it works with all mouses (serial, ps2, usb).
The mouse controller generates an interrupt (0x33) every time a mouse event (movement, buttons' state is changed, etc.) occurs.

We get the position of the mouse and the buttons' state from the interrupt.
Note: You always set _AX to the function number and then call the interrupt.
First we should initialize the mouse:

int mouse_buttons_number = 0;
int init_mouse()
{
 asm {
  mov ax, 0
  int 0x33
 }
 if (_AX != 0) 
 {
  return(1);
  mouse_buttons_number = _BX;
 }
 return(0);
}

Note: Always initialize the mouse ATER initializing the Video Card.
Here we set _AX to function 0 (Initialize Mouse), then we generate the interrupt.
After generating the interrupt, _AX tells if the mouse driver is installed and _BX tells the number of buttons.
Note: if _AX doesn't equal 0 it means the mouse driver isn't installed.

Now we want to get the position of the mouse (X,Y):

int get_mouse_X()
{
 asm {
  mov ax, 3
  int 0x33
 }
 return (_CX);
} 

int get_mouse_Y()
{
 asm {
  mov ax, 3
  int 0x33
 }
 return (_DX); //Here is the difference, here we return DX.
} 

Note: In Mode 13h devide the X by 2.
Here we set _AX to function 3 (get X,Y), generate the interrupt, read the mouse's X coordinate from _CX and read the mouse's Y coordinate from _DX.

Now we want to check if one of the mouse's buttons is pressed:
int left_pressed()
{
 asm {
  int ax, 3
  int 0x33
 }
 return (_BX & 1);
}
int right_pressed()
{
 asm {
  int ax, 3
  int 0x33
 }
 return (_BX & 2);
}
Here we set _AX to function 3 (get buttons' state), generate the interrupt, read the left button state from bx (bit 1) and read the right button state from bx(bit 2)
If you don't know how to handle bits see The Structure of Bits

Here are 5 helpful functions:
void show_mouse_cursor() //Default cursor..
{
 asm {
  mov ax, 1
  int 0x33
 } 
}
void show_mouse_cursor()
{
 asm {
  mov ax, 2
  int 0x33
 } 
}
//Set mouse position.
void set_mouse_position(int x, int y)
{
 asm {
  mov ax, 4 //Function # 4, changes the mouse position.
  mov cx, x
  mov cy, y
  int 0x33
 }
}
//Define a window that the mouse is limited in
//(In Mode 13h multiply rect_right and rect_left by 2).
void set_mouse_window(int rect_left, int rect_top, 
                      int rect_right, int rect_bottom)
{
 asm{
  mov ax, 7 //Function # 7, sets the range of the X axis.
  mov cx, rect_left
  mov dx, rect_right
  int 0x33
  mov ax, 8 //Function # 7, sets the range of Y axis.
  mov cx, rect_top
  mov dx, rect_bottom
 }
}
//Set the mouse sensitivity(the ratio of the
//mouse coordinates per screen pixel )
void set_mouse_sensitivity(int ratiox, int ratey)
{
 asm{
  mov ax, 0x1a //Function # 25, sets sensitivity.
  mov bx, ratex
  mov cx, ratey
  mov dx, 0 
  int 0x33
 }
}
Note: Init mouse (Function # 0) won't set the sensitivity.

NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[117082]
2D Rotated Rectangles Collision Detection[88999]
Keyboard Hook[77347]
UDP[65892]
HTTP Proxy[41228]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11562]
PS2 Mouse Driver[6960]
Wave Format Player[5793]
Reading FAT12[5621]
CodeGuru[5361]


All rights reserved to RageStorm © 2009