3

The title pretty much says it all, how would I check if a char is typeable in java? And what I mean by typeable is not only if it is a letter or digit, but also if it is an exclamation mark or blankspace etc.

I do not want things such as escape and backspace characters to go through the filter.

I am aware of Character.isLetter() but this is not what I want, the result is too narrow.

Of course I could use a blacklist / whitelist filter, but since this is rather inconvenient I would prefer a somewhat more practical solution if their is one.

3
  • 4
    That's completely dependent on the keyboard layout.
    – kiheru
    May 6, 2015 at 21:06
  • Even symbols? (exclamation marks, quotation marks etc.)
    – SporreKing
    May 6, 2015 at 21:12
  • At least in part. The inverted question & exclamation marks (¿¡) are easily available on some keyboards, but less so on others. There may be others, but those came first to my mind. On the other hand, if you just want printable characters, then this question may be useful.
    – kiheru
    May 6, 2015 at 21:19

4 Answers 4

2

You could use one of the Character.isISOControl methods, if it's a control character then it's not typeable in the sense that I think you mean.

From wikipedia:

In computing and telecommunication, a control character or non-printing character is a code point (a number) in a character set, that does not represent a written symbol.

1
  • This one did the trick, thanks for all the other answers though!
    – SporreKing
    May 6, 2015 at 21:22
0

I think that the best way to do this is to use regular expressions. You can exactly specify which characters are "typeable".

3
  • @CasimiretHippolyte Seriously? ;)
    – slanecek
    May 6, 2015 at 21:12
  • 1
    No, but on the other hand, the question is a little crooked. May 6, 2015 at 21:15
  • Regex is an answer to everything... almost.
    – slanecek
    May 6, 2015 at 21:16
0

From the String point of view, thinking for instance a char as a single character string, you can use a regex (with a character class) to define what you consider as a valid character.

For instance, you can have something like this:

assertTrue("a".matches("[\\w!?/-]")); // Bear in mind that \w is [a-zA-Z0-9]
assertTrue("!".matches("[\\w!?/-]"));
assertTrue("?".matches("[\\w!?/-]"));
assertFalse(":".matches("[\\w!?/-]"));

So, you can define within the regex character class what the valid characters are. A more detailed example can be:

String charToTest = "W";
String validCharsRegex = "^[A-Za-z0-9!@#$%^&*)(-]$"; // Put withing [...] what
                                                     //  you consider valid characters

if (charToTest.matches(validCharsRegex))
    System.out.println("Valid character");
else
    System.out.println("Invalid character");

You can define your valid characters in a regex character class by putting all characters within [ and ]. So, for the above example the regex diagram looks like this:

Regular expression visualization

0

This is a strange question because you don't know from which keyboard the char comes from at the moment you have it.

If you want to check the nature of the char you could use regular expressions:

char a = 'a';
String.valueOf(a).matches(".*"); // returns true

Find out more about regular expressions in Pattern

Now if you want to check for key press events, you need to tell us which API or framework you are using, or what kind of application you are trying to build, as it would vary how you can check for these events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.