How to test if modifier with keycode is down
I have this code:
import java.awt.KeyEventDispatcher; import java.awt.event.KeyEvent; import java.awt.Toolkit; public class KeyListener implements KeyEventDispatcher { @Override public boolean dispatchKeyEvent(KeyEvent e) { if(e.getID() == KeyEvent.KEY_PRESSED) { if(e.isDown(Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) && e.getKeyCode() == KeyEvent.VK_V) { paste(); } } } }
The problem is that e.isDown
doesn't exist.
What alterntives can use so that it is cmd+开发者_如何转开发V for macs and control+V for other OS'es?
KeyEvent extends InputEvent so you can use the isControlDown()
method of InputEvent.
Edit: I found this link which shows how to create a KeyStroke for the command key:
http://lists.apple.com/archives/java-dev/2007/May/msg00243.html
So instead of using a KeyEventDispatcher, you should be using Key Bindings. Key Bindings allow you to invoke an Action for a given KeyStroke. Swing was designed to use Key Bindings.
精彩评论