keycode for ctrl
For multiple selection in a Jtree,I am using multiple selection mode in it.开发者_如何学CIt works.But I want to know when i am making multiple selection exactly in this tree to do this i wrote a very simple keycontroller class that implements KeyListener, but i wanna check whether CTRL is pressed or not to do so i am using this code but it seems to be not working :
kc.getKeyCode() == KeyEvent.CTRL_DOWN_MASK ;
what is the keyCode for ctrl ? Or am i doing something wrong ?
As CTRL is a key mask, there is no character for the CTRL key alone.
However, according to KeyEvent documentation, there is always a vaild key code that is sent when either a key is pressed or released. in that case, it should be KeyEvent.CHAR_UNDEFINED
and getModifiersEx()
should return true for the CTRL key. Notice that, for it to work, you have to register a KeyListener (specially handle for both keyPressed() and keyReleased()).
The key code for Ctrl is KeyCode.VK_CONTROL
. In order to find if Ctrl is held you can do this:
if ((event.getModifiers() & ActionEvent.CTRL_MASK) ==ActionEvent.CTRL_MASK) {
System.out.println("CTRL KEY PRESSED");
}
Which is using the java.awt.event.ActionEvent
instead of the java.awt.event.KeyEvent
. So the code for Ctrl in ActionEvent
is CTRL_MASK
.
Hope this helps.
精彩评论