Results 1 to 4 of 4

Thread: [RESOLVED] Keys to Char?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Resolved [RESOLVED] Keys to Char?

    Hi,

    In KeyDown event, one can check the pressed key by looking at e.KeyValue/KeyCode, but since there are two sets of numeric keys (one is in the keypad and another is above the letter keys), they have differencen KeyCode, e.g. for number "1", e.KeyCode would either equals to Keys.D1 or Keys.NumPad1. What I'm interested is only the char "1", so is there any efficient way to obtain a single char for the key that being pressed?

    I tried
    KeyInChar = Convert.ToChar(e.KeyValue)
    but it is not correct

    Any help would be appreciated!
    Last edited by LazNiko; Dec 5th, 2006 at 11:38 AM.

  2. #2
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: Keys to Char?

    Im pretty sure you just have to make a case for numpad1 OR d1

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Keys to Char?

    It sounds to me like you should be handling the KeyPress event instead. The KeyDown and KeyUp events relate to individual kays on the keyboard, while the KeyPress event relates to input characters. Note that if you use the KeyDown event you'd also have to test the modifier keys because if the Shift key is depressed then pressing the "1" key won't produce the character "1" but an exclamation mark instead. In the KeyPress event it would simply be:
    VB Code:
    1. If e.KeyChar = "1"c Then
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: Keys to Char?

    Thanks for the suggestions!! They solved my problem.

Posting Permissions

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



Click Here to Expand Forum to Full Width