Scripts / [SOLVED] How to press a button only once

Author
Message
AlreadyTakenGuy
User Offline
Joined: 25th Mar 2021
Location:
Posted: 25th Mar 2021 00:48 Edited at: 25th Mar 2021 08:54
Hi, I have a problem. If I press the E key once, multiple key presses are actually captured.
For example, if I press E key once, the counter reads 5-6.
I would like to get only 1, not 5.
Thank you in advance.

The author of this post has marked a post as an answer.

Go to answer
PM
AlreadyTakenGuy
User Offline
Joined: 25th Mar 2021
Location:
Posted: 25th Mar 2021 08:58
Hi, I have a problem. If I press the E key once, multiple key presses are actually captured.
For example, if I press E key once, the counter reads 5-6.
I'd like to get only 1, not 5-6.
Thank you in advance

PM
smallg
Community Leader
16
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 25th Mar 2021 10:53
This post has been marked by the post author as the answer.
use a variable to check the state

lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AlreadyTakenGuy
User Offline
Joined: 25th Mar 2021
Location:
Posted: 25th Mar 2021 11:43
The code works perfectly, thanks for your help
PM
GubbyBlips
3
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 31st Mar 2021 18:43
In consideration of adding this to global.lua --------- smallg
and for most minimalist space/ lines occupation; basically your code can be shrunk a
little to make a universal (any key pressed one time) use. Which could also be capable
of targeting specific keys. But here's the 'any key' version;

if g_Scancode > 0 then count = count + 1
if count == 1 then g_AnyOneKeyPress = true else g_AnyOneKeyPress = false end
else count = 0 end

--- used in action;
Text(11,11,1, "Press any key to add Fries with your Burger!")
if g_AnyOneKeyPress == true then fries = fries + 1 end
Text(11,14,1,"Fries with your Burger; "..fries)
PM
smallg
Community Leader
16
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 31st Mar 2021 23:55 Edited at: 31st Mar 2021 23:58
here you go, a function that will let you check if any alphanumeric key was pressed - i even added an option to allow you to specify if it's case sensitive or not
(i used the g_InKey as it's more user friendly than trying to memorise / workout which scan code you need so the actual end use function is way nicer but scancode will allow you to target the entire keyboard so it's probably best to have a function for both)


example usages
(checks for specifically the "R" key being pressed)


(checks for the "R" or "r" key(s) being pressed)


(checks for any alphanumeric key being pressed)
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
5
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 1st Apr 2021 09:27
As long as you have a single script checking for a key press that will work fine, but if you have two or more only the first will trigger which might get confusing.

For example if you had one script which waits for E to be pressed and plays a sound and another that puts up a sprite when E is pressed then they both need independent memo-ised states.
Been there, done that, got all the T-Shirts!
PM
smallg
Community Leader
16
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 1st Apr 2021 10:40
Quote: "As long as you have a single script checking for a key press that will work fine, but if you have two or more only the first will trigger which might get confusing.

For example if you had one script which waits for E to be pressed and plays a sound and another that puts up a sprite when E is pressed then they both need independent memo-ised states."

true, fixed version added to my thread
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11

Login to post a reply

Server time is: 2022-01-25 05:39:13
Your offset time is: 2022-01-25 05:39:13