Page 1 of 1

Detect when a key is held down

Posted: Tue Feb 23, 2021 10:10 pm
by JasonCurnow
I have a simple quality-of-live mod (https://mods.factorio.com/mod/Blink) that teleports you in a certain direction with shift or control-WASD. I made it since I got down to ~15 UPS with my first megabase and running was.. Painful. I've had a very good suggestion to make this mod work when the key is held down - i.e shift-D held down makes you keep teleporting south until you release it. Looking for a keypress wasn't hard to figure out, but I can't find any way to detect when a key is held down to repeat the action. Anyone have any advice? Thanks!

- Jason

Re: Detect when a key is held down

Posted: Tue Feb 23, 2021 10:28 pm
by DaveMcW
There are no key down events. The best you can do is make one button to start teleporting in a direction, and one button to stop. Then do the teleporting in on_tick() depending on which was pressed last.

You could also hijack the player controls by checking character.walking_state every tick, which does update on key down.

Re: Detect when a key is held down

Posted: Tue Feb 23, 2021 10:38 pm
by JasonCurnow
Thank you very much for the rapid reply!

You know, that makes me think of something.. I've always wanted a way to "run and keep running" with one keypress. Before I did the Blink mod, running a distance required constantly holding the key down and you can't switch to another window while looking something up on a long run. Would it be possible to build an always-run function (i.e. alt+w to run north until any other button is pressed) with what you are describing?

Re: Detect when a key is held down

Posted: Tue Feb 23, 2021 11:12 pm
by DaveMcW
Yes, character.walking_state is writable, so you can automate running by updating it every tick.

Re: Detect when a key is held down

Posted: Mon Mar 01, 2021 11:19 pm
by jamiechi1
I found out recently that my programmable keypad (from allreli) can be programmed to do this.
I just created a macro that pressed and released a key and then deleted the keyup event in the recorded macro.
I suppose any programmable keyboard can do this. (Unfortunately the allreli keypad is no longer available.)
You might also be able to do this with autohotkey.

Re: Detect when a key is held down

Posted: Tue Mar 09, 2021 9:57 am
by eradicator
DaveMcW wrote:
Tue Feb 23, 2021 11:12 pm
Yes, character.walking_state is writable, so you can automate running by updating it every tick.
Years ago i tried on_tick'ing walking_state in multiplayer and concluded that (probably due to lag hiding) it produces erratic laggy walking.
jamiechi1 wrote:
Mon Mar 01, 2021 11:19 pm
You might also be able to do this with autohotkey.
Trivially! I.e. "press shift+w to hold down w until w is pressed again."

Code: Select all

+w::Send, {w down}