Capture text during gameplay (for cheats)

Godot Version

4.2.1

Question

Context: If you know games like Doom or Duke Nukem 3D (for their older versions, from the Windows 98 era), there were cheats that were activated if during the game you typed certain words (regardless of whether they were in upper or lower case), for example :
Doom: IDDQD (God mode), IDKFA (All ammo and keys), IDCLEV## (Level select, replace ## with number of level).
Duke Nukem 3D: dnkroz (God mode), dnclip (Walk through SOME walls), dnweapons (all weapons and ammo).
My idea is that there is a text (or key) logger, and if it detects a specific string, a special script is activated.
For now, what I don’t know is how to program a key or letter logger.

just create a singleton that capture keystrokes, apend to a string, and iterate over that string on process. remember to convert all text to lowerCASE

create a if list of:

if “cheat1” in string:
dothat1()
string = “”

if “cheat2” in string:
dothat2()
string = “”

this should be

This could work:

extends Node

const CHEATS = ["IDDQD", "DNKROZ"]
const TIME_BETWEEN_KEYS = 1.0

var cheat = ""
var delay_counter = 0.0

func _unhandled_input(event):
	if event is InputEventKey and event.pressed:
		cheat += event.as_text_keycode()
		delay_counter = TIME_BETWEEN_KEYS
		if cheat in CHEATS:
			print("Cheat detected: " + cheat)

func _process(delta):
	delay_counter -= delta
	if delay_counter <= 0.0:
		cheat = ""
2 Likes

I should have been more explicit, I use C#, but I don’t mind being answered with GDScript.
Here’s my code (I haven’t included the delayed_counter yet):

public static string _keyLogged ="aeiou";

 public override void _UnhandledInput(InputEvent @event)
{
     if (@event is InputEventKey eventKey)
     {
         if (eventKey.Pressed)
         {
             GD.Print(eventKey.ToString());
             GD.Print(eventKey.AsText());
             GD.Print(eventKey.AsTextKeycode());
             GD.Print(eventKey.AsTextPhysicalKeycode());
             GD.Print(_keyLogged);
             _keyLogged = new System.Text.StringBuilder(_keyLogged).Remove(0, 1).Append(eventKey.AsText()[0]).ToString().ToUpper();
             GD.Print(_keyLogged);
         }
     }
 
 }

Thanks for the advice, however I have found a new problem: the keys that are registered are… all of them.
I would like to know if there is any mention of restricting the registered keys to alphanumeric characters (see attached image).
There are letters that not everyone has, such as Ñ or Ç, those letters I would like to know if they can be excluded or accepted.
On the other hand, the number keys on the right side of the keyboard, the “Num Lock”, I would also like to know if they can be restricted or enabled.