Results 1 to 6 of 6

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

  1. #1
    idph is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Dec 2007
    Posts
    3
    Reputation
    0

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

    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!

  2. #2
    InertiaM is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2007
    Location
    Kent, UK
    Posts
    663
    Reputation
    246
    e.KeyCode should give you what you are looking for
    Always parameterize your queries - read more here

    "When people discover the center of the universe, a lot of them will be disappointed to find they are not it."

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,470
    Reputation
    2732
    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 by JohnH; 12-22-2007 at 3:27 PM. Reason: edit note

  4. #4
    idph is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Dec 2007
    Posts
    3
    Reputation
    0
    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 by idph; 12-24-2007 at 2:44 PM. Reason: further explaination

  5. #5
    idph is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Dec 2007
    Posts
    3
    Reputation
    0
    Anyone have an idea???

  6. #6
    InertiaM is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2007
    Location
    Kent, UK
    Posts
    663
    Reputation
    246
    I dont have WPF, but based on JohnH's post, try this.

    Code:
            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.
    Always parameterize your queries - read more here

    "When people discover the center of the universe, a lot of them will be disappointed to find they are not it."

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking