CSE 203 Closed Lab 11 Instructions

Table of Contents


1. Objectives

To learn about handling mouse events.


2. Set Up

  1. Two students should work together at one workstation.
  2. In one student's account, follow the instructions given below in section 3, Method.  Remember, trading roles (driver and non-driver) for each new session is a very good idea.

3. Method

  1. Start by opening "015 Mouse Handling.kpl" from the "1) Step-by-Step Tutorial" folder.
  2. Immediately Save As New Program with name "Line_Segments.kpl" in folder "0) My Own".
  3. In a comment at the top of the file, add author name(s) and the date appropriately.
  4. Change the program name to "Line_Segments" (if you haven't already done so).
  5. Run the program to experience how it behaves.  Try moving the mouse while you hold down the left mouse button, then lift up that button.  Do you notice that a line segment is drawn either when you press the button down or when you lift it up?  We'll change that behavior today.  Now stop the running program and make sure its window is closed.
  6. Use Intellisense to see the one Method and seventeen Properties available for Mouse handling.  (One way you can do this is by finding "MouseY", deleting the 'Y', typing a dot ('.'), and scrolling in the resulting pop-up window.)  Actually, there are only ten distinct properties, as some of the names are duplicated.  For example, the x-coordinate of the point the mouse is touching can be known either as MouseX or as Mouse.X.  The ability to use this latter name, Mouse.X, is why "X" is in the list.  Its fully-qualified name is Mouse.X and that's the only way to name that "X".  On the other hand, we don't have to use the fully qualified name, Mouse.MouseX, to reach "MouseX" because simply "MouseX" is available as an alternative to "Mouse.MouseX".  Is that clear as mud?  :-)
  7. I said "ten distinct properties" above, but it's really (and this is the last time I'll change my mind about it!) only seven because LeftPressed, MiddlePressed, and RightPressed are synonyms with testing Mouse.Button for equality with the Integer values 1, 3, and 2, respectively.
  8. The "while" loop in this program, the one that begins with "While Not IsKeyDown( Escape )", like similar loops in the other programs we've seen so far, is often known as "the main event-processing loop" or "the event loop".  It appears that it's a good idea to call the method ClearMouseEvents() just before the event loop because it clears pending mouse messages from the process queue.  Calling ClearMouseEvents() keeps any user mouse actions that may have occurred just after launching our program, and are still unprocessed, from being attended to by our event loop.  We want the user to see what's going on in our program before their mouse actions affect our program.  So, please place a call to ClearMouseEvents() before the event loop and run the program to assure yourselves that this call doesn't radically mess anything up.  When you're done, of course, stop the running program and make sure its window is closed.
  9. OK, let's focus on changing the program so that the line segment is drawn only when the left mouse button goes up.  For this job, the Mouse.Event property will be useful.  It's a String property, and you can use Intellisense to see what String values can be compared for equality with this property.  (One way to do this is to define a string object s, assign Mouse.Event to it (type "Define s As String = Mouse.Event"), and hover your mouse pointer over the word "Event".)  Now, see if you can figure out how you can change the program so that the line segment is drawn only when the left mouse button goes up.
  10. As you test your program, you may find that some of the "ButtonUp" mouse events get missed.  If you haven't yet encountered a missing "ButtonUp" event, try to play with the program longer to see if you can experience this problem.  This is an error in Phrogram that the authors currently don't know how to fix.  :-(  If sometime you experience this situation to be a big problem, there seems to be a way to reduce its severity.  This way is strange, but it illustrates how many things interact in a computer system.  I've found that the severity of the missing mouse events problem is somewhat reduced when I use Windows Media Player to play music!  If sometime you need help in playing this trick on Phrogram, let a proctor know.
  11. Now think about whether you want to use the mouse in your course project.  If so, think about what you want the mouse to do in your project.  Then, go on to think about how you would accomplish your plans.  Feel free to begin changing your project accordingly.

4. Proctor Help

If you have a question or get stuck, raise your hand and one of the proctors will come by to chat.