Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

QBasic & the mouse

Status
Not open for further replies.

The

Programmer
Jul 30, 2001
92
CA
I'm not that experienced in qbasic, and I was wondering if there is a very simple way to get the mouse coordinates and/or info on which button is being pressed in qbasic. I read a little bit on interrupts, but I don't quite know how to use those. Any help would be appreciated.
 
DECLARE SUB Mousestat (lb%,rb%,xmouse%,ymouse%)
DECLARE SUB Mousedriver (ax%,bx%,cx%,dx%)
DECLARE SUB Mouseshow ()
DECLARE FUNCTION Mouseinit% ()
DIM SHARED mouse$
Mouse$ = SPACE$(57)
FOR i% = 1 to 57
READ a$
h$ = CHR$(VAL("&h"+a$))
MID$(Mouse$,i%,1)=h$
NEXT i%
DATA 55,89,e5,8b,5e,0c,8b,07,50,8b,5e,0a,8b,07,50,8b
DATA 5e,08,8b,0f,8b,5e,06,8b,17,5b,58,1e,07,cd,33,53
DATA 8b,5e,0c,89,07,58,8b,5e,0a,89,07,8b,5e,08,89,0f
DATA 8b,5e,06,89,17,5d,ca,08,00
CLS
ms% = Mouseinit%
IF NOT ms% THEN PRINT "Mouse Not Found": END
mouseshow
PRINT "Right Click to Exit"
DO
Mousestat lb%,mb%,rb%,x%,y%
PRINT lb%,mb%,rb%,x%,y%
LOOP UNTIL rb% = -1

SUB Mousedriver (ax%,bx%,cx%,dx%,Mouse%)
DEF SEG = VARSEG(Mouse$)
Mouse% = SADD(Mouse$)
CALL Absolute (ax%,bx%,cx%,dx%,Mouse%)
END SUB

FUNCTION Mouseinit%
ax% = 0
Mousedriver ax%,0,0,0
Mouseinit% = ax%
END FUNCTION

SUB Mouseshow
ax% = 1
Mousedriver ax%,0,0,0
END SUB

SUB Mousestat (lb%,mb&,rb%,xmouse%,ymouse%)
ax% = 3
Mousedriver ax%,bx%,cx%,dx%
lb% = ((bx% AND 1)<>0)
rb% = ((bx% AND 1)<>0)
mb% = -((bx% AND 4) \ 4)
ymouse% = dx%
xmouse% = cx%
END SUB
 
i wrote a mouse library that does everything you want, i think. assuming you know how to use a library you can get it here .
below are teh commands and what they do

mouseinit-turns mouse on
showmouse-shows mouse
hidemouse-hides mouse
mousey-gives mouse y coordinate
hiresmousex-gives mouse x coordinate in screens 2, 8, 9, 10, 11, and 12
lowresmousex-gives mouse x coordinates in screens 1, 7, and 13
positionmouse (x,y)-positions mouse at coordinates (x,y)
mousexrange (min,max)-sets the mouse x range to the min and the max
mouseyrange (min,max)-sets the mouse y range to the min and the max
mousebutton-returns 0 if no button is pressed, return 1 if the left mouses button is pressed, and returns 2 if the right mouse button is pressed

thats all their is, be sure to put
'$include: 'ssmouse.bi'
at the beggining of your program
 
No... QBasic has NO &quot;Built-In&quot; mouse functions...
You have to use &quot;In-Line Machine Code&quot; or assembly to directly access the Hardware to imort data into Qbasic about the mouse...

It is Complicated... yet fairly simple to use...

A library is a &quot;Add-In&quot; type of extended procedures and functions... Or precompiled (Into Machine language Binary) code that the computer can understand and execute.

Basically someone writes the code and compiles it...
(You need to find out if you have QBasic or QuickBasic 4.5 (QB 4.5))
...then load it into QB using QB /Lfilename.qlb
where filename.qlb is the quick library (*.QLB) that qbasic reads...
to Compile A QB program into an executable (EXE) you need QuickBasic 4.5, a linkable library (*.LIB) file for the qlb and a Basic Include File (*.BI) which defines the Subs/Functions in a LIB/QLB file.

