Connecting Tech Pros Worldwide Forums | Help | Site Map

C# Win: KeyCode numbers to string??

Member
 
Join Date: Jan 2008
Posts: 116
#1: Nov 9 '08
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.ConvertFromUtf32() 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's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#2: Nov 9 '08

re: C# Win: KeyCode numbers to string??


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.

Expand|Select|Wrap|Line Numbers
  1.  private void Form1_KeyDown(object sender, KeyEventArgs e)
  2.         {
  3.             string bob = e.KeyCode.ToString();// Put a breakpoint here 
  4.         }
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.
Member
 
Join Date: Jan 2008
Posts: 116
#3: Nov 12 '08

re: C# Win: KeyCode numbers to string??


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

Expand|Select|Wrap|Line Numbers
  1.             public String KeycodeToChar(int keyCode)
  2.             {
  3.                 Keys key = (Keys)keyCode;
  4.  
  5.                 switch (key)
  6.                 {
  7.                     case Keys.Add:
  8.                         return "+";
  9.                     case Keys.Decimal:
  10.                         return ".";
  11.                     case Keys.Divide:
  12.                         return "/";
  13.                     case Keys.Multiply:
  14.                         return "*";
  15.                     case Keys.OemBackslash:
  16.                         return "\\";
  17.                     case Keys.OemCloseBrackets:
  18.                         return "]";
  19.                     case Keys.OemMinus:
  20.                         return "-";
  21.                     case Keys.OemOpenBrackets:
  22.                         return "[";
  23.                     case Keys.OemPeriod:
  24.                         return ".";
  25.                     case Keys.OemPipe:
  26.                         return "|";
  27.                     case Keys.OemQuestion:
  28.                         return "/";
  29.                     case Keys.OemQuotes:
  30.                         return "\"";
  31.                     case Keys.OemSemicolon:
  32.                         return ";";
  33.                     case Keys.Oemcomma:
  34.                         return ",";
  35.                     case Keys.Oemplus:
  36.                         return "+";
  37.                     case Keys.Oemtilde:
  38.                         return "`";
  39.                     case Keys.Separator:
  40.                         return "-";
  41.                     case Keys.Subtract:
  42.                         return "-";
  43.                     case Keys.D0:
  44.                         return "0";
  45.                     case Keys.D1:
  46.                         return "1";
  47.                     case Keys.D2:
  48.                         return "2";
  49.                     case Keys.D3:
  50.                         return "3";
  51.                     case Keys.D4:
  52.                         return "4";
  53.                     case Keys.D5:
  54.                         return "5";
  55.                     case Keys.D6:
  56.                         return "6";
  57.                     case Keys.D7:
  58.                         return "7";
  59.                     case Keys.D8:
  60.                         return "8";
  61.                     case Keys.D9:
  62.                         return "9";
  63.                     case Keys.Space:
  64.                         return " ";
  65.                     default:
  66.                         return key.ToString();
  67.                 }
  68.             }
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Nov 12 '08

re: C# Win: KeyCode numbers to string??


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?

Expand|Select|Wrap|Line Numbers
  1. public String KeycodeToChar(int keyCode) 
  2.    return ((char)keyCode).ToString();
  3. }
  4.  
EDIT: Ignore below
Expand|Select|Wrap|Line Numbers
  1. Keys k;
  2. k = (Keys)'.';
  3. k = (Keys)65;
  4.  
OR

Expand|Select|Wrap|Line Numbers
  1. char c;
  2. c = (char)Keys.Decimal;
  3. c= (char)Keys.A;
  4. //etc
  5.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#5: Nov 13 '08

re: C# Win: KeyCode numbers to string??


Quote:

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?

Expand|Select|Wrap|Line Numbers
  1. public String KeycodeToChar(int keyCode) 
  2.    return ((char)keyCode).ToString();
  3. }
  4.  
EDIT: Ignore below
Expand|Select|Wrap|Line Numbers
  1. Keys k;
  2. k = (Keys)'.';
  3. k = (Keys)65;
  4.  
OR

Expand|Select|Wrap|Line Numbers
  1. char c;
  2. c = (char)Keys.Decimal;
  3. c= (char)Keys.A;
  4. //etc
  5.  

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.

Expand|Select|Wrap|Line Numbers
  1. case Keys.NumPad2:
  2.      columns = 2;
  3.      // Do some cool stuff to format for 2 column display
  4.      return true;// Handled so don't pass up the chain
  5. break;
  6.  
Member
 
Join Date: Jan 2008
Posts: 116
#6: Nov 21 '08

re: C# Win: KeyCode numbers to string??


Quote:

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?

Expand|Select|Wrap|Line Numbers
  1. public String KeycodeToChar(int keyCode) 
  2.    return ((char)keyCode).ToString();
  3. }
  4.  

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 KeyPressEventArgs.KeyChar
Reply


Similar .NET Framework bytes