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:

0 None
1 Left
2 Right
3 Both

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

SUB Mouse (Funk) STATIC
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)
H = (PEEK(&HBBB) + PEEK(&HBBC) * 256)
V = (PEEK(&HCCC) + PEEK(&HCCD) * 256)

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

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; 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

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 )

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

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; 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! ;^] )



    • Pocket Sized Contest

      Pocket Sized Contest
    • Tape Contest

      Tape Contest
    • Paper Contest

      Paper Contest

    17 Discussions


    7 years ago on Step 3

    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!

    6 replies

    Reply 1 year ago

    is that mess to me ????


    Reply 1 year ago

    you can do it like this:



    _delay 1/60


    Pset (x,y),15

    Do while _mouseinput






    Reply 1 year ago

    Greetings, AndrejV16, and thanks for the input! I don't believe that specific syntax will work in QBasic, but the concept is sound, and I'm sure a very handy techinique in more advanced languages! Thanks again, & as always, cheers! And happy coding!


    Reply 1 year ago

    Yes, sorry for the confusion, but as I mentioned, if you download and run the sample programs, the process is pretty straight- forward. Essentially, there are just a few steps; first, you'd create your on-screen menu or "buttons", then, call "Mouse 1" to display the mouse cursor. At this point, you can see the cursor, but your program still has no idea what the mouse is doing. To get mouse data, start a loop and call "Mouse 3"; this loads the variables "B", "H" and "V" with the mouse button staus, and Horizontal and Vertical co-ordinates, respectively. Now, just test the variables with an "If" statement, such as, "If B = 1 AND H > (top co-ordinate of your menu button) AND H < (bottom co-ordinate) AND V > (left side co-ordinate) AND V < (right side co-ordinate) THEN (petform some command)". If all those conditions are met, it means you've clicked on your menu option or button, and any command you specify should execute. Hope this helps, & please let me know if you have any more questions or (hopefully), success with my code! Cheers & happy coding!


    Reply 1 year ago

    Oops! Sorry, gang - I just spotted a mistake in my previous post; in th "if" statement, I swapped the "H" & "V" variable names! It should instead read; "If B = 1 AND V > (top co-ordinate of your menu button) AND V < (bottom co-ordinate) AND H > (left side co-ordinate) AND H < (right side co-ordinate) THEN (perform some command)". Sorry for any confusion, & PLEASE let me know if you have any further problems or, (hopefully) success! &, As always, Cheers & happy coding!


    6 months ago

    Greetings, folks! I just heard from a kind fellow that apparently, one must now purchase a premium membership at in order to download any files from the site. Therefore, if you prefer, you may e-mail me directly at - just put "requesting mouse code" in the subject line, and I will happily send you a copy of the code. Thanks for your interest in my code & happy programming!


    Answer 1 year ago

    Greetings, aadarshamitsah06!

    To answer your question, the "poke" commands place numeric values directly into memory, in this case starting at offset 100, up to offset 117. The values listed correspond to Assembly language commands which access the mouse.
    The specific Assembly commands are as follows:
    Mov AX, 0000
    Int 33
    mov [0AAA],BX
    mov [0BBB],CX
    mov [0CCC],DX

    Note that the second "poke" command puts the value of Funk ( Either 1, 2 or 3, depending on which function is called) into memory at offset 101.

    Once these values are poked into memory, the command "Call Absolute (100)" calls up the values in memory, and executes them as an assembly language routine. Hope this answers your question, and that my code helps you write many useful programs! Enjoy, and have fun programming!


    1 year ago

    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 .

    3 replies

    Reply 1 year ago

    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;
    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!


    Reply 1 year ago

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


    4 years ago on Introduction

    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:

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

    2 replies

    Reply 3 years ago on Introduction

    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!


    Reply 3 years ago on Introduction

    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!