Kamis, 09 Mei 2013

Latihan 12 Visual Basic - Demo Event Keyboard

Latihan 12 Visual Basic - Demo Event Keyboard


'Deklarasi variabel global
Dim xpos As Single, ypos As Single

Private Sub Form_Load()
    'Pindahkan posisi roket ke tengah form
    xpos = (Me.ScaleWidth - picRoket.Width) / 2
    ypos = (Me.ScaleHeight - picRoket.Height) / 2
    picRoket.Move xpos, ypos
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    'MsgBox "Kode tombol yang ditekan : " & KeyCode
   
    'Cek tombol keyboard yang ditekan
    Select Case KeyCode
    Case vbKeyLeft  'Tombol panah kiri
        If Shift = vbCtrlMask Then
            Call RoketKeSisiKiri
        Else
            Call RoketKeKiri
        End If
    Case vbKeyRight 'Tombol panah kanan
        If Shift = vbCtrlMask Then
            Call RoketKeSisiKanan
        Else
            Call RoketKeKanan
        End If
    End Select
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    'MsgBox "Kode tombol yang dilepas : " & KeyCode
   
    'Cek tombol keyboard yang dilepas
    Select Case KeyCode
    Case vbKeyUp    'Tombol panah atas
        Call RoketKeAtas
    Case vbKeyDown  'Tombol panah bawah
        Call RoketKeBawah
    End Select
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    'MsgBox "Kode ASCII tombol yang ditekan : " & KeyAscii
   
    'Cek tombol keyboard yang ditekan
    If KeyAscii = vbKeyEscape Then  'Tombol Escape
        If MsgBox("Tutup program ?", vbQuestion + vbYesNo, _
                  Me.Caption) = vbYes Then Unload Me
    End If
End Sub

Private Sub RoketKeKiri()
    xpos = xpos - 10    'Geser ke kiri 10 pixel
    If xpos < 0 Then
        xpos = 0        'Cek bila melewati batas kiri
    End If
    picRoket.Move xpos  'Pindahkan posisi roket
End Sub

Private Sub RoketKeKanan()
    xpos = xpos + 10                                'Geser ke kanan 10 pixel
    If xpos > Me.ScaleWidth - picRoket.Width Then   'Cek bila melewati batas kanan
        xpos = Me.ScaleWidth - picRoket.Width
    End If
    picRoket.Move xpos                              'Pindahkan posisi roket
End Sub

Private Sub RoketKeAtas()
    ypos = ypos - 10            'Geser ke atas 10 pixel
    If ypos < 0 Then            'Cek bila melewati batas atas
        ypos = 0
    End If
    picRoket.Move xpos, ypos    'Pindahkan posisi roket
End Sub

Private Sub RoketKeBawah()
    ypos = ypos + 10                                 'Geser ke bawah 10 pixel
    If ypos > Me.ScaleHeight - picRoket.Height Then  'Cek bila melewati batas bawah
        ypos = Me.ScaleHeight - picRoket.Height
    End If
    picRoket.Move xpos, ypos                         'Pindahkan posisi roket
End Sub

Private Sub RoketKeSisiKiri()
    xpos = 0                    'Geser ke sisi kiri
    picRoket.Move xpos, ypos    'Pindahkan posisi roket
End Sub

Private Sub RoketKeSisiKanan()
    xpos = Me.ScaleWidth - picRoket.Width   'Geser ke sisi kanan
    picRoket.Move xpos, ypos                'Pindahkan posisi roket
End Sub

0 komentar:

Posting Komentar

 
Design by Wordpress Theme Template Blog Free | Bloggerized by Free Blogger Templates | coupon codes