Keyboard controls - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Keyboard controls

Runt1128Runt1128 Posts: 18Member
When my program runs, i need to be able to pause, start, and exit it whenever needed by using the keyboard. I know i have to use the KeyPress command and im guessing ascii to use which ever keys needed. But how do i go about setting this up? i read the msdn site and i need a little more info.

Comments

  • hobbyhobby Posts: 84Member
    : When my program runs, i need to be able to pause, start, and exit it whenever needed by using the keyboard. I know i have to use the KeyPress command and im guessing ascii to use which ever keys needed. But how do i go about setting this up? i read the msdn site and i need a little more info.
    :

    Use the Form_KeyPress(KeyAscii as integer) Event. It's called whenever the form has focus and a key is pressed. (Note, if a textbox or picturebox or other control has the focus on the form, then the form will NOT receive the keypress, but that control will receive the keypress event). The KeyAscii is the Ascii number (A=68).

    For example:
    [code]
    Private Sub Form_KeyPress(KeyAscii as Integer)
    Debug.print Chr(KeyAscii) ' Output the keypress
    If Lcase(Chr(KeyAscii)) = "p"
    ' Code here to pause
    Elseif LCase(Chr(KeyAscii)) = "x"
    ' Code here to exit
    Elseif KeyAscii = 68 ' A is pressed, checking number instead of character.
    ' Code here to do something
    End if
    End Sub
    [/code]

  • Runt1128Runt1128 Posts: 18Member
    Well i understand the syntax of the keypress command, but can you tell me how i would go about pausing a program using a single key? like the number 1.
  • HardCodedHardCoded Posts: 14Member
    : Well i understand the syntax of the keypress command, but can you tell me how i would go about pausing a program using a single key? like the number 1.
    :
    Well, I guess that you can use a little api. Search for it in the API text viewer:
    Sleep - and the argument is in miliseconds.


    [code]
    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    'This is to declare the function in the declaration section of the form

    Private Sub Form_KeyPress(KeyAscii as Integer)
    If Chr(KeyAscii) = "1"
    Sleep 3000 'pauses the app for 3secs
    End if
    End Sub
    [/code]

    Hope this helps - This includes API, but its simple.

    [b]Hard[b][grey][italic]Coded[/italic][/grey]

  • Runt1128Runt1128 Posts: 18Member
    I know i can use the API but from what im seeing i can pause it for a certain amount of time. Sorry i wasn't clear. I need to press a key to pause the program, stop it in its tracks.....then press another key to start it up again from where it left off.

    : Well, I guess that you can use a little api. Search for it in the API text viewer:
    : Sleep - and the argument is in miliseconds.
    :
    :
    : [code]
    : Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    : 'This is to declare the function in the declaration section of the form
    :
    : Private Sub Form_KeyPress(KeyAscii as Integer)
    : If Chr(KeyAscii) = "1"
    : Sleep 3000 'pauses the app for 3secs
    : End if
    : End Sub
    : [/code]
    :
    : Hope this helps - This includes API, but its simple.
    :
    : [b]Hard[b][grey][italic]Coded[/italic][/grey]
    :
    :

  • HardCodedHardCoded Posts: 14Member
    : I know i can use the API but from what im seeing i can pause it for a certain amount of time. Sorry i wasn't clear. I need to press a key to pause the program, stop it in its tracks.....then press another key to start it up again from where it left off.
    :
    : : Well, I guess that you can use a little api. Search for it in the API text viewer:
    : : Sleep - and the argument is in miliseconds.
    : :
    : :
    : : [code]
    : : Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    : : 'This is to declare the function in the declaration section of the form
    : :
    : : Private Sub Form_KeyPress(KeyAscii as Integer)
    : : If Chr(KeyAscii) = "1"
    : : Sleep 3000 'pauses the app for 3secs
    : : End if
    : : End Sub
    : : [/code]
    : :
    : : Hope this helps - This includes API, but its simple.
    : :
    : :
    Ok.I got it.In that case you can is put a timer runnig, with an api function that goes checking what keys are being pressed (you can find the code for this and adaptit in a keylogger tut) and then aply the func Sleep. This func doens stop every thing.

    [b]Hard[b][grey][italic]Coded[/italic][/grey]
  • Runt1128Runt1128 Posts: 18Member
    : : : Well, I guess that you can use a little api. Search for it in the API text viewer:
    : : : Sleep - and the argument is in miliseconds.
    : : :
    : : :
    : : : [code]
    : : : Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    : : : 'This is to declare the function in the declaration section of the form
    : : :
    : : : Private Sub Form_KeyPress(KeyAscii as Integer)
    : : : If Chr(KeyAscii) = "1"
    : : : Sleep 3000 'pauses the app for 3secs
    : : : End if
    : : : End Sub
    : : : [/code]
    : : :
    : : : Hope this helps - This includes API, but its simple.
    : : :
    : : :
    : Ok.I got it.In that case you can is put a timer runnig, with an api function that goes checking what keys are being pressed (you can find the code for this and adaptit in a keylogger tut) and then aply the func Sleep. This func doens stop every thing.
    :
    : [b]Hard[b][grey][italic]Coded[/italic][/grey]
    :
    Ok so use the same api function as stated above? And how do i know how long the timer should run for? never had to use one before?

  • Runt1128Runt1128 Posts: 18Member
    I think what im trying to say is....Does the timer have to be specified? or does it run continuously until the user ends the program?
  • Runt1128Runt1128 Posts: 18Member
    this works:

    Dim mloop As Boolean

    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKey2 Then 'pause
    mloop = True
    ElseIf KeyAscii = vbKey4 Then 'continue
    mloop = False
    ElseIf KeyAscii = vbKeyReturn Then
    End
    End If
    End Sub

    Private Sub Timer1_Timer()
    Do While mloop = True
    Debug.Print "looping"
    DoEvents
    DoEvents
    DoEvents
    DoEvents
    If mloop = False Then
    Exit Sub
    End If
    Loop
    End Sub

    Private Sub Form_Load()
    Label1.TabIndex = 0
    End Sub


  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : When my program runs, i need to be able to pause, start, and exit it whenever needed by using the keyboard. I know i have to use the KeyPress command and im guessing ascii to use which ever keys needed. But how do i go about setting this up? i read the msdn site and i need a little more info.
    :

    If there is a specific control that will always have the focus, put this code in it's KeyPress event. Otherwise, set the form's KeyPreview property to True and put this in the form's KeyPress event:
    [code]
    If LCase$(Chr$(KeyAscii)) = "p" Then
    Paused = Not Paused
    ElseIf LCase$(Chr$(KeyAscii)) = "x" Then
    'exit code here
    End If
    [/code]
    If there is only one form, put this in it's general declarations section:
    [code]
    Private Paused As Boolean
    [/code]
    Otherwise, change Private to Public and put it in a .bas module instead.

    In any event (or loop) that controls the game, check the value of Paused to see if you need to execute the code. Here's a small psuedocode example:
    [code]
    Do
    If Not Paused Then
    UpdateEnemyPositions
    UpdatePlayerPosition
    RedrawScreen
    Else
    Sleep 100
    End If
    DoEvents
    Loop
    [/code]
    Or perhaps you would simply use it to ignore keypresses or mouse events.

    Either way, using Sleep will freeze your app for the indicated amount of time. So use a small value for Sleep so your app can check to see if it needs to be unpaused. (Sleep will also cause your app to give up it's slice of CPU time so it doesn't bog down the system when it's paused.)

    Hope this helps!
Sign In or Register to comment.