Page 1 of 1

InstallEventHandler OnKeyDown event.key special keys like TAB, and how to see them?

Posted: Tue Apr 29, 2025 8:55 pm
by Bugala
I am trying to read TAB as an option for InstallEventHandler:

Code: Select all

InstallEventHandler({
	
	  OnKeyDown = Function(event) 
		DebugPrint(event.key)
		If event.key = "TAB" Or event.key = "\t" THEN DebugPrint("Tab pressed")
		 EndFunction})
Two problems, I cant figure out what I should use to recognise TAB-key.

And second, several of they keys, when Debugprinted, will not show anything, but instead print empty line to Console.

As example, pressing RETURN results in empty line, and so does pressing TAB.

How can I print out some way to see what key was pressed in case of these special keys that normally print nothing, and therefore this way be able to use this info to use in IF condition as in " IF (specialkey pressed) THEN something ".

Re: InstallEventHandler OnKeyDown event.key special keys like TAB, and how to see them?

Posted: Thu May 01, 2025 3:54 pm
by Flinx
Maybe this helps:

Code: Select all

Function p_PrintHex(st$)
	Local Debugstr$=""
	For Local i=0 To StrLen(st$, #ENCODING_RAW)-1
		Debugstr$=Debugstr$ ..FormatStr("%.2X", ByteVal(MidStr(st$,i,1, #ENCODING_RAW),#BYTE)).." "
	Next
	DebugPrint(Debugstr$)
EndFunction

InstallEventHandler({
	
	  OnKeyDown = Function(event) 
		DebugPrintNR(event.key.."  ")
		p_PrintHex(event.key)
	 EndFunction})

Repeat
	WaitEvent()
Forever
But if you need something more technical with more control, you could use OnRawKeyDown.

Re: InstallEventHandler OnKeyDown event.key special keys like TAB, and how to see them?

Posted: Sun May 04, 2025 5:12 pm
by airsoftsoftwair
Actually, TAB comes in as 8 spaces. This is a little strange but it has been like that since forever so it can't be changed. So to check for TAB you can check for:

Code: Select all

InstallEventHandler({OnKeyDown = Function(event) 
		If event.key = "        " THEN DebugPrint("Tab pressed")
EndFunction})
To check for RETURN or ENTER you need to check against "\n" (ASCII code 10). The reason why you see newlines when using DebugPrint() on these keys is that they all consist of whitespace only which is why the only thing you'll see with DebugPrint() is a linebreak.

As an alternative, you could also try to use "OnRawKeyDown". This will return more readable key names like "TAB" or "ENTER" etc.

Re: InstallEventHandler OnKeyDown event.key special keys like TAB, and how to see them?

Posted: Sun May 04, 2025 9:28 pm
by Bugala
Thanks. This helped.

A question regarding "OnRawKeyDown"

I suppose if I use if OnRawKeyDown(Xkey) as a first thing, as in:

Code: Select all

InstallEventHandler({OnKeyDown = Function(event) 
if OnRawKeyDown(somekey) = true then ...
rest of the lines
EndFunction})
Then there is no normal case where it would fail?

Re: InstallEventHandler OnKeyDown event.key special keys like TAB, and how to see them?

Posted: Mon May 05, 2025 11:03 am
by jPV
Bugala wrote: Sun May 04, 2025 9:28 pm Thanks. This helped.

A question regarding "OnRawKeyDown"

I suppose if I use if OnRawKeyDown(Xkey) as a first thing, as in:

Code: Select all

InstallEventHandler({OnKeyDown = Function(event) 
if OnRawKeyDown(somekey) = true then ...
rest of the lines
EndFunction})
Then there is no normal case where it would fail?
There isn't an OnRawKeyDown() function. You just do InstallEventHandler({OnRawKeyDown = Function...

Re: InstallEventHandler OnKeyDown event.key special keys like TAB, and how to see them?

Posted: Mon May 05, 2025 11:45 am
by plouf
airsoftsoftwair wrote: Sun May 04, 2025 5:12 pm Actually, TAB comes in as 8 spaces. This is a little strange but it has been like that since forever so it can't be changed. So to check for TAB you can check for:

Code: Select all

InstallEventHandler({OnKeyDown = Function(event) 
		If event.key = "        " THEN DebugPrint("Tab pressed")
EndFunction})
To check for RETURN or ENTER you need to check against "\n" (ASCII code 10). The reason why you see newlines when using DebugPrint() on these keys is that they all consist of whitespace only which is why the only thing you'll see with DebugPrint() is a linebreak.

As an alternative, you could also try to use "OnRawKeyDown". This will return more readable key names like "TAB" or "ENTER" etc.
Considering answers i believe noone has even use it with 8 spaces
So changing to TAB to be inline with rest. Isnt going to break anythink imho :-)