3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Using a mouse in QBasic programs

Using a mouse in QBasic programs
As anyone who has programmed with QBasic knows, it is a fun, easy and ( for it's time ) quite powerful programming tool!   Admittedly, it is rather slow, but given that it has never cost a dime, I suppose that's an acceptable concession.  As I mentioned, QBasic includes a wide range of simple, powerful commands, although it's one glaring shortcoming is it's complete lack of mouse support! 

For the longest time, I had wanted to find a way to use the mouse in my own QBasic programs.  While I eventually found several fine examples of code on-line that did in fact interface with the mouse, they all seemed very complicated to me, with multiple sub-routines, functions and procedure calls.  So, I decided to "boil it all down" to the simplest, bare-minimum method, and to my knowledge, what I came up with is still the quickest, easiest way to incorporate mouse functions into a QBasic program! 

One might think of my code as a kind of "black box" or "plug-in" of sorts;  you simply copy my "Mouse" sub-routine into your program, and it immediately provides rudimentary mouse support!  Interfacing with the mouse is then accomplished by way of three simple functions; "Mouse 1" shows the mouse cursor, "Mouse 2" hides it again, and "Mouse 3" reads the mouse's current button status as well as it's location.  This information is communicated to the user's program through 3 global variables; "B" represents the mouse buttons, "H" contains it's horizontal co-ordinates, and "V" returns it's vertical co-ordinates.  For a 2-button mouse, the variable "B" will return the following values:

                          VALUE:                      BUTTON(S) PRESSED:
                                0                                      None
                                1                                       Left
                                2                                       Right
                                3                                       Both


The following is the actual mouse code segment which provides mouse support:

''''''''''''''''''''''''''''''''''''''''''''''''''' Mouse Sub-program  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'       This sub-program provides mouse support to QBasic programs.
'       It is called with one parameter, and performs as follows:
'              Mouse 1       ( Shows mouse cursor )
'              Mouse 2       ( Hides mouse cursor )
'              Mouse 3       ( Reads button status & co-ordinates )
'
'       Notes:
'
'       This sub-program requires Microsoft's mouse driver ( Mouse.com )
'       or an equivalent Dos-based mouse driver, which must be loaded and
'       running before use.
'
'       Variables B, H & V are global, so be certain not to create any
'       other variables of the same name, or you must re-name these.
'
'       Be sure to hide the mouse cursor before performing any graphics
'       function, or else any graphics under the cursor will be garbled.
'                                                                                                                                                                                                                                              
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SUB Mouse (Funk)                                                        'Define sub & parameter passed.
SHARED B, H, V                                                             'Share variables with main sub.
STATIC Crsr                                                                    'Track whether Cursor is shown.
IF Funk = 1 THEN Crsr = 1                                           'Show Cursor.
IF Funk = 2 AND Crsr = 0 THEN EXIT SUB              'Don't hide Cursor more than once.
IF Funk = 2 AND Crsr = 1 THEN : Crsr = 0              'Hide Cursor.
POKE 100, 184: POKE 101, Funk: POKE 102, 0    'Poke machine code necessary for
POKE 103, 205: POKE 104, 51: POKE 105, 137    'using the mouse into memory
POKE 106, 30: POKE 107, 170: POKE 108, 10       'starting at offset 100 in the
POKE 109, 137: POKE 110, 14: POKE 111, 187     'current segment.  This code is
POKE 112, 11: POKE 113, 137: POKE 114, 22       'then executed as a unit, via the
POKE 115, 204: POKE 116, 12: POKE 117, 203     'statement " Call Absolute ".
CALL Absolute(100)                                                      'Call machine code.
B = PEEK(&HAAA)                                                         'Get values for Buttons.
H = (PEEK(&HBBB) + PEEK(&HBBC) * 256)            'Horizontal position ( 2 bytes ).
V = (PEEK(&HCCC) + PEEK(&HCCD) * 256)           'Vertical position   ( 2 bytes ).
END SUB                                                                           'End of sub-program.



The following statements should be the first two commands in any program which uses this code:


DEFINT A-Z
DECLARE SUB Mouse (Funk)   ' Declare Mouse sub-program.


Along with this instructable, I have included a sample program ( "QBMOUSE.BAS" ) as well as a "template" of sorts     ( "MOUSE.SUB" ), which contains the Mouse sub-routine, and a blank main program, where you would enter your own code.   Should you have any questions, concerns or just gripes regarding this code, please feel free to contact me at; flurng@gmail.com.  I welcome any feedback, and I hope you enjoy creating exciting new mouse-enabled QBasic programs with the help of this sub-routine. 

When you're ready to begin, read on to the first step, and above all...... Have Fun!!!




 
Remove these adsRemove these ads by Signing Up
 

Step 1Download Files

Download Files
As the title of this step implies, all we'll be doing in this step is downloading the "template" file ( "MOUSE.SUB" ), which is a bare-bones skeleton file, used for creating a new mouse-aware program, and the sample program ( "QBMOUSE.BAS"), which demonstrates the mouse sub-routine in action!  Simply start by downloading these files into the folder on your hard-drive that contains the QBasic executable file. ( "QBASIC.EXE" )

When you're finished doing this, move on to Step 2........


« Previous StepDownload PDFView All StepsNext Step »
1 comment

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
0
Followers
1
Author:flurng