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

Mynotoar

Active member
Joined
Sep 27, 2012
Messages
28
Location
England
Programming Experience
Beginner
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.

VB.NET:
Dim CKI As ConsoleKeyInfo
[COLOR=#00008B]Do
    CKI = Console.Readkey(True)
    If[/COLOR] [COLOR=#00008B]Char[/COLOR].IsLetterOrDigit(CKI.KeyChar) [COLOR=#00008B]Or[/COLOR] [COLOR=#00008B]Char[/COLOR].IsPunctuation(CKI.KeyChar) [COLOR=#00008B]Or[/COLOR] [COLOR=#00008B]Char[/COLOR].IsWhiteSpace(CKI.KeyChar) [COLOR=#00008B]Or[/COLOR] [COLOR=#00008B]Char[/COLOR].IsSymbol(CKI.KeyChar) [COLOR=#00008B]Then
[/COLOR]
         [Run code]

[COLOR=#00008B]    End[/COLOR] [COLOR=#00008B]If
Loop
[/COLOR]

Is there a way to refer to the set of keys that don't generate characters?
 
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.
 
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.
 
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.
 

Latest posts

Back
Top