Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill...

18
Timer, Animation Responding to Mouse & Keyboard Lab 7 McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Transcript of Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill...

Page 1: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Timer, AnimationResponding to Mouse &

Keyboard

Lab 77

McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Page 2: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

The Timer Control

A control that enables you to generate response based on the value of the computer’s internal clock.

Properties:Interval: Specify the frequency of timer events

(measured in milliseconds = 1/1000 second).Enabled : False/TrueEvents:Tick: The only event where you place the code for the

timer control.

Page 3: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

The MouseThe Mouse

The mouse is an object that is attached to the computer allowing the user to interact with the machine.

The mouse can accomplish some tasks that are not normally available on the other.

Page 4: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

The Mouse….ContThe Mouse….Cont

The mouse is equipped with: two, three, or more buttons. When a mouse has two buttons, one is usually located

on the left and the other is located on the right. When a mouse has three buttons, one usually is in the

middle of the other two. A mouse can also have a round object referred to as a

wheel.

Page 5: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

The Mouse….ContThe Mouse….Cont

The mouse is used to select a point or position on the screen. To use the mouse, the user would press either the left, the middle

(if any), or the right button. If the user presses the left button once, this action is called Click. If the user presses the right mouse button, the action is referred

to as Right-Click. If the user presses the left button twice and very fast, the action is

called Double-Click. If the mouse is equipped with a wheel, the user can position the

mouse pointer somewhere on the screen and roll the wheel. This usually causes the document or page to scroll up or down, slow or fast, depending on how it was configured.

Page 6: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Mouse Enter Event

When positioninig the mouse on a control, the control fires a MouseEnter event. This event is initiated as follows:

Private Sub ControlName_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseEnter

This event let you know that the mouse was positioned on a control.

Page 7: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Mouse Move Event

Whenever the mouse is being moved on top of a control, MouseMove event is sent.

Private Sub Object_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

End SubTracking the mouse:

e.X >>x position e.Y >> y position

Page 8: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Mouse Hover Event

If the user positions the mouse on a control and hovers over it, a MouseHover event is fired.

Private Sub Object_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseHover

End Sub

Page 9: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Mouse Down Event

Occurs when the user press down one of the mouse buttons while it is positioned on a control.

Private Sub Object_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

End Sub

Note: e.button (specify which button is clicked)

Page 10: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Mouse Up Event

After pressing a mouse button, while the button is being released, a button-up message is sent.

Private Sub Object_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

End Sub

Page 11: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Mouse Wheel Event

If a mouse is equipped with a wheel, when the user presses the wheel or starts turning it, a MouseWheel event is fired.

You can use this event to specify what would happen if or when the user decides to use the wheel.

Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel

End Sub

Page 12: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Mouse Leave Event

When the user moves the mouse pointer away from a control, the control fires a MouseLeave event.

Private Sub Form1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave

End Sub

Page 13: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

The Key Down Message

When a keyboard key is pressed, a message called KeyDown is sent.

Private Sub Object_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

End SubTo detect the key, use:

e.Keydata or e.kayvalue

Page 14: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Key Press Event

When the user presses a key, the KeyPress message is sent.

Key pressed for this event should (must) be a character key.

Private Sub Object_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

End SubTo detect the key you can use (e.keychar method)

Page 15: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Key Up Event

KeyUp message is sent when the user releases the key.

Private Sub Object_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

End Sub

To detect the key, use:e.keydata e.keyvalue

Page 16: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Keyboard supprot

Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown

MsgBox(e.KeyData)

If e.KeyCode = Keys.A Then

MsgBox("a")

End If

End Sub

Page 17: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Lab Training I

Design a digital clock. Design an interface to move an object (button) in

different directions as follow:

1. U : up

2. D : down

3. L : left

4. R : right

5. N : right diagonal

6. M : Left diagonal

Page 18: Timer, Animation Responding to Mouse & Keyboard Lab 7 7 McGraw-Hill© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

End