Convert to ASCII key code?

Without knowing what alt-2 gives you on your keyboard, 8364 is probably the Unicode code point of that character, as I described above.

Ok,
let’s start again :slight_smile:

The code below works fine, until I depress the alt key and any other key - then I get extremely high Ascii key code values in return:

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

The code below, freezes the userfield, preventing me from entering keys, thus returning no Ascii code at all.

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

Kem, Alt-2 is the Euro symbol (I presume)
My number 2 key has the @ symbol (shift-2), and then the Euro symbol to the right (which I presume would be Alt-2).

Also, my code segments, are converting it to Ascii (as you kindly recommended), so surely that should prevent the unicode point from being displayed??

Hmmm, not sure why the Chr(Ascii) is freezing the interface (shouldn’t be doing that)?

sad face

I’m not sure how you need to adapt your code, just wanted to point out that for basic ASCII characters

Str(Asc(CHAR)) <> CHAR
and
Chr(Asc(CHAR)) = CHAR

  if Str(Asc("a")) = "a" then
    MsgBox "This message won't display"
  end if
  
  if Chr(Asc("a")) = "a" then
    MsgBox "This message will display"
  end if

…if it makes any sense what I’m trying to say.

Alwyn, no - I didn’t understand that :slight_smile:

Does anyone have any working code, which will display the Ascii key code when a key is depressed?
At least that way, I can see what is happening and learn from it.

The second code block is mostly right, but you should be using Str, not Chr. Str will take a number and convert that number to a string. Chr will take a code point and return the Unicode character it represents.

Where did you put this code?

Kem,
Here is what I originally had.

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 = me.text.ConvertEncoding( Encodings.ASCII ).Asc AsciiField.text = Str(Ascii)

However, when I enter a letter into the userfield - the Asciifield displays 0.
When I press backspace / delete - the Asciifield shows 100.

When I retype another letter into user field and press backspace again - the AsciiField increments??

I am happy to have this work any way possible - so long as a textfield can display the Ascii code of the key being depressed.
It would be even better, if the user could just press any key when the window opens, and have the corresponding Ascii code display in a textfield, (without the user having to type into a textfield).

I think I may have got Ascii and Unicode mixed up?

When I press the enter key - I need the value of 13 to appear.
When I press the tab key - I need the value of 9 to appear.
When I press the “a” key - I need the value of 97 to appear.

I believe these are unicode values, and not Ascii??
Or am I wrong?

In Xojo - we can use the code for example:

// CHECK FOR THE a KEY BEING PRESSED If Key = Chr(97) Then skipKey = False

This is Unicode and not Ascii - correct?

If so, then Unicode would be better, because if I understand correctly - Unicode also contains all accented characters on non English keyboards.

Basically, whatever type of key code Xojo checks for - that is the code value I need returned.

Hope that made sense.

Richard, here is an example project that does what you described (placing ascii codes in a textbox on keypress)…

http://www.xojo3d.com/press.xojo_binary_project

Thank you so much Alwyn - that worked perfectly!
Would that solve the problem that Kem kindly pointed out:

[quote]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]

Kem stated that Xojo uses the UTF-8 Unicode, and I need to check the key code (for use in Xojo), so I am now presuming that I need to display the Unicode value?

Is there a similar function in Xojo which would display the Unicode for a depressed key?

So, to sum things up so far:

The code below - displays the Ascii character: (but I am now unsure if I need to adapt the code, in order to encode it into Ascii - as recommended by Kem)???

Does this code:

TextField1.Text = Str(Asc(Key)) return true

Now need to be modified to this code???:

TextField1.Text.ConvertEncoding( Encodings.ASCII ) TextField1.Text = Str(Asc(Key)) return true

Also, I now need to find out if I can display the UTF-8 Unicode in a text field.

Thank you all for the help so far.

Would this display the Unicode UTF-8 value of a depressed key?
I can’t check as I am on my iPad :frowning:

TextField1.Text.ConvertEncoding( Encodings.UTF8 ) TextField1.Text = Str(Asc(Key)) return true

Richard try this and let me know if this is what you are after.

https://www.dropbox.com/s/l511ar5nm2y2d2r/RSTest.xojo_binary_project

** Note I just made a few edits to give you a picklist to try different encodings.

Note the Encoding Types and Characters in the TextArea Box.

I sense that an explanation of encodings may help. Let me know and I will.

Kem I PM’ed Richard a link that describes the key differences between ASCII and UTF-8. Any other links I am sure will help also.

Post those here too?

I am on the cusp of understanding.
Will eat my dinner and then look in depth at Mike’s project, he kindly made.

I just got confused as some of my values seemed to be Ascii, and others seemed to be Unicode UTF-8.

I will report back after my tummy is filled :slight_smile:

We’ve posted a few on the blog already
http://www.xojo.com/blog/en/2013/08/why-are-there-diamonds-in-my-user-interface.php
http://www.realsoftwareblog.com/2013/01/encodings-what-are-they.html

http://programmers.stackexchange.com/questions/97247/what-is-the-advantage-of-choosing-ascii-encoding-over-utf-8