I was wondering how to get all of the keys pressed in a key event. For example, I want to write a listener for ctrl + f that would toggle fullscreen. How could I check if both ctrl and f are pressed in one event?
EDIT 1:
I tried printing KeyEvent.getModifiersExText(e.getModifiersEx())
and typing ctrl + f, but that just yielded ?
.
I found a few cool methods that will work in this instance:
e.isAltDown();
e.isAltGraphDown()
e.isControlDown()
e.isShiftDown()
Something like this should do it:
To be honest,
KeyListener
has many limitations and is cumbersome to use (IMHO), instead, I would simply take advantage of the key bindings API, which generally provides you with a greater deal of flexibility and potentional for resuse.And just so you don't think I'm completely bias, here's an example using
KeyListener
...If you press two keys the 'keyPressed(KeyEvent e)' is called twice. You can store the pressed key in a boolean array and check if they are both pressed.
The release has to release the control variable again.