Author Topic: Only execute a command after holding a key/button down for a set time.  (Read 3879 times)

pgleesonuk

  • Guest
Is this possible.

I would like to program a 2 stage command.
One which activates as soon as a key/button is pressed
The second only when the key/button has been held down for more than 1 second.

Any ideas how to do this?

Thanks

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4790
  • RTFM
Re: Only execute a command after holding a key/button down for a set time.
« Reply #1 on: August 08, 2017, 12:53:35 PM »
Code: [Select]
Set decimal [~CommandStart] value to the converted value of {DATETICKS}
Write '[Blue] {TIMESECOND}:{TIMEMILLISECOND}' to log
Start Loop While : [{STATE_KEYSTATE:NUM+}] Equals '1'
    Begin Text Compare : [{EXP: {DATETICKS} > ({DEC:~CommandStart} + 10000000)}] Equals '1'
        Press B key and hold for 0,05 seconds and release
        Write '[Blue] {TIMESECOND}:{TIMEMILLISECOND}' to log
        Exit Command
    End Condition
End Loop
Press A key and hold for 0,05 seconds and release
If you press Numpad + less than 1 second, the command will press "a". If you hold Numpad + longer than one second, it will press "b".
Replace the key identifier in the "{STATE_KEYSTATE:}" token with the one you're using to trigger the command(a list of special keys can be found under the "Key State Token Parameter Values" section of the manual).

10000000 ticks should equate to one second, but because of processing overhead the total wait time is a few tens of milliseconds longer. The command will write the command start and execution seconds to the log, so you can tweak it if you like.

pgleesonuk

  • Guest
Re: Only execute a command after holding a key/button down for a set time.
« Reply #2 on: August 08, 2017, 01:00:39 PM »
Thanks.

I figured it out but using 3 standard button presses and an IF statement.

All on the same button press
1st sends the first key stroke and sets button held to TRUE
2nd set the button held to be FALSE on release
3rd delays 1 sec then checks the state of the button held. if TRUE send the second key


Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: Only execute a command after holding a key/button down for a set time.
« Reply #3 on: August 08, 2017, 01:04:33 PM »
Thanks for helping, Pfeil. Not anywhere near a pc, but could it just be:

Press A
Pause 1 second
Check key state...  if key down press B?

I need to come up with something better than having to work with ticks o_O

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4790
  • RTFM
Re: Only execute a command after holding a key/button down for a set time.
« Reply #4 on: August 08, 2017, 01:11:16 PM »
Ah. I misinterpreted the requirement. I was under the impression you wanted one key to do one of two tasks, rather than having one key do either one or two tasks.

My original example can easily be modified for this:
Code: [Select]
Press A key and hold for 0,05 seconds and release
Set decimal [~CommandStart] value to the converted value of {DATETICKS}
Write '[Blue] {TIMESECOND}:{TIMEMILLISECOND}' to log
Start Loop While : [{STATE_KEYSTATE:NUM+}] Equals '1'
    Begin Text Compare : [{EXP: {DATETICKS} > ({DEC:~CommandStart} + 10000000)}] Equals '1'
        Press B key and hold for 0,05 seconds and release
        Write '[Blue] {TIMESECOND}:{TIMEMILLISECOND}' to log
        Exit Command
    End Condition
End Loop
The command will terminate as soon as the key is released, rather than waiting ~1000ms. Whether that's necessary/worth the overhead, I'll let you be the judge.



And Gary's version in VoiceAttack actions:
Code: [Select]
Press A key and hold for 0,05 seconds and release
Pause 1 second
Begin Text Compare : [{STATE_KEYSTATE:NUM+}] Equals '1'
    Press B key and hold for 0,05 seconds and release
End Condition
« Last Edit: August 08, 2017, 01:21:43 PM by Pfeil »

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: Only execute a command after holding a key/button down for a set time.
« Reply #5 on: August 14, 2017, 12:27:25 AM »
Here is a max time loop without having to use the date ticks trick.  Note that it's a command-scoped variable that will always initialize as, 'Not Set', so if the option, 'evaluate Not Set' as current date/time' the value will always be the current date/time + whatever you are adding to it:

Code: [Select]
Set date [~Start] to [~Start] plus [5] seconds    <---'Evaluate, 'Not Set' as current date/time' is checked
Start Loop While : [~Start] Is Greater Than Current Date/Time
    //Do Stuff Here
End Loop

This loops for five seconds (plus whatever time is taken in the loop).  I know it looks a little backwards, but it's about as small as it can be.  Hope that can be helpful.

sutex

  • Jr. Member
  • **
  • Posts: 91
Re: Only execute a command after holding a key/button down for a set time.
« Reply #6 on: August 26, 2017, 04:49:30 PM »
I use one command Say 'Jump' for all FSD tasks.
So I need to keep track of hardpoints states for both Joystick triggers, below is for Joystick Trigger 2

((HardpointsJoy2)) Joystick trigger 2 fires the 2nd slot Discovery Scanner on the 1st fire-group
Code: [Select]
Begin Small Integer Compare : [supercruise] Equals 1
    Set small int (condition) [HardpointsJoy2] value to 0
Else
    Set small int (condition) [HardpointsJoy2] value to 1
End Condition
Wondering if the topic can help improve ((HardpointsJoy2))
conditions set on length of joystick Trigger press held ? , and/or,  a quick press won't change the condition ?

« Last Edit: August 28, 2017, 04:47:32 PM by sutex »