Introduction: Using a Mouse in QBasic Programs

Picture of 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:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SUB Mouse (Funk) STATIC
SHARED B, H, V
STATIC Crsr
IF Funk = 1 THEN Crsr = 1
IF Funk = 2 AND Crsr = 0 THEN EXIT SUB
IF Funk = 2 AND Crsr = 1 THEN : Crsr = 0
POKE 100, 184: POKE 101, Funk: POKE 102, 0
POKE 103, 205: POKE 104, 51: POKE 105, 137
POKE 106, 30: POKE 107, 170: POKE 108, 10
POKE 109, 137: POKE 110, 14: POKE 111, 187
POKE 112, 11: POKE 113, 137: POKE 114, 22
POKE 115, 204: POKE 116, 12: POKE 117, 203
CALL Absolute(100)
B = PEEK(&HAAA)
H = (PEEK(&HBBB) + PEEK(&HBBC) * 256)
V = (PEEK(&HCCC) + PEEK(&HCCD) * 256)
END SUB

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 bare-bones "template" of sorts ( "MOUSE.SUB" ), which contains the Mouse sub-routine and a blank main program, where you would enter your own code, along with a few sample programs; ( "QBMOUSE.BAS", "BUTTONS.BAS" and "PONG3.BAS" ), which should give you enough examples to get you started. 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!!!

Step 1: Download Files

Picture of 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 a few sample programs ( "QBMOUSE.BAS"), which demonstrates the mouse sub-routine in action! I've also included ("Buttons.bas"), a simple "menu" style program and ("Pong3.bas"), a version of the classic "Pong" game. 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........

Step 2: Load the Template File ( MOUSE.SUB )

Picture of Load the Template File ( MOUSE.SUB )

Assuming that you are at least familiar with programming in QBasic, I don't imagine I need to tell you how to open an existing Basic file! However, if you'll notice, our template file, "MOUSE.SUB", has the extension ".SUB", so it won't show up in the list of basic files you may already have on your hard-drive. Therefore, you'll need to type the full name of the file into the "File Name" box, and then press "Enter" ( Or, click the "OK" button ). This is intentional; it sort of "keeps it out of the way" while you're searching through your source files, and prevents you from accidentally opening ( and editing ) the file. Plus, as you create your own sub-routines, you can build a "library" of sorts; essentially a collection of files, each with a handy, re-usable sub-routine, and give them all the ".SUB" extension. Then, you can just copy these routines & "pop" them into any future project you may create!

Once you've got the template file loaded, move on to the next step.....

Step 3: Editing the Template File

Picture of Editing the Template File

Awright, looks like we're just about ready to go! Now, the first thing you'll want to do is CHANGE THE FILE NAME! This step is important - it keeps the template file in it's original condition, so that should any thing go wrong, you'll be able to start over with the original template file. Also, you'll want to give your new file the ".BAS" extension, so that it will show up in the file list box, next time you wish to load it! To change the file name, we simply click on the "File" menu in the top left corner of the screen, and then click "Save As..." from the drop menu. This will bring up an input box, where you'll supply the name you wish to give your new project.

Now, we're ready to begin editing. To do this, simply move the cursor down to the line that reads "Enter your code here" and, you guessed it; enter your code here! As to what code you should enter, well, that depends on what type of program you wish to create, although I will typically start with the command "Mouse 1", which just displays the mouse cursor as mentioned before.

Keep in mind however, that you may not want to display the cursor at all! For example, suppose you want to create the latest, greatest SLAMMIN' new version of the arcade classic "Pong". For a game like this, you can use the mouse to move your "paddle" around, simply by reading the "H" co-ordinate ( with the command "Mouse 3" ), and then placing the paddle accordingly. In this situation, the mouse cursor would just get in the way if it were visible, so there's no need to display it at all!

For a menu- or button- driven application however, you will certainly want to display the cursor, so that you know what you are clicking on! Just remember to hide the cursor ( using "Mouse 2" ) before you send any text or graphics to the screen, so that it doesn't garble any of your artwork. You can always turn the cursor back on by calling "Mouse 1" at any time!

To make your program respond to specific actions, such as button clicks, you might use an "IF" statement like the following;
"IF B = 1 THEN......" This causes the program to do something whenever the left button is pressed. Use "B = 2" to intercept the right button, "B = 3" for both, or even "B > 0" to activate on ANY combination of buttons! See the sample file "QBMOUSE.BAS" for more examples of this and other techniques for interfacing with the mouse.

