?
Solved

Interrupting a loop with a keyboard input

Posted on 2003-03-10
14
Medium Priority
?
649 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:Moustro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 3

Expert Comment

by:DocM
ID: 8108627

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


0
 
LVL 11

Expert Comment

by:supunr
ID: 8108637
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!
0
 
LVL 11

Expert Comment

by:supunr
ID: 8108649
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:SantoshShitole
ID: 8108652
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
0
 
LVL 6

Expert Comment

by:marconovaro
ID: 8109324

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

0
 
LVL 5

Expert Comment

by:avya2k
ID: 8109798
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

0
 
LVL 5

Expert Comment

by:avya2k
ID: 8109803
Dont use text1_keydown or keyup events
0
 

Expert Comment

by:SpideyMod
ID: 8123726
Notice:
This thread was started at this location and the points were refunded so it could be moved here for more VB exposure:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20545773.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
0
 
LVL 92

Expert Comment

by:objects
ID: 8123904
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
0
 
LVL 5

Expert Comment

by:avya2k
ID: 8125124
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
0
 
LVL 5

Expert Comment

by:avya2k
ID: 8125129
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
0
 

Expert Comment

by:CleanupPing
ID: 8900350
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?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9074766
Moustro, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9166054
Question is PAQ'd and no points refunded.

YensidMod
Community Support Moderator @Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question