' Ch 7 Demo #6
' Mr Minich
' This program illustrates the use of the Form_KeyPress event being
' used to control the movement of Shape controls
Option Explicit
Private Sub Form_KeyPress(KeyAscii As Integer)
' The Form must have "the focus" in order for the Form_KeyPress event
' to work.
'Player 1
If KeyAscii = 97 Then 'works since the ASCII
' value of 'a' is 97
shpPlayer1.Left = shpPlayer1.Left - 100 'moving to the left
ElseIf KeyAscii = Asc("s") Then 'it may be easier to use
' the Asc function though
shpPlayer1.Left = shpPlayer1.Left + 100 'moving to the right
ElseIf KeyAscii = Asc("w") Then
shpPlayer1.Top = shpPlayer1.Top - 100 'moving upward
ElseIf KeyAscii = Asc("z") Then
shpPlayer1.Top = shpPlayer1.Top + 100 'moving downward
End If
' Player 2
If KeyAscii = Asc("j") Then
shpPlayer2.Left = shpPlayer2.Left - 100
ElseIf KeyAscii = Asc("k") Then
shpPlayer2.Left = shpPlayer2.Left + 100
ElseIf KeyAscii = Asc("i") Then
shpPlayer2.Top = shpPlayer2.Top - 100
ElseIf KeyAscii = Asc("m") Then
shpPlayer2.Top = shpPlayer2.Top + 100
End If
End Sub