|
-
Jun 17th, 2000, 09:13 AM
#1
Thread Starter
Junior Member
Why doesn't this work???????
i'm trying to get picture1 to move around the screen using the keyboard!!
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = Chr(104) Then Picture1.Left = Picture1.Left + 100
If KeyAscii = Chr(107) Then Picture1.Left = Picture1.Left - 100
If KeyAscii = Chr(117) Then Picture1.Top = Picture1.Top + 100
If KeyAscii = Chr(109) Then Picture1.Top = Picture1.Top - 100
End Sub
Thanx
-
Jun 17th, 2000, 09:29 AM
#2
A better way of doing it is using the GetAsyncKeyState API.
Code for module.
Code:
Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer
Code for a Timer on the Form.
Code:
Privaye Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyUp) Then Pic1.Top = Pic1.Top - 100
If GetAsyncKeyState(vbKeyDown) Then Pic1.Top = Pic1.Top + 100
If GetAsyncKeyState(vbKeyLeft) Then Pic1.Left = Pic1.Left - 100
If GetAsyncKeyState(vbKeyRight) Then Pic1.Left = Pic1.Left + 100
End Sub
-
Jun 17th, 2000, 09:35 AM
#3
Thread Starter
Junior Member
Yeh i know but it's for an assignment, and we have to have the use of ASCII conversions. is what u said using ASCII?
-
Jun 17th, 2000, 09:57 AM
#4
Lively Member
just put your form keypreview propriety at true.
Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
If chr(KeyAscii) = "h" Then Picture1.Left = Picture1.Left + 100
If chr(KeyAscii) = "k" Then Picture1.Left = Picture1.Left - 100
If chr(KeyAscii) = "u" Then Picture1.Top = Picture1.Top + 100
If chr(KeyAscii) = "m" Then Picture1.Top = Picture1.Top - 100
End Sub
Its not elegant but it work as you need it to.
If possible please provide codes.
Process by example is the best way to learn.
-
Jun 17th, 2000, 01:54 PM
#5
PowerPoster
almost the samw question.
It almost same as this Thread
-
Jun 17th, 2000, 09:50 PM
#6
The GetAsyncKeyState is more flexible than using the KeyPress or KeyDown event because this allows for multiple KeyDown's, Hence if you press DOWN and LEFT, the Picture will move diagonally, whereas if you do this with the KeyDown or KeyPress, it will just stop because 2 keys are pressed.
Yes, this method is, in a sense, using ASCII because vbKeyUp and vbKeyDown etc. are all Constants for the actual ASCII value.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|