Reputation: 3
I am trying to check if the user typed the y key, but I want it to be lowercase. I don't know how to check that. I am using if (e.getKeyCode()==KeyEvent.VK_Y)
, but I need the y key to be lowercase, when it is capital it is messing up my code.
Upvotes: 0
Views: 392
Reputation: 420
The method getKeyCode()
happens to return the ASCII code, and the value of constant VK_Y
is a integer 89 (reference). This value corresponds to char Y
from ASCII table.
If you want a lowercase, use the corresponded value to y instead of constant KeyEvent.VK_Y
. According the ASCII table, a integer 121 corresponds to char y
.
So, you can do like:
if (e.getKeyCode() == 121)
or:
if (e.getKeyCode() == 'y')
Upvotes: 0
Reputation: 2393
Maybe try something like
KeyEvent.getKeyText(e.getKeyCode()).equalsIgnoreCase("y")
This converts the keycode to a string then compares it case insensitive.
Or if you only want to accept lowercase just equals
e.g.
KeyEvent.getKeyText(e.getKeyCode()).equals("y")
Upvotes: 1
Reputation: 5165
getKeyCode
returns the code of the key pressed, regardless of the intent or modifiers. To see if the shift modifier is applied, try using getModifiers()
on the event.
Note that these key events are low-level, so making sense of the events in a more natural way is going to be more complex.
Upvotes: 2