QB45 Compiles your program into a binary object file (*.OBJ)
the linker (LINK.EXE) links the Object file (*.OBJ) with the library (*.LIB) to create a stand alone EXE file

A standard library has 3 files...
file.LIB - The main Library that links to the object file to make a stand alone EXE
file.QLB - The QuickLibrary that is simular to a DLL for runtime use in QB.
file.BI - Th Basic include file that tells your program what is in the Library and how to use it.

I suggest you go to my site and download my MMSINPUT
Master Mind Soft Input

It includes MultiKey input, Mouse input, and a Game Pad input examples...

It should not require any libraries for QBasic and only the standard library for QB45... (start with QB/L)


go to the QBasic Page and it should be at the bottom of the file list...

I will post it on the forum in a second...
 
Code:
'****************************
'*  MASTER MIND SOFT INPUT  *
'*   DIRECT INPUT FOR QB    *
'* CODED BY: JOSH STRIBLING *
'* FOR: MASTERMIND SOFTWARE *
'*       MARCH 11,2001      *
'*      QBASIC  VERSION     *
'* FOR QUICKBASIC USE QB /L *
'****************************

DECLARE SUB MSSHOW ()
DECLARE SUB MSSTART (SN%)
DECLARE SUB MSDRIVER (AX%, BX%, CX%, DX%)
DECLARE SUB MSSTATUS ()
DECLARE FUNCTION MSINIT% ()
DECLARE SUB SETKEYS ()
DECLARE SUB GETGPI (G AS ANY)
DECLARE SUB CLEARBUF ()
DECLARE SUB GETKEYS ()
DECLARE SUB INSTRUCT ()
DefInt A-Z

'UNIVERSIAL TRUE & FALSE VALUES (GOOD HABIT)
CONST TRUE = -1: FALSE = 0

'***** KEYBORAD - ITEMS (REQUIRED) *****
Dim KB(128), lastk
Dim KBUP, KBDOWN, KBLEFT, KBRIGHT, KBESC, KBLSHIFT, KBRSHIFT, KBCTRL, KBSPACE, KBALT

'***** MOUSE - ITEMS (REQUIRED) *****
Type MSTYPE
        X As Integer
        Y As Integer
        B1 As Integer
        B2 As Integer
        B3 As Integer
End Type
Dim MOUSE$, MS As MSTYPE

'***** GAME PAD - ITEMS (REQUIRED) *****
Type GAMEPAD
        LEFT As Integer
        RIGHT As Integer
        UP As Integer
        DOWN As Integer
        B1 As Integer
        B2 As Integer
        B3 As Integer
        B4 As Integer
        B5 As Integer
        B6 As Integer
        X As Integer
        Y As Integer
End Type
Dim GP As GAMEPAD


MSSTART 9 'THIS ALSO SETS THE SCREEN MODE (&quot;MSSTART [SCREENMODE]&quot;
'CLS
SETKEYS
MSSHOW
Do
  MSSTATUS  'GET MOUSE INPUT
'  GETGPI GP 'GET GAME PAD INPUT
  GETKEYS   'GET KEYBOARD INPUT

  LOCATE 1, 1: Print &quot;MOUSE: MS.X=&quot;; MS.X; &quot; MS.Y=&quot;; MS.Y; &quot; MS.B1=&quot;; MS.B1; &quot; MS.B2=&quot;; MS.B2; &quot; MS.B3=&quot;; MS.B3; &quot;        &quot;
 
  LOCATE 3, 1: Print &quot;KEYBOARD: UP KB(72)=&quot;; KB(72); &quot; DOWN KB(80)=&quot;; KB(80); &quot; LEFT KB(75)=&quot;; KB(75); &quot; RIGHT KB(77)=&quot;; KB(77)
  LOCATE 4, 1: Print &quot;          ALT KB(56)=&quot;; KB(56); &quot; CTRL KB(29)=&quot;; KB(29); &quot; SPACE KB(57)=&quot;; KB(57); &quot;        &quot;
 
