Small Basic #14: Mouse and Keyboard Events



If you’re interested in creating basic text-based programs, Microsoft Small Basic has TextWindow and Text objects are probably enough for your needs. However, if you’re interested in interactive graphical programs (such as games or rich personal productivity applications), Microsoft Small Basic has a GraphicsWindow object that supports mouse and keyboard events. To learn about how it works, you can try importing the following code into Small Basic using ID KKG377 and then running it:

‘ Standard code for hooking up key, mouse, and text input actions to the graphics window.
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.MouseUp = OnMouseUp
GraphicsWindow.TextInput = OnTextInput

Sub OnKeyDown
‘ Code for key presses goes here.
GraphicsWindow.Title = “‘” + GraphicsWindow.LastKey + “‘ pressed”
EndSub

Sub OnKeyUp
‘ Code for key releases goes here.
GraphicsWindow.Title = “‘” + GraphicsWindow.LastKey + “‘ released”
EndSub

Sub OnMouseDown
‘ Code for mouse button presses goes here.
If Mouse.IsLeftButtonDown Then
GraphicsWindow.Title = “Left button pressed”
ElseIf Mouse.IsRightButtonDown Then
GraphicsWindow.Title = “Right button pressed”
Else
GraphicsWindow.Title = “Some mouse button pressed (other than left and right)”
EndIf
EndSub

Sub OnMouseMove
‘ Code for mouse moves goes here.
‘ GraphicsWindow.MouseX and GraphicsWindow.MouseY are relative to the graphics window.
‘ Mouse.MouseX and Mouse.MouseY are relative to the entire screen, which is typically bigger than the graphics window.
GraphicsWindow.Title = “GWX = ” + GraphicsWindow.MouseX + “, GWY = ” + GraphicsWindow.MouseY + “, ScreenX = ” + Mouse.MouseX + “, ScreenY = ” + Mouse.MouseY
EndSub

Sub OnMouseUp
‘ Code for mouse button releases goes here.
GraphicsWindow.Title = “Some mouse button released”
EndSub

Sub OnTextInput
‘ Code for text inputs goes here.
EndSub


Notice that as you move the mouse, click and release mouse buttons, and press and release keys on the keyboard, the graphics window displays information about the mouse and key events. You can use this information to make your program take action in response to these events. 

Comments (11)

  1. Cameron the Annihilator says:

    I found this to be helpful. I can now use the graphics window effectively and have you to thank!

    You may now have my firstborn child.

  2. Sally Soother says:

    Cameron, you fool! Don't you know that this man – Paul Cornell – is the destroyer of souls???? If you give him one soul, he can quickly infest the lifeline connecting you to your child and KILL YOU AS WELL! YOU MUST REVOKE THAT SENTENCE!!!!

  3. Cameron the Annihilator says:

    No… the destroyer of souls does not exist. He was vanquished in the Trial of the Third Sun! Do you not understand? We are free from his reign!!!

  4. Paul Cornell says:

    I was not vanquished. The Third Sun was just a setback… my time has returned. You and your firstborn child are just the beginning. After you, Cameron, comes the entire universe!

  5. Sally Soother says:

    He has returned! Team Trinity Force – assemble!!!!

  6. Ami Buch says:

    Hey Guys What are you talking about???

  7. Ami BUCH says:

    I Have Not seen any word called "The Destroyer Of The Souls" in my History Book!!!!!

  8. Iona Erofeeff says:

    That is because it is not, in fact, one word. It is a polynomial nomenclatural phrase. However, the reason you have not seem it in your textbooks is because, of course, The Destroyer of Souls' minions are very loyal and have destroyed evidence of his existence until the appropriate time. He has now risen to power, and the only thing we can do stop him is nothing. At best, we can slow him. This would be futile, though, as we should spend the rest our existence with our friends and family. All is lost…

  9. The destroyer of souls says:

    FOOLS! THE IS NOT STOPPING ME NOW!!! I HAVE RETURNED FOR GOOD AND YOU ARE ALL UNDER MY COMMAND!!!

  10. help says:

    can you please tell me how to make a square on the place you click

  11. Hello! If anyone has any questions, please ask in the Small Basic forum:

    social.msdn.microsoft.com/…/threads

    Thanks!