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

Username:   Password: 
 RegisterRegister   
 Need help with Keypress
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
zwnage




PostPosted: Sat Jan 20, 2007 12:28 am   Post subject: Need help with Keypress

I'm pretty new to visual basic, and for some reason, I cannot get keyboard inputs to work. I want to know when the left or right arrow keys are pressed. It is to replace my joystick so that I can test my program at home. Can anyone help? Thanks for your time.

EDIT: I got the inputs to work, but only when there isn't a textbox on the form. Where there is one, I'm writing into the textbox instead of regestering the keypresses like I want them to.
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Sat Jan 20, 2007 11:04 am   Post subject: Re: Need help with Keypress

Can you please be a little more specific. You say that your registering the key presses into the text box instead of how you want it. But how do you want it? What exactly are you trying to do?

P.S. Dan the times are screwed up because he posted at 12:28 and i'm posting at 11:04 how is that possible? I guess its something to do with time differences.
zwnage




PostPosted: Sat Jan 20, 2007 2:52 pm   Post subject: Re: Need help with Keypress

I'll give an example:

Here's a code I copied from an earlier post, with a few very small changes:
code:

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

Dim KeyHeld(0 To 3) As Boolean
Dim ctrl 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
        Case vbKeyControl
            ctrl = 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
        Case vbKeyControl
            ctrl = 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
    If ctrl Then Label1.Visible = False
End Sub


It uses a timer, a form, and a label. When I press a arrow key, it moves the label. Now if I add a textbox to the form, instead of moving the label when I press the arrow keys, it moves the curser in the text box. How would I fix that?
Monstrosity_




PostPosted: Sat Jan 20, 2007 3:05 pm   Post subject: Re: Need help with Keypress

zwnage @ Sat Jan 20, 2007 1:28 am wrote:
I got the inputs to work, but only when there isn't a textbox on the form. Where there is one, I'm writing into the textbox instead of regestering the keypresses like I want them to.

Change the forms 'KeyPreview' to True.. Its been a while, but this should pass key events from the forms controls to the form.
cool dude




PostPosted: Sat Jan 20, 2007 3:47 pm   Post subject: Re: Need help with Keypress

yes that is exactly right. Another way would be to disable the textbox on your form but if you have it there i'm assuming that you don't won't to disable it.
zwnage




PostPosted: Sat Jan 20, 2007 4:24 pm   Post subject: RE:Need help with Keypress

Thanks for the help, the keypreview thing worked! Very Happy
MIdas0036




PostPosted: Tue Dec 11, 2007 4:41 pm   Post subject: RE:Need help with Keypress

What about using ascii code for the key press function
Silent Avenger




PostPosted: Tue Dec 11, 2007 5:11 pm   Post subject: Re: RE:Need help with Keypress

MIdas0036 @ Tue Dec 11, 2007 4:41 pm wrote:
What about using ascii code for the key press function


I think ascii code is the easiest way to detect when a key is pressed especially if you're new to VB.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: