How to wait for key and know what key it was?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1261
Joined: Sun Feb 14, 2010 7:11 pm

How to wait for key and know what key it was?

Post by Bugala »

What I am trying to achieve is to let player choose keys for actions.

However, since I didn't take this into consideration already earlier, I am now looking for a likely hackish solution to avoid having to rewrite code.

Basically I have come up with two possible solutions.

Simplest would be following:

Code: Select all

TextOut(#CENTER, #CENTER, "Press new key for UP")
WaitKeyDown("any")

UP = WaitKeyDownsResult


However, I don't see a way to know what key user pressed, hence the "WaitKeyDownsResult" doesn't exist.


Second solution would be:

Code: Select all

Copy all events to X
Clear All Events
Make Event that checks for key being pressed
WaitEvent()
Handle setting the new key for UP
Events = X
But is this doable either?

And to clarify a bit.

Reason why I cant just set event handler to wait for keyboard being pressed and then use Waitevent is that there are many other Events happening at same time, which I would first need to turn off to avoid situations where user could do something else than press the keyboard next, and that result in unexpected behavior.
Flinx
Posts: 261
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: How to wait for key and know what key it was?

Post by Flinx »

I think that even your second solution is doable, but I a have a hack for the first one, unfortunately with polling. Though a return value for WaitKeyDown would be handy.

Code: Select all

keytable={"UP","DOWN","RIGHT","LEFT","HELP","DEL","BACKSPACE","TAB","RETURN","ENTER","ESC","SPACE",
"F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","F13","F14","F15","F16",
"INSERT","HOME","END","PAGEUP","PAGEDOWN","PRINT","PAUSE",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"0","1","2","3","4","5","6","7","8","9",
"LSHIFT","RSHIFT","LALT","RALT","LCOMMAND","RCOMMAND","LCONTROL","RCONTROL"}
TextOut(#CENTER, #CENTER, "Press new key for UP")
WaitKeyDownsResult=Nil
While IsNil(WaitKeyDownsResult)
	For Local i=0 To TableItems(keytable)-1
		If IsKeyDown(keytable[i])
			WaitKeyDownsResult=keytable[i]
			Break
		EndIf
	Next
	Wait(10,#MILLISECONDS)
Wend
DebugPrint(WaitKeyDownsResult)
UP = WaitKeyDownsResult
User avatar
jPV
Posts: 675
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: How to wait for key and know what key it was?

Post by jPV »

As a feature request, it indeed would be nice to have something like:

Code: Select all

key$ = WaitKeyDown("any")
Bugala
Posts: 1261
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to wait for key and know what key it was?

Post by Bugala »

Thanks, was thinking that solution as one of possibilities, but decided not to, both because its lot of work to type all the options, and also, because it might still leave something out. However, now that you already did that typing part, I might use that if there is no other option to do this, without rewriting the existing code.
Bugala
Posts: 1261
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to wait for key and know what key it was?

Post by Bugala »

jPV wrote: Tue Sep 24, 2024 7:14 am As a feature request, it indeed would be nice to have something like:

Code: Select all

key$ = WaitKeyDown("any")
Was thinking myself too that if there isn't anything similar already existing, then would have made this as feature request, since it would be handy and had been a simple and obvious (was the first thing I tried, but noticed it didn't return anything) solution to this current problem.
Flinx
Posts: 261
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: How to wait for key and know what key it was?

Post by Flinx »

Bugala wrote: Tue Sep 24, 2024 7:34 am now that you already did that typing part
:) This was more a copy and paste from the manual and some replace all, not that much work. I was interested to see if I could get it to work at all.
BTW: If we have a feature request already, I think an additional option for "down" or "up" could be helpful, because at the moment WaitKeyDown() is mainly a "WaitKeyUp".
User avatar
airsoftsoftwair
Posts: 5626
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How to wait for key and know what key it was?

Post by airsoftsoftwair »

jPV wrote: Tue Sep 24, 2024 7:14 am As a feature request, it indeed would be nice to have something like:

Code: Select all

key$ = WaitKeyDown("any")
Um, don't we have that already or am I missing something here? The manpage of WaitKeyDown() explicitly mentions the "ANY" special key since Hollywood 6.1 already.
Flinx
Posts: 261
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: How to wait for key and know what key it was?

Post by Flinx »

airsoftsoftwair wrote: Sat Sep 28, 2024 10:38 pm don't we have that already or am I missing something here?
They want a return value.
Bugala
Posts: 1261
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to wait for key and know what key it was?

Post by Bugala »

Like flinx said, point was that we were in need of the RETURN value of the WaitKeyDown(). As in, which key user pressed to get past the WaitKeyDown().

This way this key can be used to set for example a hotkey.

Like:

Code: Select all

TextOut(#CENTER, #CENTER, "give me a key for UP")
Result = WaitKeyDown("any")
KeyUP = Result
User avatar
airsoftsoftwair
Posts: 5626
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How to wait for key and know what key it was?

Post by airsoftsoftwair »

Bugala wrote: Sun Sep 29, 2024 8:40 am Like flinx said, point was that we were in need of the RETURN value of the WaitKeyDown(). As in, which key user pressed to get past the WaitKeyDown().
Ah ok, I can understand that this could be useful. I'll see what I can do.
Post Reply