Why does
KeyEvent.getKeyText(0).substring(0, 7) == "Unknown"
return false when
System.out.print(KeyEvent.getKeyText(0).substring(0, 7));
prints exactly "Unknown"?
In Java Strings are objects, so you should not compare with ==
. You have to call equals on strings to compare there content.
If you compare them with ==
you compare them by reference.
KeyEvent.getKeyText(0).substring(0, 7).equals("Unknown");
An alternative would be contains:
KeyEvent.getKeyText(0).contains("Unknown");
See this post: Java String.equals versus ==