Results 1 to 4 of 4

Thread: Is it possible to test whether the key pressed can generate a character?

  1. #1
    Mynotoar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2012
    Location
    England
    Posts
    28
    Reputation
    55

    Question Is it possible to test whether the key pressed can generate a character?

    I'm trying to make a class that produces a facsimile of the textbox from Winforms in Console. I have a loop within this class that is reading keypresses as ConsoleKeyInfo. What I want to do is to take a keypress and test whether it corresponds to an actual character. For example, the A key passes this test because it generates an a. Letters, digits, punctuation, symbols etc. are all fine, the only thing I don't want to accept are things like Home, Esc, Arrow keys, modifier keys, etc. This is essentially the solution I've got now, but as you can see it's hardly ideal.

    Code:
    Dim CKI As ConsoleKeyInfo
    Do
        CKI = Console.Readkey(True)
        If Char.IsLetterOrDigit(CKI.KeyChar) Or Char.IsPunctuation(CKI.KeyChar) Or Char.IsWhiteSpace(CKI.KeyChar) Or Char.IsSymbol(CKI.KeyChar) Then
    
             [Run code]
    
        End If
    Loop
    
    Is there a way to refer to the set of keys that don't generate characters?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,767
    Reputation
    1702
    Perhaps you should start by reading the relevant MSDN documentation when you strike an issue. The Help for the ConsoleKeyInfo.KeyChar property specifically states:
    If the key pressed does not map to a Unicode character (for example, if the user presses the F1 key or the Home key), the value of the KeyChar property is \U0000.
    If I'm not mistaken, that's equivalent to ControlChars.NullChar.

  3. #3
    Mynotoar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2012
    Location
    England
    Posts
    28
    Reputation
    55

    Thumbs up

    Quote Originally Posted by jmcilhinney View Post
    Perhaps you should start by reading the relevant MSDN documentation when you strike an issue. The Help for the ConsoleKeyInfo.KeyChar property specifically states:If I'm not mistaken, that's equivalent to ControlChars.NullChar.
    Thank you, that does almost exactly what I need . The reason that MSDN isn't my first port of call is simply because it's not a good reference guide. You have to know exactly what you're looking for, and even when you do it's often not written in an intuitive way.

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,767
    Reputation
    1702
    Quote Originally Posted by Mynotoar View Post
    Thank you, that does almost exactly what I need . The reason that MSDN isn't my first port of call is simply because it's not a good reference guide. You have to know exactly what you're looking for, and even when you do it's often not written in an intuitive way.
    Firstly, it is a good reference guide. I've been using it since I first started using .NET so I know from personal experience.

    Secondly, you DID know exactly what you were looking for, or at least you should have. You're already using the ConsoleKeyInfo.KeyChar property so why would it be a mystery to you?

    Thirdly, MSDN is a programming reference, not a beginner tutorial, so it's not always going to be easy for the beginner to understand. That's not justification for not using it though. If you read it and you can't find what you need or can't understand what you find then you've lost very little but if it can find and understand the information you need then your problem is solved. Did you not bother to post your question on this forum just in case noone could answer it? The more you read the documentation, the better you'll get at using it and you will also end up finding things that you weren't even looking for that will be helpful immediately or later. Again, I speak from personal experience so I know what I'm talking about. If I can do it, you can do it.

Tags for this Thread

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
  •