Convert to ASCII key code?

Hi,
Is is possible to determine the ASCII key character code (OS X) of a letter which is in a textfield?

Example:
If I enter the letter “B” into TextField1 - is it possible to have TextField 2 display the corresponding ASCII key character for that letter?

Any pointers where to look in the language reference would be much appreciated.

Thank you all in advance.

Try Asc()

:slight_smile:
Thanks - I will look in the LR for Asc.

http://documentation.xojo.com/index.php/Asc

That was easy, (when you know where to look) :slight_smile:

Just one question - are the key codes different on Windows, than on OS X?

Nope.

Coolio - Thanks matey!

No problem.

Richard this should also help you as it has helped me when dealing with ASCII Codes.

HTH
http://www.asciitable.com/

Thankooo.

However, Asc is a bit misleading because it will return the code point of the character within its encoding. If that encoding is Unicode (UTF-8, the Xojo default, UTF-16, or UTF-32), the value returned by Asc could be well over 255.

If you are looking for the true ASCII value, convert its encoding first.

code = TextField1.Text.ConvertEncoding( Encodings.ASCII ).Asc

[quote=101547:@Kem Tekinay]However, Asc is a bit misleading because it will return the code point of the character within its encoding. If that encoding is Unicode (UTF-8, the Xojo default, UTF-16, or UTF-32), the value returned by Asc could be well over 255.

If you are looking for the true ASCII value, convert its encoding first.

code = TextField1.Text.ConvertEncoding( Encodings.ASCII ).Asc [/quote]
Great tip and thank you Kem!

Thanks Kem.

Ok,
I am obviously missing something here.

I have created a property in a module called Ascii As Integer.
I also have 2 textfields - one called userfield (where I can depress a key), and another called AsciiField (which displays the corresponding Ascii key code).

The code in the keydown event of the user field, is as follows:

Ascii = Asc(me.text) AsciiField.text = Str(Ascii)

I then also tried Kem’s approach:

Ascii = me.text.ConvertEncoding( Encodings.ASCII ).Asc AsciiField.text = Str(Ascii)

When I enter a letter or number (in the user field), the Asciifield displays 0.
When I press the delete / backspace key (in the user field), the Asciifield displays 100, and then either increases or decreases in value, each time I repeat the process?

Any ideas as to what I am doing wrong?

Thanks.

I’m not able to test at the moment, but maybe the KeyDown fires before the .Text is changed. What if you use the Key from KeyDown(Key As String) or try KeyUp event?

Thomas - the keyup event also failed - the Asciifield value now always stays at 0?

I didn’t quite understand your other option??

[quote=101547:@Kem Tekinay]However, Asc is a bit misleading because it will return the code point of the character within its encoding. If that encoding is Unicode (UTF-8, the Xojo default, UTF-16, or UTF-32), the value returned by Asc could be well over 255.

If you are looking for the true ASCII value, convert its encoding first.

code = TextField1.Text.ConvertEncoding( Encodings.ASCII ).Asc [/quote]

To add to the confusion, and to reply to a question above, Mac and Windows do share the same ASCII key codes as long as they are part of the plain ASCII. But some symbols can be placed at different ASCII values in Mac and Windows, such as the Euro symbol. It is usually not of a big concern for regular applications, but monetary symbols can be critical for some.

AsciiField.Text = Str(Key.Asc)

[quote=101667:@Richard Summers]Ok,

Ascii = me.text.ConvertEncoding( Encodings.ASCII ).Asc AsciiField.text = Str(Ascii)[/quote]

To go back from Ascii to a string you should use the Chr() method, e.g.

AsciiField.text = Str(Ascii)

Str converts a number to a string (e.g. “20” = Str(20))… but Chr converts a Ascii value into its string character representation.

Thomas - your last solution works perfectly! :slight_smile:
However, if I press alt and the number 2 key - it gives 8364??? I never knew Ascii even went up that far?

Michel I only need the standard Ascii codes (at the moment) :slight_smile:

Alwyn, are you saying that I should adapt my code as follows:

Ascii = me.text.ConvertEncoding( Encodings.ASCII ).Asc AsciiField.text = Chr(Ascii)

Thanks.