If at any point it seems that your program just isn't responding to the mouse at all, it's probably because you haven't called "Mouse 3" before trying to use the B, H or V values. Remember, you MUST read the mouse first, in order to ensure that these values are correct and current! That's why I typically make the command the first statement within a loop, just so I know the mouse variables are always up-to-date!

Another handy trick I have found is to put the following line somewhere within the main loop of your program:
"Mouse 3: Locate 1,1: Print B;H;V" This just reads the mouse and prints the values of the mouse buttons, horizontal & vertical co-ordinates up in the top left corner of the screen, so that you can instantly see what's happening with the mouse. This helps immensely with development and de-bugging, and once your project is finished, you simply delete the line, and you're all set!

That should just about do it! As I mentioned before, check out the sample program, and really try to get a handle on just what's happening with the mouse before you get started. And as always, please contact me at; flurng@gmail.com with any questions or comments! But above all, HAVE FUN!!!


Update: For those who find it easier to learn visually, I have just uploaded the following video to Youtube:


Please feel free to leave any questions or comments....( and TRY to stay awake! [Yawn! ;^] )

Comments

flurng (author)2011-08-31

Well, I just re-read my instructable and I'm starting to think I sound "simply" ridiculous! Ah, well - guess I'll SIMPLY have to deal with it!

SamH223 (author)flurng2017-10-08

is that mess to me ????

SamH223 (author)SamH2232017-10-08

*message

SamH223 (author)2017-10-08

what if I wanted to make a " start menu like " button so how would I get the program to calculate where the mouse is then the mouse clicks in the bottom right then a menu pops up .

flurng (author)SamH2232017-10-12

Greetings, SamH223! If you download and open the file; "Buttons.bas", it should give you an idea how it all works, but the basic process goes as such;
1.Draw a button on screen. (Let's suppose It's 100 pixels from the left to 200 pixels from the left, and 100 pixels from the top to 200 down.)
2.Create a loop to check the mouse coordinates repeatedly.
3. Call Mouse 3 to get the current coordinates and button status.
4.Write an "if" statement, such as; "If B = 1 then gosub WhichButton"
5. Create a "WhichButton" subroutine, such as;
"WhichButton:
Select case v
Case 100 to 200:
Gosub (do first button)
Case 201 to 300:
Gosub (do next button)
Case 301 to 400:
Gosub (do next, etc...)
End select
End sub

6. Create a subroutine for each button you created, and the the "WhichButton"
Routine should branch to the appropriate subroutine.

Naturally, feel free to use whatever labels make sense for program. For instance, you might create an "Open" routine, or "Options", or "Quit", etc.... whatever your program requires. Then, just create appropriate subroutines to handle each operation.

Hope this helps & thanks for your interest in my code. And of course, should you have more questions, feel free to ask anything!
So long for now & happy coding!
Flurng

SamH223 (author)SamH2232017-10-08

and can you give the code , and put comments on the code so I know what dose w

SamH223 (author)SamH2232017-10-08

what

veganaize (author)2015-03-09

This routine is somewhat ambiguous. The coordinates and button stats don't seem to be returned correctly and therefore it is mostly unusable. I made a simple and universal mouse routine for QB that is very similar to this idea after pouring over the INT 33h mouse sub-functions for DOS myself:

http://staticchaos.awardspace.com/basic/qbmouse.ph...

It is the simplest (and fully commented) QB mouse routine available (and it works).

flurng (author)veganaize2015-09-08

Greetings, Veganaize! I am disappointed, and more than a bit mystified that my code did not work for you, as it works perfectly for me on every machine I've tried it on. ( One caveat: on certain systems, I need to run QBasic through DOSbox, rather than directly from the command line. ) I am unsure whether that might have anything to do with your experience, however I see you've successfully created your own code, and I'm glad you were able to get it working!

veganaize (author)flurng2015-09-21

I don't think that you took the various screen modes into account. You might have failed to test it under multiple resolutions. I don't think that I, originally, noticed that there were .sub and .bas files available for download; something may have been lost in translation. It would be nice if you could re-format your article to make the code more clear for online viewers. Thank you for the effort! I respect anyone who values the wealth of computing knowledge available via QBasic ~ Especially these days!

About This Instructable

8,936views

2favorites

License:

More by flurng:Using a mouse in QBasic programs
Add instructable to: