Author Topic: Force key release  (Read 863 times)

CJH

  • Newbie
  • *
  • Posts: 21
Force key release
« on: October 13, 2020, 08:57:48 PM »
I'm having an old man moment(s) tonight. If I were in the grocery store, my cart would be in the middle of the aisle blocking everybody.
Anyway, this is for RFactor2, specifically for the Formula E cars. The RFactor2 people haven't implemented an overtake button yet. So I thought I could just have a button that would increase the power level by one increment (220 to 225) when a button is pressed. No problem, set a button when pressed to go up and the same button when released to go back down. So, we're running at 200KW and press and hold the button and the car gets boosted to 225. Release the button and the car goes back down to 200. Works fine.
But now I want to have a maximum amount of time that the button can be held down--for example, no more than 5 seconds. So even if you held it down for more than that time, when it hit five seconds it would break out of the boost and reduce power back down. I don't want it to always be five seconds though because you want to be able to stop the boost any time early in case you hit the button late and were approaching a chicane or a slow part of the track where you don't want to waste the energy.
Is that possible?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Force key release
« Reply #1 on: October 13, 2020, 09:10:59 PM »
Yes, you can use a date/time variable to set a point five seconds in the future, then check whether that point is after the current time (I.E. it hasn't been passed yet); Something like
Code: [Select]
Press Up key and hold for 0,03 seconds and release
Write [Blue] 'Boost on' to log
Set date [~boostTimeout] to [~boostTimeout] plus [5] seconds
Start Loop While : (Keyboard Key 'B' Is Pressed AND [~boostTimeout] Is After Current Date/Time)
End Loop
Press Down key and hold for 0,03 seconds and release
Write [Blue] 'Boost off' to log

If you want a notification when it times out, you can add a condition, E.G.
Code: [Select]
Press Up key and hold for 0,03 seconds and release
Write [Blue] 'Boost on' to log
Set date [~boostTimeout] to [~boostTimeout] plus [5] seconds
Start Loop While : (Keyboard Key 'B' Is Pressed AND [~boostTimeout] Is After Current Date/Time)
End Loop
Begin Date Compare : [~boostTimeout] Is Before Current Date/Time
    Write [Orange] 'Boost timed out' to log
End Condition
Press Down key and hold for 0,03 seconds and release
Write [Blue] 'Boost off' to log

CJH

  • Newbie
  • *
  • Posts: 21
Re: Force key release
« Reply #2 on: October 14, 2020, 06:22:26 AM »
Thank you. That worked perfectly as long as I didn't use a numkey as the activation key. Now I want boost mode to have a 75 second wait between times when it can be used. I don't want to be able to repeatedly press the key and jump to boost mode every five seconds. That shouldn't be too hard for me to figure out...after all, I was able to copy your work with no problem at all.