Contoh Penggunaan Event Keyboard dengan Visual Basic 6.0


Assalamu’alaikum Wr.Wb

Nantslight – Sore ni aq mau share tentang nggerakin sesuatu dengan keyboard, nggerakin apa ya.. nggerakin gambar mksdnya... (^_^). OK Langsung aj.. Pertama seperti biasa buka IDE Visual Basic-nya dulu..Kemudian tambahkan 1 PictureBox..





 Dengan Propertiesnya sebagai berikut..
Object
Propeties
Value
Form1
BackColor
Caption
KeyPreview
ScaleMode
StartUpPosition
Putih
Contoh Event Keyboard
True
3-Pixel
2-Center Screen
Picture1
Name
AutoSize
BackColor
BorderStyle
Picture

ScaleMode
picRoket
True
Putih
0-None
(terserah anda yang penting file icon yang berformat ico)
3-Pixel

Kemudian Masukkan kode berikut..
------------------------------------------------------------------------------------------------------------
Dim xpos As Single, ypos As Single


Private Sub Form_Load()
    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)
    Select Case KeyCode
    Case vbKeyLeft
        Call RoketKeKiri
    Case vbKeyRight
        Call RoketKeKanan
    End Select
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyUp
        Call RoketKeAtas
    Case vbKeyDown
        Call RoketKeBawah
    End Select
End Sub

Private Sub KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
        If MsgBox("Tutup Program?", vbQuestion + vbYesNo, Me.Caption) = vbYes Then Unload Me
    End If
End Sub

Private Sub RoketKeKiri()
xpos = xpos - 10
If xpos < 0 Then
xpos = 0
End If
picRoket.Move xpos
End Sub

Private Sub RoketKeKanan()
xpos = xpos + 10
If xpos > Me.ScaleWidth - picRoket.Width Then
xpos = Me.ScaleWidth - picRoket.Width
End If
picRoket.Move xpos
End Sub

Private Sub RoketKeAtas()
ypos = ypos - 10
If ypos < 0 Then
ypos = 0
End If
picRoket.Move xpos, ypos
End Sub

Private Sub RoketKeBawah()
ypos = ypos + 10
If ypos > Me.ScaleHeight - picRoket.Height Then
ypos = Me.ScaleHeight - picRoket.Height
End If
picRoket.Move xpos, ypos
End Sub

------------------------------------------------------------------------------------------------------------

Setelah itu jalankan aplikasi yang anda buat dan gerakkan icon dengan keyboard..



Selamat Mencoba..

Semoga Bermanfaat..

Wassalamu’alaikum Wr.Wb
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Leave a comment