Key
In my code im trying to get it to take only 1 input then it wont take another input until the key press ahs been released
58 Replies
When space is pressed it will take 1 input then immediatly another tehn doesnt take other inputs.
You don't set
keypressed
herethats to avoid key taking in more hten 1 input
if i removed that it would take in all inputs
also i want to try get it down to the point i can remove the Thread.Sleep(50);
Console.ReadKey is a blocking call, you don't need sleeps if you plan your while loops according to that.
if i dont have the sleep then the loop will constantly go through taking every input
Taking every input? Console.ReadKey returns 1 key
yes
but it instantly repeats the loop so console.readkey is constantly taking in a new input
because your exit condition is not met
what exit condition ?
theres multiple ifs and whiles
main exit condition is the
finished
bool. You can exit the loops too via break;
finished is there to allow the game to end when teh user presses escape
Then what prevents you from just doing something like this?
would that just constantly run as true next gets changed to false ?
while (true)
runs until the
return
keyword is triggered in the Methodbut if i use the return in another procedure it wouldnt have an effect
say i were in shop procedure and press escape as its a variable i can set it to false then it insta ends game
doesnt make a difference tho
shop
is a method. You would need to check for input inside the shop method for an escape prompt.
You can use a method for that too then.
eg.
what i have works
the issue is happening within the while loop
more specificly the if statement
^thats all the code
can u use $paste instead
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
i can try
$paste https://paste.mod.gg/
BlazeBin
A tool for sharing your source code with the world!
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
how do i use it ?
paste ur code there and save it
BlazeBin - ygzzctyynomz
A tool for sharing your source code with the world!
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
that ?
yea but no need to write $ paste
ah k
whats ur issue again?
when the code runs and we go to the the procedure game(); when psace is pressed and held it adds 1 to coins the instantly goes through again then wont add again
i want it to add once then it wont add until the player has released the space bar and presses it again
ah i ran ur code i see what u mean
everything iv tried has either got it to register all inputs or no inputs
did u try debugging?
$debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
im not a fan of how the console handles keyboard input
ye i have tried
i dont think the console can detect if the key got released
not directly
it can check if a key is available or not
if its available it means its being pressed
if its not then its not getting pressed
well the issue here is
since how windows handles the input
if ur holding space it thinks ur constantly pressing space very fast
when u hold down a button in notepad or discord for example
it writes the first letter, then waits a few milliseconds, then constantly writes the letter
yeah but when you execute and hold the space it will add first twice then wont ever add again no matter how logn you hold it
probably because of thread.sleep
it wrote too fast the console had no time to check if a key isnt pressed
if you removed thread.sleep
it will constantly add a coin
if the time is reduce it will take an input while still ehld down
its only at 50 where it wont take another input
i dont know what u mean, that doesnt happen to me
change the time to 40 then press and hold
change it to 1 press and hold
keep at 50, press and hold
in thread.Sleep?
all are dif outcomes
ye
still doesnt happen to me
it depends on your keyboard settings probably
you see this
that controls how fast it repeatedly writes the letter ur pressing
mines the same as whats in the images
well mine is not that fast
and im not on windows
i dont actually need a fix but id prefer to have 1
the current state is suitable
ill try further but if i dont succeed its fine