Howdy, Stranger!

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

Categories

Key Press Help

Script_OverridScript_Overrid Member Posts: 3
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

  • SagaciousSagacious Member Posts: 36
    [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]

  • Script_OverridScript_Overrid Member Posts: 3
    : [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.

  • Andre YoungAndre Young USAMember Posts: 0

    \ http://forcoder.org \ free video tutorials and ebooks about [ Assembly C MATLAB Swift Perl Scratch C++ Java PL/SQL C# Python Objective-C JavaScript R Visual Basic .NET Visual Basic Go Ruby Delphi PHP Prolog Fortran Scheme COBOL D SAS Hack Transact-SQL Clojure Kotlin Logo ABAP Dart VBScript Crystal Ada Julia Bash Lua Erlang Alice Awk ML F# FoxPro Apex Scala Lisp LabVIEW Rust ] ___________

Sign In or Register to comment.