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 ?
.
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.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class KeyListenerTest {
public static void main(String[] args) {
new KeyListenerTest();
}
public KeyListenerTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel lbl;
private boolean fullScreen = false;
public TestPane() {
lbl = new JLabel("Normal");
setLayout(new GridBagLayout());
add(lbl);
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_DOWN_MASK), "FullScreen");
am.put("FullScreen", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (fullScreen) {
lbl.setText("Normal");
} else {
lbl.setText("Full Screen");
}
fullScreen = !fullScreen;
}
});
}
}
}
And just so you don't think I'm completely bias, here's an example using KeyListener
...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class KeyListenerTest {
public static void main(String[] args) {
new KeyListenerTest();
}
public KeyListenerTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel lbl;
private boolean fullScreen = false;
public TestPane() {
lbl = new JLabel("Normal");
setLayout(new GridBagLayout());
add(lbl);
setFocusable(true);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocusInWindow();
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_F && e.isControlDown()) {
if (fullScreen) {
lbl.setText("Normal");
} else {
lbl.setText("Full Screen");
}
fullScreen = !fullScreen;
}
}
});
}
}
}