C# Win: KeyCode numbers to string??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShadowLocke
    New Member
    • Jan 2008
    • 116

    C# Win: KeyCode numbers to string??

    Ive been digging around google for the past hour trying to figure this out..

    I have a key code that is not coming from an event, its manually entered in. I need to convert that code to its key representation. i.e. I enter in 65, i want to see A. I have tried a few different ways but cant seem to get the results im looking for. I tried using Char.ConvertFro mUtf32() but if press "." it returns the "3/4" character. I tried casting it to Keys but it gives me "OemPeriod" . How do i get it to just give me "."??
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    It would help if you would share with us the code you are using for the translation but...

    I am pretty certain that the KeyCodes and ASCII codes don't completely correspond. For that matter, KeyCodes aren't even always numbers.

    Code:
     private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                string bob = e.KeyCode.ToString();// Put a breakpoint here 
            }
    For example, testing with "KeyDown" event and pressing an unmodified 'a' gets you a KeyCode of 65. But 65 is an ASCII uppercase A.
    With a KeyValue of 65.

    Pressing a period gets you a KeyCode of "RButton | MButton | Back | ShiftKey | Space | F17"
    With a KeyValue of 190

    When you say you are entering a KeyCode manually like 65.... Are you saying you are typing in a number into a text box and you are translating that number, and expecting a single character to be returned? So if you type 65 you want an upper case A. If you enter 46 you want a period.

    But later you say you want a period returned when you press a period. "." ASCII 46. I don't know why you are translating it at all. If you want returned, exactly what is pressed then just use it as you receive it.

    Obviously there are two different ways you are taking in values and trying to convert them. In one you want to convert a number like 65 to a character, and in another you want to take a character like a period and simply return a period.

    Without seeing the code for what you're doing I don't think there's much more advice I can offer.

    Comment

    • ShadowLocke
      New Member
      • Jan 2008
      • 116

      #3
      I got what i was looking for out of this..

      Code:
                  public String KeycodeToChar(int keyCode)
                  {
                      Keys key = (Keys)keyCode;
      
                      switch (key)
                      {
                          case Keys.Add:
                              return "+";
                          case Keys.Decimal:
                              return ".";
                          case Keys.Divide:
                              return "/";
                          case Keys.Multiply:
                              return "*";
                          case Keys.OemBackslash:
                              return "\\";
                          case Keys.OemCloseBrackets:
                              return "]";
                          case Keys.OemMinus:
                              return "-";
                          case Keys.OemOpenBrackets:
                              return "[";
                          case Keys.OemPeriod:
                              return ".";
                          case Keys.OemPipe:
                              return "|";
                          case Keys.OemQuestion:
                              return "/";
                          case Keys.OemQuotes:
                              return "\"";
                          case Keys.OemSemicolon:
                              return ";";
                          case Keys.Oemcomma:
                              return ",";
                          case Keys.Oemplus:
                              return "+";
                          case Keys.Oemtilde:
                              return "`";
                          case Keys.Separator:
                              return "-";
                          case Keys.Subtract:
                              return "-";
                          case Keys.D0:
                              return "0";
                          case Keys.D1:
                              return "1";
                          case Keys.D2:
                              return "2";
                          case Keys.D3:
                              return "3";
                          case Keys.D4:
                              return "4";
                          case Keys.D5:
                              return "5";
                          case Keys.D6:
                              return "6";
                          case Keys.D7:
                              return "7";
                          case Keys.D8:
                              return "8";
                          case Keys.D9:
                              return "9";
                          case Keys.Space:
                              return " ";
                          default:
                              return key.ToString();
                      }
                  }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        That seems like a lot of work to just type cast?
        You just want to go from the number to the string version of it?

        [code=c#]
        public String KeycodeToChar(i nt keyCode)
        {
        return ((char)keyCode) .ToString();
        }
        [/code]

        EDIT: Ignore below
        [code=c#]
        Keys k;
        k = (Keys)'.';
        k = (Keys)65;
        [/code]

        OR

        [code=c#]
        char c;
        c = (char)Keys.Deci mal;
        c= (char)Keys.A;
        //etc
        [/code]

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Originally posted by Plater
          That seems like a lot of work to just type cast?
          You just want to go from the number to the string version of it?

          [code=c#]
          public String KeycodeToChar(i nt keyCode)
          {
          return ((char)keyCode) .ToString();
          }
          [/code]

          EDIT: Ignore below
          [code=c#]
          Keys k;
          k = (Keys)'.';
          k = (Keys)65;
          [/code]

          OR

          [code=c#]
          char c;
          c = (char)Keys.Deci mal;
          c= (char)Keys.A;
          //etc
          [/code]
          I think it is about the only way to do some custom conversion where you want to change things, or handle a 10key pad seperatly from the main numbers.
          case Keys.NumPad1: would be different than Keys.D1

          And in the above code a tilde is being substitued as ' ` ' instead of ' ~', and a question mark has become a ' / '

          I am curious about how this handles an 'Enter' key on your keyboard. Because my Logitech sends a "LBUTTON | MBUTTON | RBUTTON" and does not hit on Keys.Enter

          I was able to differentiate the 10key enter from the main key enter by looking at the LPARM and HPARM of the windows message.

          While the above is a lot of work for a simple cast, it could be a good way to handle form-wide hot-keys without concern for which control has focus.

          Code:
          case Keys.NumPad2:
               columns = 2;
               // Do some cool stuff to format for 2 column display
               return true;// Handled so don't pass up the chain
          break;

          Comment

          • ShadowLocke
            New Member
            • Jan 2008
            • 116

            #6
            Originally posted by Plater
            That seems like a lot of work to just type cast?
            You just want to go from the number to the string version of it?

            [code=c#]
            public String KeycodeToChar(i nt keyCode)
            {
            return ((char)keyCode) .ToString();
            }
            [/code]
            Thats exactly what i am looking for Plater. But as tlhintoq mentioned pressing the "." key on the keyboard returns 190 instead of 46. Im looking for the equvalent of KeyPressEventAr gs.KeyChar

            Comment

            Working...