Home Page Home Page
Using the Mouse in DOS mode



Feedback

The following page is extracted from the Turbo C++ Graphics Tutor Copyright © Digital International Ltd 1997.
Note : for any additional information concerning this page & the source code included, download the :-
Turbo C++ mouse & images demo program files & source code.
Turbo Pascal mouse & images demo program files & source code.

A screen shot from the demo program :-

Click to enlarge

Contents of this page :-

  1. Interfacing with the Mouse.
  2. Initializing the Mouse.
  3. Displaying the Mouse Cursor.
  4. Getting Mouse Information.

Interfacing with the mouse :-

The two primary input devices used with GUIs are the keyboard and the mouse. The mouse is the input device of choice for many users because it offers greater flexibility and movement than manual keyboard input.
The mouse is very easy to access and control. The mouse system consist of the mouse and the mouse driver. The mouse is the device manipulated by the user to provide directional input to the program. The mouse driver is the memory resident program that provides communication between the mouse and the computer. The mouse driver maintains the cursor position of the mouse and the status of the mouse buttons. You can access the cursor position and the button status with in any application through software interrupt 33h. The C++ function int86 can be used to generate the software interrupt 33h and the Turbo Pascal procedure Intr can be used for the same purpose as well. The mouse driver has several functions by specifying the function number in the AX register when calling interrupt 33h. The BX,CX, and DX registers are used to pass parameters to the mouse functions.

FunctionMeaning
0Resets the mouse and retrieves the mouse status.
1Displays the mouse cursor.
2Hides the mouse cursor.
3Retrieves the mouse cursor positioned the status of the mouse buttons.
4Retrieves the number of times a button was pressed since the last call.
5Retrieves the number of times a button was released since the last call.
6Specifies the horizontal limits for the cursor.
7Specifies the vertical limits for the cursor.
8Specifies the cursor to use for graphics modes.
9Specifies the cursor to use for text modes.

Getting Started with the Mouse:

There are two things you need to know before we started to use the mouse. First under DOS, you must load the mouse driver software that came with your mouse before trying to use it.

Initializing the Mouse:

Service 0 - put AX = 0
Returns : AX = 0 means Failure.

C++ Example:

int mouse_init(void)
{
 int result;
 asm {
   mov ax,00h;
   int 33h;
   mov result,ax;
     }
 return(result);
}
Turbo Pascal Example:
Function Mouse_Init : Boolean;
Var
Result : Word;
Begin
 ASM
   MOV AX,$00
   INT $33
   MOV Result,ax
 End;
 Mouse_Init := (Result <> 0);
End;
Using INT 33H service 0 is the necessary first step toward using the mouse. If this service returns a nonzero value, the mouse is initialized. Otherwise, the mouse can not be used (because it is not installed in the computer or the mouse driver is missing). In that case, and if your program depends on the use of a mouse, you should print out an error message and quit.
Once the mouse is initialized, you've all set until the computer is turned off- you don't have to initialize it again (although doing so does no harm). Note that initializing the mouse does not display the mouse cursor. A function that initialize the mouse and returns the result is included in the *.ZIP files specified above "MOUSE.INC" & "MOUSE.PAS".

Displaying the Mouse Cursor:

Service 1 - put AX = 1

C++ Example:

void mouse_show(void)
{
 asm {
	mov ax,01h;
	int 33h;
 }
}
Turbo Pascal Example:
procedure Mouse_Show;
Begin
 ASM
   MOV AX,$01
   INT $33
 End;
End;
Next we have to display the mouse cursor using INT 33H service 1. A small procedure that displays the mouse cursor is included in the *.ZIP files specified above "MOUSE.INC" & "MOUSE.PAS". At this point, the mouse system is active and the cursor has appeared on the screen. We can also make it vanish using INT 33H service 2 to hides the mouse cursor. If the cursor is already off, it stays off. There are times when the mouse cursor be a distraction on the screen, and this service can fix that.
The very important reason for hiding the mouse cursor, as the mouse cursor moves over the screen, the mouse driver software reads the character at the preset position before it displays the mouse cursor. Then, when the mouse cursor moves on, that character is restored, attributes and all. However, this means that if you have changed the screen display behind the mouse cursor (for example, opened a window there), it will still restore the original-and wrong- character, leaving a one-character hole. To avoid this problem, you should always turn the mouse cursor off when displaying a window or over-writing the mouse cursor in any way, and turn it on again immediately afterwards.

C++ Example:
void mouse_hide(void)
{
 asm {
	mov ax,02h;
	int 33h;
 }
}
Turbo Pascal Example:
Procedure Mouse_Hide;
Begin
 ASM
   MOV AX,$02
   INT $33
 End;
End;
We can also read right and left button information from the mouse INT 33H service 3. Let's look at that process next.

Getting Mouse Information:

Service 3-set these things : AX = 3
This will return:

BXMeans
0No button down
1Left button down
2Right button down
3Both button down

CX = Current mouse cursor column
DX = Current mouse cursor row

C++ Example:
unsigned int co,ro,bo;
/* This fuction is to return the
      mouse information
      bo = mouse button
      ro = the y position of the mouse cursor
      co = the x position of the mouse cursor */
void mouse_info(void)
{
asm {
  mov ax,03h;
  int 33h;
  mov bo,bx;
  mov ro,dx;
  mov co,cx;
 }
}
Turbo Pascal Example:
(* This Procedure is to return the
      mouse information
      Bu = mouse button
      Ro = the y position of the mouse cursor
      Co = the x position of the mouse cursor *)
Procedure Mouse_info;
Var
B, C, R : Word;
Begin
 ASM
   MOV AX,$03
   INT $33
   MOV B,BX
   MOV C,CX
   MOV R,DX
 End;
 Co := C; Ro := R; Bu := B;
End;
This serve, service 3, returns information in BX, DX, and CX. BX indicates which button(s) is (are) down. This service also returns the current row and column of the mouse cursor in DX and CX, respectively. These numbers are measured in pixels, in 640 x 200 mode, DX can range from 0 to 199 and CX from 0 to 639.
If you want to convert from pixel to alphanumeric columns and rows, just integer divide the pixel range by 8 and add 1 to the result.
The most severe limitation here is that service 3 only provides an instant snapshot of what's going on with the mouse. If you want to use it for mouse input, you have to keep "Polling" it , that is, looping over it until something happens.
All the above mouse routines are included in the *.ZIP files specified above "MOUSE.INC" & "MOUSE.PAS".