473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 30043
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

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

Similar topics

9
by: Leroy | last post by:
I have this function: function ValidateIntKey() { var Key; var CharKey; Key = event.keyCode; alert(Key); //CharKey = String.charCodeAt(Key);
6
by: rich_poppleton | last post by:
Help.... I've got a textarea where people type in a description. However for certain reasons we need to stop them typing !$*^ . I have a solution this which works fine in IE: function...
6
by: Matthias S. | last post by:
Hi, I'm pretty sure this is as easy as it gets, but I couldn't find anything in the documentation. I'd like to change the string representation of an int so that the numbers get padded with...
5
by: Flip | last post by:
I'm trying to create a form which a user can type out a key, and the CTRL-ALT-SHIFT keys are recognized (via ModifiedKeys object) and then pass the letter the user pressed to the windows API method...
1
by: Paul Nathan | last post by:
Further to my post about keyboard problems, I have discovered that Num Lock seems to change the e.keycode for the arrow keys and the 9 above them excluding scroll lock and pause-break. With Num...
5
by: Bill | last post by:
Hello, Could anyone post some simple code or advise me on how I can display the SSN number like *****7890 in a text box, even thought the user entered 1234567890, and the value of the variable...
23
by: mrvendetta1 | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 33 void StringToIntArray(const char *s1); void takeinteger(int i); int main() { char string; printf("enter a string of...
3
by: Pugi! | last post by:
I got this (piece of) script from 'DHTML Utopia - Modern Webdesign - Using Javascript & DOM'. function aKeyWasPressed(e) { if (window.event) { var key = window.event.keyCode; } else { var key...
9
by: Michael D. Ober | last post by:
OK, I can't figure out a way to optimize the following VB 2005 code using StringBuilders: Public Const RecSize as Integer = 105 Private buffer As String Public Sub New() init End Sub...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.