find_scancode_from_string doesn't give back the scancode for punctuation characters

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tayete

Hi all:
I am trying to get the scancode of the characters that are entered in a LineEdit. I am using find_scancode_from_string to print the code of the letters or puncutation symbols (“,” “.”) entered.

For simplicity, I have created a scene with just a LineEdit and a Label which changes its text with the text entered via a signal from the LineEdit, while the console shows its scancode.

In the following code you may check how if you write an “a” in the LineEdit, it works perfectly showing a “a” and prints its code “65” , but if you write a “,” it shows that “,” but code “0”. Same with “(”, “.” and on… Any ideas?

extends Node2D


func _ready():
	pass # Replace with function body.


func _on_LineEdit_text_entered(new_text):
	$"Label".set_text(new_text)
	print(new_text)
	var scan = OS.find_scancode_from_string(new_text)
	print(scan)

:bust_in_silhouette: Reply From: jgodfrey

Are you really looking for a scancodeor do you instead want the ASCII value for the character in question? It looks like the find_scancode_from_string function you’re using would require the string Comma (for example) to return the expected value of 44.

If you just want the ASCII value for a given character, maybe you want the ord function instead. Here’s an example:

print(ord("A")) # 65
print(ord(",")) # 44

Though, do be aware that the ASCII value for lower-case and upper-case letters is different. For example, a will return 97 while A will return 65.

Thanks so much @jgodfrey, I didn’t know there was an opposite to char(). That made it!
Again, thanks a lot.

tayete | 2020-02-06 05:38