'  LOCATE 6, 1: PRINT &quot;GAMEPAD: GP.UP=&quot;; GP.UP; &quot; GP.DOWN=&quot;; GP.DOWN; &quot; GP.LEFT=&quot;; GP.LEFT; &quot; GP.RIGHT=&quot;; GP.RIGHT
'  LOCATE 7, 1: PRINT &quot;         GP.B1=&quot;; GP.B1; &quot; GP.B2=&quot;; GP.B2; &quot; GP.B3=&quot;; GP.B3; &quot; GP.B4=&quot;; GP.B4; &quot; GP.B5=&quot;; GP.B5; &quot; GP.B6=&quot;; GP.B6; &quot;        &quot;
Loop Until KB(KBESC) 'END ON ESC


End

Sub CLEARBUF()
  DEF SEG = &H40
  POKE &H1A, PEEK(&H1A + 2)
'  FOR N = 0 TO 10: S$ = INKEY$: NEXT
End Sub

Sub GETGPI(G As GAMEPAD)
  G.X = 0: G.Y = 0
  G.LEFT = 0: G.RIGHT = 0
  G.UP = 0: G.DOWN = 0
  G.B1 = 0: G.B2 = 0: G.B3 = 0
  G.B4 = 0: G.B5 = 0: G.B6 = 0
  X = STICK(0) - 61
  Y = STICK(1) - 61
  B1 = STRIG(1)
  B2 = STRIG(5)
  B3 = STRIG(3)
  B4 = STRIG(7)
  B5 = STICK(3) - 61
  B6 = STICK(2) - 61
  If X < -20 Then G.X = -1: G.LEFT = -1
  If X > 20 Then G.X = 1: G.RIGHT = -1
  If Y < -20 Then G.Y = -1: G.UP = -1
  If Y > 20 Then G.Y = 1: G.DOWN = -1
  If B1 <> 0 Then G.B1 = -1
  If B2 <> 0 Then G.B2 = -1
  If B3 <> 0 Then G.B3 = -1
  If B4 <> 0 Then G.B4 = -1
  If B5 < -20 Then G.B5 = -1
  If B6 < -20 Then G.B6 = -1
End Sub

Sub GETKEYS()
  K = INP(96)
  If K Then                  'KEY CHANGED
         If K < 128 Then          'KEY PRESSED
                KB(K) = True           'SET THE KEY
                lastk = K              'SET THE LAST KEY PRESSED MARKER
         Else                     '....ELSE...LAST KEY RELEASED
                If K = 170 Then        'NOTE: (SC) 170 IS RELEASE LAST KEY PRESSED
        KB(lastk) = False    'CLEAR LAST KEY PRESSED
                Else                   '...ELSE...OTHER KEY RELEASED
        KB(K - 128) = False  'CLEAR RELEASED KEY
                End If
         End If
  End If
  CLEARBUF 'CLEAR REGULAR KEY BUFFER KEY BUFFER (NO BEEPS)
End Sub

Sub LEARNKEY()
'THIS SUB IS FOR USE IN THE IMMEDIATE WINDOW
'*** NOTE YOU MUST RUN THE PROGRAM FIRST
KB(1) = 0
Do
GETKEYS
LOCATE 1, 1: Print &quot;PRESS A KEY FOR SCAN CODE BELOW&quot;
LOCATE 2, 1: Print lastk
Loop Until KB(1)
End Sub

Sub MSDRIVER(AX, BX, CX, DX)
  DEF SEG = VARSEG(MOUSE$)
  MOUSE% = SADD(MOUSE$)
  Call ABSOLUTE(AX, BX, CX, DX, MOUSE%)
End Sub

Sub MSHIDE()
  AX = 2
  MSDRIVER AX, 0, 0, 0
End Sub

Function MSINIT()
  AX = 0
  MSDRIVER AX, 0, 0, 0
  MSINIT = AX
End Function

Sub MSPUT(X, Y)
  AX = 4
  CX = X
  DX = Y
  MSDRIVER AX, 0, CX, DX
End Sub

