extends Node


# ##########
# map strings from keyboard layout to godot global defined keys
# https://docs.godotengine.org/en/stable/classes/class_@globalscope.html
# Keys that are not available in the global scope of godot are represented by their decimal ASCII code
# ADD NEW KEY:
# extract new decimal ASCII codes from here: https://www.ascii-code.com/ISO-8859-1
# create entry with new name and reuse this name in your keyboard layout .json
# ##########

var key_list = {
	"Escape":KEY_ESCAPE,
	"Tab":KEY_TAB,
	#"Shift-Tab":16777219,
	"Backspace":KEY_BACKSPACE,
	"Return":KEY_ENTER,
	"Enter":KEY_KP_ENTER,
	"Insert":KEY_INSERT,
	"Delete":KEY_DELETE,
	"Pause":KEY_PAUSE,
	"Printscreen":KEY_PRINT,
	"SystemRequest":KEY_SYSREQ,
	"Clear":KEY_CLEAR,
	"Home":KEY_HOME,
	"End":KEY_END,
	"LeftArrow":KEY_LEFT,
	"UpArrow":KEY_UP,
	"RightArrow":KEY_RIGHT,
	"DownArrow":KEY_DOWN,
	"Pageup":KEY_PAGEUP,
	"Pagedown":KEY_PAGEDOWN,
	"Shift":KEY_SHIFT,
	"Control":KEY_CTRL,
	"Meta":KEY_META,
	"Alt":KEY_ALT,
	"Capslock":KEY_CAPSLOCK,
	"Numlock":KEY_NUMLOCK,
	"Scrolllock":KEY_SCROLLLOCK,
	"F1":KEY_F1,
	"F2":KEY_F2,
	"F3":KEY_F3,
	"F4":KEY_F4,
	"F5":KEY_F5,
	"F6":KEY_F6,
	"F7":KEY_F7,
	"F8":KEY_F8,
	"F9":KEY_F9,
	"F10":KEY_F10,
	"F11":KEY_F11,
	"F12":KEY_F12,
	"F13":KEY_F13,
	"F14":KEY_F14,
	"F15":KEY_F15,
	"F16":KEY_F16,
	"MultiplyonNumpad":KEY_KP_MULTIPLY,
	"DivideonNumpad":KEY_KP_DIVIDE,
	"SubtractonNumpad":KEY_KP_SUBTRACT,
	"PeriodonNumpad":KEY_KP_PERIOD,
	"AddonNumpad":KEY_KP_ADD,
	"Number0onNumpad":KEY_KP_0,
	"Number1onNumpad":KEY_KP_1,
	"Number2onNumpad":KEY_KP_2,
	"Number3onNumpad":KEY_KP_3,
	"Number4onNumpad":KEY_KP_4,
	"Number5onNumpad":KEY_KP_5,
	"Number6onNumpad":KEY_KP_6,
	"Number7onNumpad":KEY_KP_7,
	"Number8onNumpad":KEY_KP_8,
	"Number9onNumpad":KEY_KP_9,
	#"LeftSuper(Windows)":16777260,
	#"RightSuper(Windows)":16777261,
	"Contextmenu":KEY_MENU,
	"LeftHyper":KEY_HYPER,
	"RightHyper":KEY_HYPER,
	"Help":KEY_HELP,
	#"LeftDirection":16777266,
	#"RightDirection":16777267,
	"Back":KEY_BACK,
	"Forward":KEY_FORWARD,
	"Stop":KEY_STOP,
	"Refresh":KEY_REFRESH,
	"Volumedown":KEY_VOLUMEDOWN,
	"Mutevolume":KEY_VOLUMEMUTE,
	"Volumeup":KEY_VOLUMEUP,
	#"BassBoost":16777287,
	#"BassUp":16777288,
	#"BassDown":16777289,
	#"TrebleUp":16777290,
	#"TrebleDown":16777291,
	"Mediaplay":KEY_MEDIAPLAY,
	"Mediastop":KEY_MEDIASTOP,
	"Previoussong":KEY_MEDIAPREVIOUS,
	"Nextsong":KEY_MEDIANEXT,
	"Mediarecord":KEY_MEDIARECORD,
	"Homepage":KEY_HOMEPAGE,
	"Favorites":KEY_FAVORITES,
	"Search":KEY_SEARCH,
	"Standby":KEY_STANDBY,
	"OpenURL/LaunchBrowser":KEY_OPENURL,
	"LaunchMail":KEY_LAUNCHMAIL,
	"LaunchMedia":KEY_LAUNCHMEDIA,
	"LaunchShortcut0":KEY_LAUNCH0,
	"LaunchShortcut1":KEY_LAUNCH1,
	"LaunchShortcut2":KEY_LAUNCH2,
	"LaunchShortcut3":KEY_LAUNCH3,
	"LaunchShortcut4":KEY_LAUNCH4,
	"LaunchShortcut5":KEY_LAUNCH5,
	"LaunchShortcut6":KEY_LAUNCH6,
	"LaunchShortcut7":KEY_LAUNCH7,
	"LaunchShortcut8":KEY_LAUNCH8,
	"LaunchShortcut9":KEY_LAUNCH9,
	"LaunchShortcutA":KEY_LAUNCHA,
	"LaunchShortcutB":KEY_LAUNCHB,
	"LaunchShortcutC":KEY_LAUNCHC,
	"LaunchShortcutD":KEY_LAUNCHD,
	"LaunchShortcutE":KEY_LAUNCHE,
	"LaunchShortcutF":KEY_LAUNCHF,
	"Unknown":KEY_UNKNOWN,
	"Space":KEY_SPACE,
	"!":KEY_EXCLAM,
	"\"":KEY_QUOTEDBL,
	"#":KEY_NUMBERSIGN,
	"$":KEY_DOLLAR,
	"%":KEY_PERCENT,
	"&":KEY_AMPERSAND,
	"'":KEY_APOSTROPHE,
	"(":KEY_PARENLEFT,
	")":KEY_PARENRIGHT,
	"*":KEY_ASTERISK,
	"+":KEY_PLUS,
	",":KEY_COMMA,
	"-":KEY_MINUS,
	".":KEY_PERIOD,
	"/":KEY_SLASH,
	"0":KEY_0,
	"1":KEY_1,
	"2":KEY_2,
	"3":KEY_3,
	"4":KEY_4,
	"5":KEY_5,
	"6":KEY_6,
	"7":KEY_7,
	"8":KEY_8,
	"9":KEY_9,
	":":KEY_COLON,
	";":KEY_SEMICOLON,
	"<":KEY_LESS,
	"=":KEY_EQUAL,
	">":KEY_GREATER,
	"?":KEY_QUESTION,
	"@":KEY_AT,
	"A":KEY_A,
	"B":KEY_B,
	"C":KEY_C,
	"D":KEY_D,
	"E":KEY_E,
	"F":KEY_F,
	"G":KEY_G,
	"H":KEY_H,
	"I":KEY_I,
	"J":KEY_J,
	"K":KEY_K,
	"L":KEY_L,
	"M":KEY_M,
	"N":KEY_N,
	"O":KEY_O,
	"P":KEY_P,
	"Q":KEY_Q,
	"R":KEY_R,
	"S":KEY_S,
	"T":KEY_T,
	"U":KEY_U,
	"V":KEY_V,
	"W":KEY_W,
	"X":KEY_X,
	"Y":KEY_Y,
	"Z":KEY_Z,
	"[":KEY_BRACKETLEFT,
	"\\":KEY_BACKSLASH,
	"]":KEY_BRACKETRIGHT,
	"^":KEY_ASCIICIRCUM,
	"_":KEY_UNDERSCORE,
	"LeftQuote":KEY_QUOTELEFT,
	"{":KEY_BRACELEFT,
	"|":KEY_BAR,
	"}":KEY_BRACERIGHT,
	"~":KEY_ASCIITILDE,
	#"NoBreakSpace":160,
	#"ExcalmDown":161,
	#"¢":162,
	#"Sterling":163,
	#"Currency":164,
	"Yen":KEY_YEN,
	#"¦":166,
	"§":KEY_SECTION,
	"¨":168,
	"©":169,
	"Feminine":170,
	"«":171,
	"»":172,
	"‐":173,
	"®":174,
	"Macron":175,
	"°":176,
	"±":177,
	"²":178,
	"³":179,
	"´":180,
	"µ":181,
	"Paragraph":182,
	"·":183,
	"¬":184,
	"¹":185,
	"♂":186,
	"GuillemotRight":187,
	"¼":188,
	"½":189,
	"¾":190,
	"¿":191,
	"à":192,
	"á":193,
	"â":194,
	"ã":195,
	"ä":196,
	"å":197,
	"æ":198,
	"ç":199,
	"è":200,
	"é":201,
	"ê":202,
	"ë":203,
	"ì":204,
	"í":205,
	"î":206,
	"Idiaeresis":207,
	"ð":208,
	"ñ":209,
	"ò":210,
	"ó":211,
	"ô":212,
	"õ":213,
	"ö":214,
	"×":215,
	"ø":216,
	"ù":217,
	"ú":218,
	"û":219,
	"ü":220,
	"ý":221,
	"þ":222,
	"ß":223,
	"÷":247,
	"ÿ":255,
}

# ##########
# lowercase characters are represented by an offset of +=32 from the original "uppercase" code
# this list defines all lowercase-valid characters
# ##########

var key_array_lowercase = [
	"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",
	"à",
	"á",
	"â",
	"ã",
	"ä",
	"å",
	"æ",
	"ç",
	"è",
	"é",
	"ê",
	"ë",
	"ì",
	"í",
	"î",
	"ð",
	"ñ",
	"ò",
	"ó",
	"ô",
	"õ",
	"ö",
	"ø",
	"ù",
	"ú",
	"û",
	"ü",
	"ý",
	"ÿ"
]


func has_lowercase(name):
	for key in key_array_lowercase:
		if key == name:
			return true
	return false


func get_key_from_string(name, debug=false):
	if key_list.has(str(name)):
		if debug:
			print("Scancode OK:"+name)
		return key_list.get(str(name))
	else:
		if debug:
			print("Scancode Key not found:"+ name)
		return 0
