Link to home
Start Free TrialLog in
Avatar of Moustro
Moustro

asked on

Interrupting a loop with a keyboard input

Hey guys hope you can help me on this, it's probably a lot easier than I'm making it.

I've been using QBasic for a while and now that I moved up to VB6, I miss the INKEY$ function.
Is there a way to recreate the following code snippet in VB6? When I try it, I just get endless loops.

ReactionTime& = 0                                    
response$ = INKEY$

DO UNTIL response$ = CHR$(121) OR response$ = CHR$(110)
     response$ = INKEY$
     ReactionTime& = ReactionTime& + 1
LOOP

Appreciate the help guys,

Moustro
Avatar of DocM
DocM


Dim response

Private Sub Form_KeyPress(KeyAscii As Integer)
 response = KeyAscii
End Sub

Private Sub Form_Load()
 Form1.KeyPreview = True
 Me.Show
 Call Routine
End Sub

Private Sub Routine()
 ReactionTime& = 0
 Do Until response = 121 Or response = 110
    DoEvents
    ReactionTime& = ReactionTime& + 1
 Loop
 Stop
End Sub


Private UserKeyPressed as Boolean

Private Sub YourFunction()
     Dim ReactionTime as long

      UserKeyPressed = false
     ReactionTime = 0                                    

     DO While (Not UserKeyPressed)
          ReactionTime = ReactionTime + 1
     LOOP

End Sub

' Don't forget to enable KeyPreview event in the form to true
Private Sub Form_KeyPress(KeyAscii As Integer)
      UserKeyPressed = True
End Sub

' Good Luck!
Private response as Integer

Private Sub YourFunction()
     Dim ReactionTime as long

      UserKeyPressed = false
     ReactionTime = 0                                    

     DO (UNTIL response = CHR$(121) OR response = CHR$(110))
          ReactionTime = ReactionTime + 1
     LOOP

End Sub

' Don't forget to enable KeyPreview event in the form to true
Private Sub Form_KeyPress(KeyAscii As Integer)
      response = KeyAscii
End Sub

' Good Luck!
Dim loopFlag As Boolean

Private Sub Command1_Click()
   
    loopFlag = False
    While loopFlag = False
        DoEvents
    Wend
    MsgBox "end of loop"
   
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    loopFlag = True
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    loopFlag = True
End Sub

Private Sub Form_Load()
    loopFlag = False
    Form1.KeyPreview = True
End Sub
Dim loopFlag As Boolean

Private Sub Command1_Click()
   
    loopFlag = False
    While loopFlag = False
        DoEvents
    Wend
    MsgBox "end of loop"
   
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    loopFlag = True
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    loopFlag = True
End Sub

Private Sub Form_Load()
    loopFlag = False
    Form1.KeyPreview = True
End Sub

You can use this API function:

Private Declare Function GetAsyncKeyState Lib "user32" ( _
      ByVal vKey As Long) As Integer

This function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.
You can have a look here:

http://www.allapi.net/apilist/GetAsyncKeyState.shtml

and

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getasynckeystate.asp

for a reference document.

Hope this helps
M

Avatar of avya2k
Let's say u 've Form with one textbox and timer control
By writin following code u can get what you asked
[Ofcourse turn the code as u want to use]

Option Explicit
Dim i, x As Integer

Private Sub Text1_KeyPress(KeyAscii As Integer)
    i = 1
End Sub

Private Sub Timer1_Timer()
    If (i = 1) Then Timer1.Enabled = False
    x = x + 1
    Text1.Text = x
End Sub

Dont use text1_keydown or keyup events
Notice:
This thread was started at this location and the points were refunded so it could be moved here for more VB exposure:
https://www.experts-exchange.com/questions/20545773/Interrupting-a-loop-with-a-keyboard-input.html

If objects (the expert) decides to join into the question, we must take into consideration upon finalization that it was responded to first by objects.

Moustro,
Several experts have already given you feedback on this question.  It is time you extend them the courtesy of a resonse even if nothing more than "I'm checking what has been sent, will return tomorrow to give feedback"

SpideyMod
Community Support Moderator@Experts Exchange
for what it's worth :)

try something like:



set the keypreview property of your form to true.

private looping as boolean
private ReactionTime as integer

public sub Form_Load()
 looping = true
 ReactionTime = 0
 do while looping
    ReactionTime = ReactionTime + 1
    DoEvents
 loop

end sub


public Sub Form_KeyPress(keyAscii as integer)
 if keyAscii = 121 then '' q
 ElseIf keyAscii = 110 then '' q
     looping = false
end sub
Letz assume u have form with command1 and text1
then try following code

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim i As Long

Private Sub Command1_Click()
    i = 0
    Me.Show
    Text1.Text = 0
    While i = 0
        Text1.Text = CLng(Text1.Text) + 1
        'If (Text1.Text > 10000) Then Text1.Text = 0
        If (GetAsyncKeyState(vbKeyEscape) < 0) Then i = 1
    Wend
End Sub
Letz assume u have form with command1 and text1
then try following code

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim i As Long

Private Sub Command1_Click()
   i = 0
   Me.Show
   Text1.Text = 0
   While i = 0
       Text1.Text = CLng(Text1.Text) + 1
       'If (Text1.Text > 10000) Then Text1.Text = 0
       If (GetAsyncKeyState(vbKeyEscape) < 0) Then i = 1
   Wend
   msgbox "Loop exited"
End Sub


when you will press esc the loop will come out to popup msgbox
Moustro:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
Moustro, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
Avatar of YensidMod
YensidMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial