locked
How to determine character codes RRS feed

  • Question

  • When I use "Text.GetCharacterCode" it returns 68 for a "d".  It also returns 68 for a keyboard "3".  Am I missing part of the character code that would differentiate these two?
    Wednesday, June 24, 2009 4:31 PM

Answers

  • So, yes, you're not getting the real character ("3") but the name of the button you pressed ("D3").

    GetCharacterCode, as I said, return the character code of the FIRST CHAR of the args you send to it.

    "D"   ==> Code of "D"
    "D3" ==> Code od "D"
    ====> You've a match !

    Currently, there's no way to get the 'real' value of the charcode, or no way I know.

    In the .NET FrameWork, it seems that there's a solution :

    Supported by the .NET Compact Framework KeyCode The bitmask to extract a key code from a key value.

    Maybe this can be considered for the next version of Small Basic...

    [EDIT] Vijaye has been smarter than me.
    @Vijaye : It still doesn't works as some keys are of type "Oem1" but return characters like ";", ",", "\", ...
    It also have the problem that it doesn't take in consideration the fact we pressed on the SHIFT or the CONTROL key or not.

    ", # and 3 have the same "KeyName" on my computer (AZERTY, Belgian Keyboard).
    Maybe you should provide three properties : LastKeyName; LastKeyChar; LastKeyCode;

    ----------------------------------------------------------------------------------------------------------------------------
    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Wednesday, June 24, 2009 9:26 PM

All replies

  • How are you capturing the input?
    Wednesday, June 24, 2009 4:36 PM
  • I'm capturing the input with the following line:

    TextWindow.WriteLine( Text.GetCharacterCode( ltrKeyd ))
    Wednesday, June 24, 2009 4:48 PM
  • Text.GetCharacterCode() does not capture input. It says in the help when you move the cursor to GetCharacterCode

    "Given a unicode character, gets the corresponding character code. "

    This means in your example that it is always returning 68 which is the code for "D" (note capital D not lower case).

    To input into the text window you need to use either
    TextWindow.Read() or
    TextWindow.ReadNumber()
    both of which require you to press Enter to input the data.

    Wednesday, June 24, 2009 5:35 PM
  • TextWindow.WriteLine("...")
    While "True"
      k = TextWindow.Read()
      TextWindow.WriteLine(Text.GetCharacterCode(k))
    EndWhile

    This code works pretty great.

    The problem is that some functions returns the CHARACTER NAME. So typing '1' may return 'NumPad1', which is computed 'N' by getCharCode.
    Please verrify the way you get 'ltrKeyd'.
    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Wednesday, June 24, 2009 7:09 PM
  • Sorry, should have included my code.  I'm trying to enter data from the graphics window and print it on the same window.

    ltrPos = 20

    While "True"
      GraphicsWindow.KeyDown = keyDown
    EndWhile

    Sub keyDown
      ltrKeyd = GraphicsWindow.LastKey
      GraphicsWindow.DrawText( ltrPos, 20, ltrKeyd )
      ltrPos = ltrPos + 20
      TextWindow.WriteLine( Text.GetCharacterCode( ltrKeyd ))
    EndSub

    When I press "d" it comes back "D" with a code 68.
    When I press "3" on the keyboard it comes back "D3" with a code 68.

    I'm wondering why the same code, unless it is prefixed or suffixed with something to make it unique.

    Wednesday, June 24, 2009 8:15 PM
  • Ah, the problem is that the number "3" is represented as "D3" in the Key enumeration.  This is causing a problem in the logic, which picks up the first character ('D') to use for the character code conversion.

    One way to get around it is to use:

      ltrKeyd = Text.GetSubTextToEnd(ltrKeyd, Text.GetLength(ltrKeyd))
      TextWindow.WriteLine(Text.GetCharacterCode(ltrKeyd))

    But beware, this wouldn't still work with keys like "Home", "End", "PageUp", etc.

    Wednesday, June 24, 2009 9:21 PM
  • So, yes, you're not getting the real character ("3") but the name of the button you pressed ("D3").

    GetCharacterCode, as I said, return the character code of the FIRST CHAR of the args you send to it.

    "D"   ==> Code of "D"
    "D3" ==> Code od "D"
    ====> You've a match !

    Currently, there's no way to get the 'real' value of the charcode, or no way I know.

    In the .NET FrameWork, it seems that there's a solution :

    Supported by the .NET Compact Framework KeyCode The bitmask to extract a key code from a key value.

    Maybe this can be considered for the next version of Small Basic...

    [EDIT] Vijaye has been smarter than me.
    @Vijaye : It still doesn't works as some keys are of type "Oem1" but return characters like ";", ",", "\", ...
    It also have the problem that it doesn't take in consideration the fact we pressed on the SHIFT or the CONTROL key or not.

    ", # and 3 have the same "KeyName" on my computer (AZERTY, Belgian Keyboard).
    Maybe you should provide three properties : LastKeyName; LastKeyChar; LastKeyCode;

    ----------------------------------------------------------------------------------------------------------------------------
    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Wednesday, June 24, 2009 9:26 PM
  • That's a great suggestion Fremy.  I've added it to the list of v0.6 updates.
    Wednesday, June 24, 2009 9:53 PM
  • Is it possible that the "Text" commands are not meant to be used in the Graphics window?  Everything works fine if I use a text window.  Of course, that is not what I want to do...

    While "True"
      keyDown() 
    EndWhile

    Sub keyDown
      ltrKeyd = TextWindow.Read()
      TextWindow.WriteLine( Text.GetCharacterCode( ltrKeyd ))
    EndSub

    Wednesday, June 24, 2009 9:55 PM
  • No, the GetCharacterCode works as expected.

    As we already have said, the problem is that it needs a Character as parameter. And LastKey returns a Key Name. So, getCharactherCode returns the ANSI code of the first char of the key name, which is not what you want.

    Currently, there's no way to do what you want, but I'll maybe create a small extension to provide a temporary solution until v0.6. 
    I'll update this thread when I'll publish this small extension.
    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Wednesday, June 24, 2009 9:58 PM
  • ClydeW,
    You said,
    "I'm trying to enter data from the graphics window and print it on the same window."

    How about using an array as a look-up table?
    Your code snippet shows you the key names as you press each key. If you use these names to index into an array, you can get the corresponding value.

    I'm sorry this code is for v0.4, I haven't got round to downloading v0.5 yet. The array handling has changed, but you should be able to convert this from the information in recent posts.

    e.g. Arrayname("D3", "3")
          Arrayname("NumPad1", "1")
          Arrayname("A", "A")

    Of course you would have to enter all the required keys into the array, but the following code translates the listed keys pressed to display the correct character in the GraphicsWindow.

    Array.SetValue("keys", "D3", "3")
    Array.SetValue("keys", "NumPad1", "1")
    Array.SetValue("keys", "A", "A")

    ltrPos = 20

    While "True"
      GraphicsWindow.KeyDown = keyDown
    EndWhile

    Sub keyDown
      ltrKeyd = GraphicsWindow.LastKey
       GraphicsWindow.DrawText( ltrPos, 20, Array.GetValue("keys", ltrKeyd))
      ltrPos = ltrPos + 20
      TextWindow.WriteLine( Text.GetCharacterCode( ltrKeyd ))
    EndSub

    There was a listing posted of keynames in the thread "Is There a List of Key Names". You would need to enter all of these that you planned to use.

    Thursday, June 25, 2009 6:00 PM
  • @Stendec :
    Not really possible as it's not possible to distinguish "A" and "a" using the KeyName.
    Also, it's not possible to handle the right 'char' for some keys like "Oem1" which can be "," on some keyboars and ":" on some others.

    This method would never drive to a concluant solution...
    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Thursday, June 25, 2009 6:43 PM
  • Fremy,
       Agreed it doesn't solve all the problems and it won't work on all platforms, but it may be enough for what ClydeW is trying to do.
    Thursday, June 25, 2009 7:42 PM