Home
Site Structure

Programming Home
  Basic Home
    GUI & OS Home
    QB Knowledge Base
    Professor Answers
    Codename Surena
    QB Downloads

Write Us

Special Links
QB DirectoryNEW

Codename Surena
Forums
Sepent Basic GUI & OS Home
Register Log In
QB Knowledge Base > How To Questions > Number 9

How to use mouse?
After keyboard, mouse is the most popular input device for PC. This article describes main functions provided by mouse driver. The functions are called through the use of CALL INTERRUPT statement. If you use QBASIC 1.1 refer to ht7: How to imitate CALL INTERRUPT in QBASIC 1.1? for a replacement of this statement in version 1.1.

For calling mouse functions, you should load the function number in register AX and the parameters (if any) in other registers, as described here, and then call interrupt service &H33. Note that the code samples in this article assume that you have declared a variable of type "RegType" named "Regs". Remember that for using "INTERRUPT" routine as well as "RegType" type, you should include "QB.BI" or "QBX.BI" include files:

	'$INCLUDE: 'QB.BI'
		'or
	'$INCLUDE: 'QBX.BI'
		

Initializing and Detecting the Mouse Driver
Use function 0 to determine if the mouse driver is installed and initialize the mouse to its default state. If the driver is installed, this function returns &HFFF (-1) in register AX.

	Regs.AX = 0
	CALL INTERRUPT (&H33, Regs, Regs)
	
	IF AX = -1 THEN
		'The driver is installed.
	ELSE
		'The driver is not installed.
	END IF
		

Showing and Hiding the Mouse Pointer
Function 1 shows the mouse pointer.

	Regs.AX = 1
	CALL INTERRUPT (&H33, Regs, Regs)
		

Function 2 hides the mouse pointer.

	Regs.AX = 2
	CALL INTERRUPT (&H33, Regs, Regs)
		

Retrieving the coordinates of the Mouse Pointer and the State of the Mouse Buttons
Function 3 returns the coordinates of the mouse pointer and the state of the mouse buttons in the following registers:
  BX = The state of the mouse buttons:
    bit 0: The left button.
    bit 1: The right button.
    bit 2: The middle button.

  CX = x-coordinates of the mouse pointer.
  DX = y-coordinates of the mouse pointer.

The coordinates are returned in pixels, even in text mode. The mouse coordinates are not necessarily equivalent to that of the screen.

	CONST mbLeftButton = 1
	CONST mbRightButton = 2
	CONST mbMiddleButton = 4
	
	Regs.AX = 3
	CALL INTERRUPT (&H33, Regs, Regs)
	
	'The mouse pointer is located at (Regs.CX, Regs.DX)
	
	IF BX AND mbLeftButton THEN
		'The left button is pressed.
	END IF
	
	IF BX AND mbRightButton THEN
		'The right button is pressed.
	END IF

	IF BX AND mbMiddleButton THEN
		'The middle button is pressed.
	END IF
		

Setting the Position of the Mouse Pointer
Use function 4 to set the position of the mouse pointer. You should load the x-coordinates in CX and the y-coordinates in DX.

	Regs.AX = 4
	Regs.CX = 100
	Regs.DX = 100
	CALL INTERRUPT (&H33, Regs, Regs)
		

Setting the limits of the Mouse Pointer
You can set the horizontal limits of the mouse pointer, using function 7 and the vertical limits using function 8. For both functions, the minimum limit is loaded in CX and the maximum in DX. If the maximum limit is smaller than the minimum limit, they are automatically swapped. The functions move the mouse pointer to the specified region, if necessary.

	Regs.AX = 7
	Regs.CX = 100
	Regs.DX = 200
	CALL INTERRUPT (&H33, Regs, Regs)

	Regs.AX = 8
	Regs.CX = 100
	Regs.DX = 200
	CALL INTERRUPT (&H33, Regs, Regs)