Key Press Help

Hey, I'm new to VB programming, and to practice, I create simple games.
Anyway, I'm having problems with the Keypress funtion. Basicall, I want a picturebox (named pctrZ1) to move to the right by 20 whenever someone presses the "a" key. But im having trouble figuring this out, and the msdn help didnt do much for me either, plz help.

Comments

  • [b][red]This message was edited by Sagacious at 2002-10-19 21:51:43[/red][/b][hr]
    : Hey, I'm new to VB programming, and to practice, I create simple games.
    : Anyway, I'm having problems with the Keypress funtion. Basicall, I want a picturebox (named pctrZ1) to move to the right by 20 whenever someone presses the "a" key. But im having trouble figuring this out, and the msdn help didnt do much for me either, plz help.
    :
    :

    It's there if you look for it long enough, but I know what you mean about MSDN help. KeyPress is very picky; you must set the focus to your object and use ASCII precisely. An "a" is 97, while an "A" is 65. Try:

    [Code]
    Private Sub pctrZ1_KeyPress(KeyAscii As Integer)
    pctrZ1.SetFocus
    If KeyAscii = 97 Then
    pctrZ1.Left = pctrZ1.Left + 20
    End If
    Refresh
    End Sub
    [/Code]

    Using KeyDown may also work for you and you can use vb constants without the worry of case. The case is ignored unless you specify the state of the [shift] argument. Plus this is just easier to read.

    [Code]
    Private Sub pctrZ1_KeyDown(KeyCode As Integer, Shift As Integer)
    pctrZ1.SetFocus
    If KeyCode = vbKeyA Then
    pctrZ1.Left = pctrZ1.Left + 20
    End If
    Refresh
    End Sub
    [/Code]

    Hope this helps,
    Sagacious


    Then I was just thinking... What's the point if you can't move it all over, so >> Try this if it helps.

    [Code]
    Private Sub pctrZ1_KeyDown(KeyCode As Integer, Shift As Integer)
    pctrZ1.SetFocus
    Select Case KeyCode
    Case vbKeyA
    pctrZ1.Left = pctrZ1.Left - 20
    Case vbKeyS
    pctrZ1.Left = pctrZ1.Left + 20
    Case vbKeyW
    pctrZ1.Top = pctrZ1.Top - 20
    Case vbKeyZ
    pctrZ1.Top = pctrZ1.Top + 20
    End Select
    Refresh
    End Sub
    [/Code]

  • : [b][red]This message was edited by Sagacious at 2002-10-19 21:51:43[/red][/b][hr]
    : : Hey, I'm new to VB programming, and to practice, I create simple games.
    : : Anyway, I'm having problems with the Keypress funtion. Basicall, I want a picturebox (named pctrZ1) to move to the right by 20 whenever someone presses the "a" key. But im having trouble figuring this out, and the msdn help didnt do much for me either, plz help.
    : :
    : :
    :
    : It's there if you look for it long enough, but I know what you mean about MSDN help. KeyPress is very picky; you must set the focus to your object and use ASCII precisely. An "a" is 97, while an "A" is 65. Try:
    :
    : [Code]
    : Private Sub pctrZ1_KeyPress(KeyAscii As Integer)
    : pctrZ1.SetFocus
    : If KeyAscii = 97 Then
    : pctrZ1.Left = pctrZ1.Left + 20
    : End If
    : Refresh
    : End Sub
    : [/Code]
    :
    : Using KeyDown may also work for you and you can use vb constants without the worry of case. The case is ignored unless you specify the state of the [shift] argument. Plus this is just easier to read.
    :
    : [Code]
    : Private Sub pctrZ1_KeyDown(KeyCode As Integer, Shift As Integer)
    : pctrZ1.SetFocus
    : If KeyCode = vbKeyA Then
    : pctrZ1.Left = pctrZ1.Left + 20
    : End If
    : Refresh
    : End Sub
    : [/Code]
    :
    : Hope this helps,
    : Sagacious
    :
    :
    : Then I was just thinking... What's the point if you can't move it all over, so >> Try this if it helps.
    :
    : [Code]
    : Private Sub pctrZ1_KeyDown(KeyCode As Integer, Shift As Integer)
    : pctrZ1.SetFocus
    : Select Case KeyCode
    : Case vbKeyA
    : pctrZ1.Left = pctrZ1.Left - 20
    : Case vbKeyS
    : pctrZ1.Left = pctrZ1.Left + 20
    : Case vbKeyW
    : pctrZ1.Top = pctrZ1.Top - 20
    : Case vbKeyZ
    : pctrZ1.Top = pctrZ1.Top + 20
    : End Select
    : Refresh
    : End Sub
    : [/Code]
    :
    :
    Ok, thanks, that helps alot.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories