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

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

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

Post 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 ".
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

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

Post 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.
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

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

Post 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.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

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

Post 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?
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

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

Post 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...
plouf
Posts: 666
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

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

Post 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 :-)
Christos
Post Reply