开发者

keycode for ctrl

开发者 https://www.devze.com 2023-01-06 16:24 出处:网络
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

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.ActionEventinstead of the java.awt.event.KeyEvent. So the code for Ctrl in ActionEvent is CTRL_MASK.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消