469,280 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

C# Win: KeyCode numbers to string??

115 100+
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 "."??
Nov 9 '08 #1
5 28652
tlhintoq
3,525 Expert 2GB
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.
Nov 9 '08 #2
ShadowLocke
115 100+
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.             }
Nov 12 '08 #3
Plater
7,872 Expert 4TB
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.  
Nov 12 '08 #4
tlhintoq
3,525 Expert 2GB
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.  
Nov 13 '08 #5
ShadowLocke
115 100+
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
Nov 21 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Leroy | last post: by
6 posts views Thread by rich_poppleton | last post: by
6 posts views Thread by Matthias S. | last post: by
1 post views Thread by Paul Nathan | last post: by
23 posts views Thread by mrvendetta1 | last post: by
9 posts views Thread by Michael D. Ober | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.