Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Interrupting a loop with a keyboard input

Posted on 2003-03-10
14
Medium Priority
?
658 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
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
Industry Leaders: 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

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!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 Month11 days, 23 hours left to enroll

564 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