How to convert KeyCode to a string?

B

BD

Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD
 
G

Guest

Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD

Just cast KeyEventArgs.KeyValue to a char and assign it to the string, for
example:

private string s = string.Empty;
//
private void SomeKeyDown(object sender, KeyPressEventArgs e)
{
s += (char)e.KeyValue;
}

Regards,
Anne
 
B

BD

Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD

Just cast KeyEventArgs.KeyValue to a char and assign it to the string, for
example:

private string s = string.Empty;
//
private void SomeKeyDown(object sender, KeyPressEventArgs e)
{
s += (char)e.KeyValue;
}

Regards,
Anne

But I'm using the Form_KeyDown(object sender, KeyEventArgs e)
and the string representation of the decimal key "." is a "n".

s += (char)e.KeyValue; // s = "n" and I click the decimal key

Why it's so different?
How can I get the decimal character from the Regional Settings?

[]'s
BD
 
G

Guest

But I'm using the Form_KeyDown(object sender, KeyEventArgs e)
and the string representation of the decimal key "." is a "n".

s += (char)e.KeyValue; // s = "n" and I click the decimal key

Why it's so different?
How can I get the decimal character from the Regional Settings?

KeyEventArgs contains the key number of the keyboard , while
KeyPressEventArgs contains the key pressed (see
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx
for detailed information). So you better use Form_KeyPress instead of
Form_KeyDown;

The decimal character (and more info) can be obtained from class
NumberFormatInfo (using System.Globalization). In the next example all keys
will be send to the control that has focus (like a textbox), but not if the
decimal seperator is pressed:

private void Form_KeyPress(object sender, KeyPressEventArg e)
{
if (NumberFormatInfo.CurrentInfo.NumberDecimalSeperator ==
e.KeyChar.ToString())
{
// Decimal seperator pressed
e.Handled = true; // don't send the character to the focused control
}
}

Regards,
Anne
 
B

BD

KeyEventArgs contains the key number of the keyboard , while
KeyPressEventArgs contains the key pressed (see
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx
for detailed information). So you better use Form_KeyPress instead of
Form_KeyDown;

The decimal character (and more info) can be obtained from class
NumberFormatInfo (using System.Globalization). In the next example all keys
will be send to the control that has focus (like a textbox), but not if the
decimal seperator is pressed:

private void Form_KeyPress(object sender, KeyPressEventArg e)
{
if (NumberFormatInfo.CurrentInfo.NumberDecimalSeperator ==
e.KeyChar.ToString())
{
// Decimal seperator pressed
e.Handled = true; // don't send the character to the focused control
}
}

Regards,
Anne

Understood. Thank you very much Anne.

[]'s
BD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top