Loop until keyboard press

Hi there,

Is there a way to stay in a loop until a key on the keyboard is pressed? Something like this...

Keypressed = false

while Keypressed = false do this.
  Do stuff here....
  Do stuff here....
  Do stuff here....
wend

Then somewhere else there is code which catches keypressed..
If keyboard space bar is pressed
Keypressed = true
This will stop the loop.

Thanks!

Parents
  • Try something like below. This is for any 64 bit version of corel draw. I use a secondary boolean for remembering if space was pressed so in longer procedures the user wont have to hold space down for an entire loop and can just check at particular stages.It's not necessary though you could have  'Do while IsSpacePressed = False' and forgetting storing its state in a secondary boolean.

    Option Explicit
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'Enable Sleep
    
    Private Declare PtrSafe Function GetAsyncKeyState Lib "User32" (ByVal vKey As LongPtr) As Integer
    Public Const VK_SPACE = &H20
    
    Private Function IsSpacePressed() As Boolean
        IsSpacePressed = (GetAsyncKeyState(VK_SPACE) < 0)
    End Function
    
    Sub Spacetimer()
      
    Dim I As Long: I = 1
    Dim Spaced As Boolean: Spaced = False
    ActivePage.Activate
    
    Do While Spaced = False
        Sleep (1000)
        If IsSpacePressed = True Then Exit Do 'use this is exit the loop before it ends
        I = I + 1
        If IsSpacePressed = True Then Spaced = True 'alternatively use this to leave after loop is complete
    Loop
    
    MsgBox "You waited " & I & " seconds before pressing space"
    End Sub
    
Reply Children