Issue #3
April 7th, 2001

Ever seen somebody's paint program? Or the map editor of some RPG in which you were able to move the mouse and easily make your maps without having to use the keyboard? Ever been curious as to how the mouse works? Well If you haven't yet found out then I'm here to show you how to use this wonderful piece of hardware!

The mouse can easily be used from within QB using straight QB! We make use of the the INTERRUPT function and the data structure RegType which allows us to use the computer's registers. You don't have to use assembly for this either! Though it is better to do so in some cases. Anyways...

The mouse has it's own interrupt, 33h (a hexidecimal number so to use it in QB just use &H33). You can control the mouse using the AX register. When the AX register is set to 1, the mouse is turned 'on'. When AX is 2, it is turned 'off'. When it's 3 it'll return information regarding the current status of the mouse such as the number of buttons, current button being pressed and the current mouse position.

Here's a nice chart for you:

Value of AX Description of what happens
1 Mouse is turned on. You can see it on the screen.
2 Mouse is turned off. You can no longer see it on the screen.
3 Mouse status is returned into AX, BX, CX and DX.
AX = Number of buttons
BX = Current button being pressed (1 = left, 2 = right)
CX = Current x position on screen * 8
DX = Current y position on screen * 8

I hope that helps you out somewhat. =)

Now how do I implement this you ask? Simple. We can use straight QB code for this. No assembly required. (It's quite easy though in assembler.)

DEFINT A-Z
'$INCLUDE: 'qb.bi'			'Include the QB.QLB include file
CLS
DIM regs AS RegType			'This lets us access the registers

regs.ax = 1				'Set AX to 1 so we can turn on the mouse
INTERRUPT &H33, regs, regs		'Execute interrupt 33h
DO					'Start a loop
 regs.ax = 3				'Set AX to 3 so we can view mouse status
 INTERRUPT &H33, regs, regs		'Execute the interrupt and get the mouse status
 
 'Now we'll print the mouse status on the screen. Note that we divide CX and DX by 8 to
 'the correct column and row position.
 LOCATE 1, 1
 PRINT regs.ax, regs.bx, (regs.cx / 8) + 1, (regs.dx / 8) + 1
LOOP WHILE INKEY$ = ""
regs.ax = 2				'When we're done set AX to 2 soe we can turn off the
INTERRUPT &H33, regs, regs		'mouse driver.
END

That was a quick example that will let you see the mouse in action. It's a continuous loop that constantly prints out the mouse status. Then when the user presses a key the mouse is turned 'off' and the demo ends. Simple. The comments really explain a lot of it as it's not hard stuff.

Now you can use the mouse! What you'll need to do is make routines which check if the mouse is in a certain region of the screen so you can implement push buttons and menus and stuff like that. Maybe next issue I'll write a tutorial on making a mouse driven GUI. Hope this helped!

This article was written by: Fling-master - http://www.qbrpgs.com

All site content is © Copyright 2001, HyperRealistic Games. This excludes content submitted by other people.