Author Topic: How to loop until key is held down for period of time?  (Read 1935 times)

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
How to loop until key is held down for period of time?
« on: July 19, 2018, 10:30:38 AM »
I'm trying to create a continuous loop that pauses command execution until the user presses and holds down a particular key. Here is one way to sort of achieve this:

Code: [Select]
// Check if loop should terminate
Start Loop While : [~~ExitLoop] Does Not Equal True
    // Brief pause
    Pause 0.25 seconds
    // Check if "Numpad -" is being pressed
    Begin Device State Check :  Keyboard Key 'Numeric -' Is Pressed
        // Brief pause
        Pause 0.25 seconds
        // Check if "Numpad -" is being pressed
        Begin Device State Check :  Keyboard Key 'Numeric -' Is Pressed
            // Set boolean flag to indicate loop should be terminated
            Set Boolean [~~ExitLoop] to True
        End Condition
    End Condition
End Loop

Anyone have any ideas for how to make this more realistically react to a key being held down?
« Last Edit: July 20, 2018, 08:22:01 AM by Exergist »

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: How to loop until key is held down for period of time?
« Reply #1 on: July 19, 2018, 04:26:57 PM »
Here's one way to do it:
Code: [Select]
Start Loop While : [1] Equals [1]
    Begin Device State Check :  Keyboard Key 'Numeric -' Is Pressed
        Begin Date Compare : [~pressStart] Has Not Been Set
            Set date [~pressStart] to [~pressStart] plus [500] milliseconds
        Else
            Begin Date Compare : [~pressStart] Is Less Than Current Date/Time
                Loop Break
            End Condition
        End Condition
    Else
        Set date [~pressStart] value to [Not Set]
    End Condition
End Loop

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: How to loop until key is held down for period of time?
« Reply #2 on: July 20, 2018, 08:21:41 AM »
I tried what you suggested and it looks like VA never enters the loop. So I revised it slightly (see the start loop condition) and this does work:

Code: [Select]
// Pause VoiceAttack execution until "Numpad -" is pressed and held down
// Start an infinite loop
Start Loop While : [0] Equals 0 (evaluate 'Not set' as zero)
    // Check if "Numpad -" is being pressed
    Begin Device State Check :  Keyboard Key 'Numeric -' Is Pressed
        // Check if ~~PressStart has NOT been set
        Begin Date Compare : [~~PressStart] Has Not Been Set
            // Add 500 ms to ~~PressStart
            Set date [~~PressStart] to [~~PressStart] plus [500] milliseconds
        Else
            // Check if ~~PressStart is less than current time
            Begin Date Compare : [~~PressStart] Is Less Than Current Date/Time
                // Break out of outer loop
                Loop Break
            End Condition
        End Condition
    Else
        // Clear value of ~~PressStart
        Set date [~~PressStart] value to [Not Set]
    End Condition
End Loop

Thanks again!
« Last Edit: July 20, 2018, 11:34:46 AM by Exergist »

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: How to loop until key is held down for period of time?
« Reply #3 on: July 20, 2018, 05:18:07 PM »
If the revision is to check "Evaluate 'Not Set' as false" on the loop, that's how it's supposed to be set up.

I should have mentioned that, as for some reason a variable doesn't equal itself if it's null(it's a boolean with both "Variable Name" and "Another Variable" set to the exact same thing).


You could just use the boolean variable to run and terminate the loop instead of the "Loop Break" action, if you prefer.