Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 input
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
pavol




PostPosted: Tue Jan 03, 2006 11:33 am   Post subject: input

i am using the keyboard to move an object accross the screen (directional buttons). this is the code i use to get input from the keyboard
code:
Select Case vbKeyCode
    Case vbKeyUp
        'code for up

    Case vbKeyDown
        'code for down

....etc


this has its limits because you can't press two keys at once. ex, you can't press left and up and make the object go diagonally. this code is in the Form_KeyDown procedure, is there a solution using the code i have or will i have to use this kind of code in the Form_KeyPress procedure?
code:
If KeyAscii = 'whatever Then
    'move object
End If

or is that also not possible? i am lost Confused
any help is appreciated. thanks
Sponsor
Sponsor
Sponsor
sponsor
HazySmoke)345




PostPosted: Tue Jan 03, 2006 5:43 pm   Post subject: (No subject)

Try this for now (there might be a better solution): You need a Form1, a Label1, and a Timer1 with a nonzero interval (I used 50)

VisualBASIC:
Const Up = 0
Const Down = 1
Const Left_ = 2
Const Right = 3

Dim KeyHeld(0 To 3) As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            KeyHeld(Up) = True
        Case vbKeyDown
            KeyHeld(Down) = True
        Case vbKeyLeft
            KeyHeld(Left_) = True
        Case vbKeyRight
            KeyHeld(Right) = True
    End Select
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyUp
            KeyHeld(Up) = False
        Case vbKeyDown
            KeyHeld(Down) = False
        Case vbKeyLeft
            KeyHeld(Left_) = False
        Case vbKeyRight
            KeyHeld(Right) = False
    End Select
End Sub

Private Sub Timer1_Timer()
    If KeyHeld(Up) Then Label1.Top = Label1.Top - 50
    If KeyHeld(Down) Then Label1.Top = Label1.Top + 50
    If KeyHeld(Left_) Then Label1.Left = Label1.Left - 50
    If KeyHeld(Right) Then Label1.Left = Label1.Left + 50
End Sub
cool dude




PostPosted: Tue Jan 03, 2006 6:36 pm   Post subject: (No subject)

the way that i would do it would be like this

code:

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = 97 Then
        Shape1.Move Shape1.Left - 50, Shape1.Top - 50
    End If
End Sub


if u press "A" the ball will move diagonally. u could do this with any letter on the keyboard. to find out the keyascii for each letter in this form_keypress just type at the top print keyascii. every time u press letter on keyboard it will tell u the number for keyascii.
HazySmoke)345




PostPosted: Wed Jan 04, 2006 10:32 am   Post subject: (No subject)

There's something wrong with the keypress event: Notice that, when you're holding a key, this is what happens, let's say that you hit the key "A", the computer will display:

A [ after a while ] AAAAAAAAAAAAAAAAA

There's a waiting time between the first A and the second A, and makes the movement uneven. Surely pavol wants all the movements to be even, right? And besides, pavol wants to achieve the diagonal movement while holding two keys at a time, so key A wouldn't work that well, would it?
cool dude




PostPosted: Wed Jan 04, 2006 12:53 pm   Post subject: (No subject)

wat do u mean the computer starts displaying A's it doesn't for me. anyways if pavol does want to make it work with holding two buttons then u could just add if keyaskii =97 and someother button. although about the smoothness your way is smoother Smile
pavol




PostPosted: Thu Jan 05, 2006 4:17 pm   Post subject: (No subject)

thanks HazySmoke)345
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: