ASCII value instead of System.Windows.Input.Key?

idph

New member
Joined
Dec 21, 2007
Messages
3
Programming Experience
10+
Has anyone figured out how to get the ASCII character value instead of the System.Windows.Input.Key in a KeyUp event?

I would like very much to get the ascii character value of a returned to me as an integer from a KeyEventArgs instead of something like "Keys.A"

Please help!
 
e.KeyCode should give you what you are looking for :)
 
e.KeyCode is type Keys, e.KeyValue is the numeric value. To get the Ascii character you would just use ToString method of the Keys value. e.KeyCode.ToString() If for example the key value is Keys.A the ToString method would return "A".

Edit: This is a WPF I suddenly notice... still, using ToString method on the e.Key object (of type System.Windows.Input.Key) will give the "A" string.
 
Last edited:
Neither e.KeyCode or e.KeyValue exist in a (KeyEventArgs e)

for example in:
'vb.net
Private Sub somecontrol_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
'KeyCode and KeyValue are not properties of e
End Sub

or
//c#
private void somecontrol_KeyUp(object sender, KeyEventArgs e){
// KeyCode and KeyValue are not properties of e
}


is there another "Key State" event I should be using? I've tried a few of them... same results

The reason I want the numeric character values is because I'm using a barcode reader (which simulates keystrokes) for input. It's much easier to work with the numeric key/character values than it is the "Key" itself. I've built many of these barcode and magnetic strip reading types of apps before in vb 3-6, c++, .net 1.1 and 2, but both vb.net and C# 3.5 are plaguing me. the best way to handle the input is with the ASCII character values coming in that way to can easily distinguish between cases and detect characters 10, 13 9, 41, 20 etc.
 
Last edited:
I dont have WPF, but based on JohnH's post, try this.

VB.NET:
        If e.Key.ToString.Length = 1 Then
            Dim intAsciiValue As Int32 = Convert.ToInt32(e.Key.ToString.Chars(0))
            MessageBox.Show(intAsciiValue.ToString)
        Else
            'special key eg Return
            'handle this differently
        End If

It doesnt seem to give you differences between lower and uppercase though.
 
Back
Top