Detect when a key is held down

Place to get help with not working mods / modding interface.
Post Reply
JasonCurnow
Inserter
Inserter
Posts: 22
Joined: Wed Jan 03, 2018 4:44 am
Contact:

Detect when a key is held down

Post 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

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Detect when a key is held down

Post 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.

JasonCurnow
Inserter
Inserter
Posts: 22
Joined: Wed Jan 03, 2018 4:44 am
Contact:

Re: Detect when a key is held down

Post 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?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Detect when a key is held down

Post by DaveMcW »

Yes, character.walking_state is writable, so you can automate running by updating it every tick.

User avatar
jamiechi1
Fast Inserter
Fast Inserter
Posts: 196
Joined: Wed Jan 03, 2018 10:12 pm

Re: Detect when a key is held down

Post 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.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Detect when a key is held down

Post 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}
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”