Udostępnij za pośrednictwem


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

  • Anonymous
    February 19, 2014
    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.

  • Anonymous
    February 19, 2014
    The comment has been removed

  • Anonymous
    February 23, 2014
    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!!!

  • Anonymous
    February 23, 2014
    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!

  • Anonymous
    February 24, 2014
    He has returned! Team Trinity Force - assemble!!!!

  • Anonymous
    December 31, 2014
    Hey Guys What are you talking about???

  • Anonymous
    December 31, 2014
    I Have Not seen any word called "The Destroyer Of The Souls" in my History Book!!!!!

  • Anonymous
    March 11, 2015
    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...

  • Anonymous
    July 12, 2015
    FOOLS! THE IS NOT STOPPING ME NOW!!! I HAVE RETURNED FOR GOOD AND YOU ARE ALL UNDER MY COMMAND!!!

  • Anonymous
    February 12, 2016
    can you please tell me how to make a square on the place you click

  • Anonymous
    March 06, 2016
    Hello! If anyone has any questions, please ask in the Small Basic forum: social.msdn.microsoft.com/.../threads Thanks!