Sub MSRANGE(X1, Y1, X2, Y2)
  AX = 7
  CX = X1
  DX = X2
  MSDRIVER AX, 0, CX, DX
  AX = 8
  CX = Y1
  DX = Y2
  MSDRIVER AX, 0, CX, DX
End Sub

Sub MSSHOW()
  AX = 1
  MSDRIVER AX, 0, 0, 0
End Sub

Sub MSSTART(SN)
  MOUSE$ = Chr$(Val(&quot;&H55&quot;)) + Chr$(Val(&quot;&H89&quot;)) + Chr$(Val(&quot;&HE5&quot;)) + Chr$(Val(&quot;&H8B&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H5E&quot;)) + Chr$(Val(&quot;&H0C&quot;)) + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H07&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H50&quot;)) + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H5E&quot;)) + Chr$(Val(&quot;&H0A&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H07&quot;)) + Chr$(Val(&quot;&H50&quot;)) + Chr$(Val(&quot;&H8B&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H5E&quot;)) + Chr$(Val(&quot;&H08&quot;)) + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H0F&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H5E&quot;)) + Chr$(Val(&quot;&H06&quot;)) + Chr$(Val(&quot;&H8B&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H17&quot;)) + Chr$(Val(&quot;&H5B&quot;)) + Chr$(Val(&quot;&H58&quot;)) + Chr$(Val(&quot;&H1E&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H07&quot;)) + Chr$(Val(&quot;&HCD&quot;)) + Chr$(Val(&quot;&H33&quot;)) + Chr$(Val(&quot;&H53&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H5E&quot;)) + Chr$(Val(&quot;&H0C&quot;)) + Chr$(Val(&quot;&H89&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H07&quot;)) + Chr$(Val(&quot;&H58&quot;)) + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H5E&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H0A&quot;)) + Chr$(Val(&quot;&H89&quot;)) + Chr$(Val(&quot;&H07&quot;)) + Chr$(Val(&quot;&H8B&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H5E&quot;)) + Chr$(Val(&quot;&H08&quot;)) + Chr$(Val(&quot;&H89&quot;)) + Chr$(Val(&quot;&H0F&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H8B&quot;)) + Chr$(Val(&quot;&H5E&quot;)) + Chr$(Val(&quot;&H06&quot;)) + Chr$(Val(&quot;&H89&quot;))
  MOUSE$ = MOUSE$ + Chr$(Val(&quot;&H17&quot;)) + Chr$(Val(&quot;&H5D&quot;)) + Chr$(Val(&quot;&HCA&quot;)) + Chr$(Val(&quot;&H08&quot;)) + Chr$(Val(&quot;&H00&quot;))
  Screen SN
  M = MSINIT
  If M = 0 Then Print &quot;NO MOUSE&quot;
End Sub

Sub MSSTATUS()
  AX = 3
  MSDRIVER AX, BX, CX, DX
  MS.B1 = ((BX And 1) <> 0)
  MS.B2 = ((BX And 2) <> 0)
  MS.B3 = ((BX And 4) <> 0)
  MS.X = CX \ 2
  MS.Y = DX
End Sub

Sub SETKEYS()
  KBUP = 72
  KBDOWN = 80
  KBLEFT = 75
  KBRIGHT = 77
  KBESC = 1
  KBLSHIFT = 42
  KBRSHIFT = 54
  KBCTRL = 29
  KBSPACE = 57
  KBALT = 56
End Sub


Cheers
-Josh Stribling
cheers.gif
 
This is the Code that makes the mouse work:
55 89 e5 8b 5e 0c 8b 07 50 8b 5e 0a 8b 07 50 8b
5e 08 8b 0f 8b 5e 06 8b 17 5b 58 1e 07 cd 33 53
8b 5e 0c 89 07 58 8b 5e 0a 89 07 8b 5e 08 89 0f
8b 5e 06 89 17 5d ca 08 00


which is Binary Code That a string ((Mouse$), and passed to Call Absolute in QB by an address of type integer (MOUSE%)

Sub MSDRIVER(AX, BX, CX, DX)
DEF SEG = VARSEG(MOUSE$)
MOUSE% = SADD((MOUSE$)
Call ABSOLUTE(AX, BX, CX, DX, MOUSE%)
End Sub


AX, BX, CX, and DX are Registers that are used like variables in assembly and machine language and actually exist IN your CPU processor (not memory).

the binary code at the top of the post assigns/returns values to/through these registers via Call Absolute.

If you want to know what the actual Code looks like... you can use an &quot;ancient&quot; program found on most computers since the 80's called Debug... and uncompile the Binary Code into a somewhat readable assembly code.
(This code was actually written in and assembled in DEBUG)
This is what a dump of the unassembled code looks like in DEBUG...

Code:
Address   Hex Code      Assembly Code
Code:
141D:0100 55            PUSH	BP
141D:0101 89E5          MOV	BP,SP
141D:0103 8B5E0C        MOV	BX,[BP+0C]
141D:0106 8B07          MOV	AX,[BX]
141D:0108 50            PUSH	AX
141D:0109 8B5E0A        MOV	BX,[BP+0A]
141D:010C 8B07          MOV	AX,[BX]
141D:010E 50            PUSH	AX
141D:010F 8B5E08        MOV	BX,[BP+08]
141D:0112 8B0F          MOV	CX,[BX]
141D:0114 8B5E06        MOV	BX,[BP+06]
141D:0117 8B17          MOV	DX,[BX]
141D:0119 5B            POP	BX
141D:011A 58            POP	AX
141D:011B 1E            PUSH	DS
141D:011C 07            POP	ES
141D:011D CD33          INT	33
141D:011F 53            PUSH	BX
141D:0120 8B5E0C        MOV	BX,[BP+0C]
141D:0123 8907          MOV	[BX],AX
141D:0125 58            POP	AX
141D:0126 8B5E0A        MOV	BX,[BP+0A]
141D:0129 8907          MOV	[BX],AX
141D:012B 8B5E08        MOV	BX,[BP+08]
141D:012E 890F          MOV	[BX],CX
141D:0130 8B5E06        MOV	BX,[BP+06]
141D:0133 8917          MOV	[BX],DX
141D:0135 5D            POP	BP
141D:0136 CA0800        RETF	0008

Notice Above, the Memory Address to the left shows where the Bytes are stored, The Next number is the HexCode at that address, the next string is the Assembly code that the Hex code represents... Notice for Exampe... that every 58 is a POP AX and 8B07 is MOV AX,[BX]... These are also know as OP-Codes or Operation Codes for the Operating system (Dos/Window OS)

Below is the ASSEMBLY CODE ripped from the dump that actually controls the mouse data...
Code:
PUSH	BP
MOV	BP,SP
MOV	BX,[BP+0C]
MOV	AX,[BX]
PUSH	AX
MOV	BX,[BP+0A]
MOV	AX,[BX]
PUSH	AX
MOV	BX,[BP+08]
MOV	CX,[BX]
MOV	BX,[BP+06]
MOV	DX,[BX]
POP	BX
POP	AX
PUSH	DS
POP	ES
INT	33
PUSH	BX
MOV	BX,[BP+0C]
MOV	[BX],AX
POP	AX
MOV	BX,[BP+0A]
MOV	[BX],AX
MOV	BX,[BP+08]
MOV	[BX],CX
MOV	BX,[BP+06]
MOV	[BX],DX
POP	BP
RETF	0008

Notice All the AX, BX, CX, and DX registers used...
the Stack is like a virtual stack of values push'ed from and pop'ed to registers where... the first thing you POP is the last thing you PUSHed... in other words... if AX is 1 and BX is 2 if you PUSH AX (1) then PUSH BX (2) then POP AX and POP BX... AX will now be 2 and BX will equal 1... this is how you store and swap values in Assembly...
MOV is Like Equal...It Copies A Value to A register
MOV AX,BX is like AX = BX... if AX = 1 & BX = 2...
MOV AX,BX ... now AX = 2 and BX = 2
you can also say MOV AX, 3 then AX will = 3

well it's 1am and I'm getting tired...
Hope you learn something from this;-)...
Cheers-
cheers.gif

-Josh Stribling
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top