I currently have a very simple game, where you move a black rectangle from side to side using the arrows keys. My problem is that whenever the alt key is being pressed (mostly by accident), the game window loses focus and doesn't reply to the arrow keys anymore. The focus is regained if I press alt again, but if the rectangle is already in motion it will keep going non stop.
I'm using these render and update methods btw.
Is there a way to prevent the alt key from doing its job - focusing on the UI's menu?
Is there a way to prevent the alt key from doing its job - focusing on the UI's menu?
Strictly speaking, no - or more to the point, that's the wrong approach.
What you should be doing is using the Key Bindings API which will overcome the focus related issues of KeyListener, this way, you don't "have" to care
This is a really simple test which binds actions to the ALT and other keys and seems to work for me, but immediately, I'm testing on MacOS. I also found that you can only bind to the "key released" event of the meta keys
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
JMenuBar mb= new JMenuBar();
JMenu menu = new JMenu("Menu");
menu.add("This is test item");
mb.add(menu);
frame.setJMenuBar(mb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
public TestPane() {
setLayout(new GridBagLayout());
label = new JLabel("...");
add(label);
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "A");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "W");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "S");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "D");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true), "ALT");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0, true), "CTRL");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_META, 0, true), "META");
ActionMap actionMap = getActionMap();
actionMap.put("A", new TextAction("A"));
actionMap.put("W", new TextAction("W"));
actionMap.put("S", new TextAction("S"));
actionMap.put("D", new TextAction("D"));
actionMap.put("ALT", new TextAction("ALT"));
actionMap.put("CTRL", new TextAction("CTRL"));
actionMap.put("META", new TextAction("META"));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected class TextAction extends AbstractAction {
private String text;
public TextAction(String text) {
this.text = text;
}
#Override
public void actionPerformed(ActionEvent e) {
label.setText(text);
}
}
}
}
As an alternative, you could also look at KeyboardFocusManager.addKeyEventDispatcher and/or Toolkit.addAWTEventListener to see if you can determine when the ALT key is pressed and either force focus back to the component OR, better, pause the game - as there might be a legitimate reason the user wants access to the system menu.
To that end, a FocusListener might be a simpler solution all round, as you can simply determine when focus is lost or gained and pause/resume the game appropriately
Related
This is the code for my JPopupMenu and how I added it, it is supposed to respond when I right click the table:
JMenuItem deleteRows = new JMenuItem("Delete Row");
popup.add(deleteRows);
personTable.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3) {
popup.show(personTable, e.getX(), e.getY());
}
}
});
I am not sure why the popup menu is not appearing when I right-click the table in the application. I would appreciate it if somebody told me what I am doing wrong.
The trigger for a popup is different for different OS's, you can't simply use mousePressed and your certainly shouldn't be using e.getButton() == MouseEvent.BUTTON3
From How to use Menus, Bringing up a PopupMenu
The exact gesture that should bring up a popup menu varies by look and feel. In Microsoft Windows, the user by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component that is popup-enabled. In the Java look and feel, the customary trigger is either pressing the right mouse button (for a popup that goes away when the button is released) or clicking it (for a popup that stays up).
Instead, you should be checking for each of the mouse events, pressed, released and clicked. You should also be using MouseEvent#isPopupTrigger to determine if the event is a popup trigger for the OS.
Having said all that, it would be simpler to just us JComponent#setComponentPopupMenu and let it decide instead
personTable.setComponentPopupMenu(popup);
Runnable example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(10, 10);
JTable table = new JTable(model);
JMenuItem mi = new JMenuItem("I'll be your menu for today");
JPopupMenu popup = new JPopupMenu();
popup.add(mi);
table.setComponentPopupMenu(popup);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Here is the simple way to create a popupMenu without using mouse listener:
JpopupMenu popMenu = new JpopupMenu() ;
JMenuItem item = new JMenuItem("my item") ;
popMenu.add(item);
myTable.setComponentPopupMenu(popMenu);
I am trying to do a small app that compares two similar texts contained in 2 JTextarea. I am wondering if it's possible to select text from the first JTextarea and automatically select the text on the second JTeaxtarea (lets consider that it's guarantee that the 2 JTextarea will have the same text for now) ?
Should I share events or listeners ?
Thank you
This would be so much easier if JTextComponent supported a selection model...
Basically, what you can do is attach a ChangeListener to the JTextArea's Caret and monitor for changes to the Caret, changing the selection of the other JTextArea in response...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class MirrorTextSelection {
public static void main(String[] args) {
new MirrorTextSelection();
}
public MirrorTextSelection() {
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 JTextArea left;
private JTextArea right;
private DefaultHighlighter.DefaultHighlightPainter highlightPainter;
public TestPane() {
highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(UIManager.getColor("TextArea.selectionBackground"));
left = new JTextArea(20, 20);
left.setWrapStyleWord(true);
left.setLineWrap(true);
right = new JTextArea(20, 20);
right.setWrapStyleWord(true);
right.setLineWrap(true);
left.setText("I am trying to do a small app that compares two similar texts contained in 2 JTextarea. I am wondering if it's possible to select text from the first JTextarea and automatically select the text on the second JTeaxtarea (lets consider that it's guarantee that the 2 JTextarea will have the same text for now) ? Should I share events or listeners ? Thank you");
right.setText("I am trying to do a small app that compares two similar texts contained in 2 JTextarea. I am wondering if it's possible to select text from the first JTextarea and automatically select the text on the second JTeaxtarea (lets consider that it's guarantee that the 2 JTextarea will have the same text for now) ? Should I share events or listeners ? Thank you");
setLayout(new GridLayout(0, 2));
add(new JScrollPane(left));
add(new JScrollPane(right));
left.getCaret().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
int dot = left.getCaret().getDot();
int mark = left.getCaret().getMark();
right.setCaretPosition(mark);
right.moveCaretPosition(dot);
}
});
}
}
}
Now, when you run this, you will find that the right side doesn't seem to get highlighted...what?!
The selection is changing, it's just not been rendered because the component doesn't have focus...
Instead, you could use a Highlighter to highlight the text...
private DefaultHighlighter.DefaultHighlightPainter highlightPainter;
//...
highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(UIManager.getColor("TextArea.selectionBackground"));
left.getCaret().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
int dot = left.getCaret().getDot();
int mark = left.getCaret().getMark();
right.getHighlighter().removeAllHighlights();
try {
int start = Math.min(dot, mark);
int end = Math.max(dot, mark);
right.getHighlighter().addHighlight(start, end, highlightPainter);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
});
Okay, this is now working and you can control the background color of the highlight...
There is another alternative...We can replace the Caret of the right JTextArea with one that doesn't hide the selection when focus is lost...
public class HighlightCaret extends DefaultCaret {
#Override
public void install(JTextComponent c) {
super.install(c);
setSelectionVisible(true);
}
#Override
public void focusGained(FocusEvent e) {
JTextComponent component = getComponent();
if (component.isEnabled()) {
if (component.isEditable()) {
setVisible(true);
}
setSelectionVisible(true);
}
}
#Override
public void focusLost(FocusEvent e) {
setVisible(false);
}
}
Then we set the Caret to right...
right.setCaret(nwe HighlightCaret());
This means we don't need the Highlighter code, we can stick with the original and we get control over not only the background selection color but also the foreground selection color as well...
i wanted to make a java program for getting keystrokes without focusing and without keyevent class object for my project ..
is there any way to do so..??
i tried this but it is not fulfilling my requirements!!
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.Keymap;
public class key1 {
private static void showUI() {
JFrame jFrame = new JFrame("");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = jFrame.getContentPane();
JTextField txt = new JTextField();
container.add(txt, BorderLayout.NORTH);
ActionListener actListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println(event.getActionCommand() + " selected");
}
};
JPanel jPane = new JPanel();
JButton defaultButton = new JButton("Hit Enter");
defaultButton.addActionListener(actListener);
jPane.add(defaultButton);
JButton otherButton = new JButton("Onother Button");
otherButton.addActionListener(actListener);
jPane.add(otherButton);
container.add(jPane, BorderLayout.SOUTH);
Keymap map = txt.getKeymap();
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
map.removeKeyStrokeBinding(stroke);
jFrame.getRootPane().setDefaultButton(defaultButton);
jFrame.setSize(350, 250);
jFrame.setVisible(true);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
showUI();
}
});
}
}
I don't believe this is possible at the Swing level..the entire KeyEvent API revolves around some Component having focus :(
In fact, I'm pretty sure the JVM only generates events based on what the OS gives to it..and you can only get those events if something is in focus.
However, I've found a library that might do what you want. I have never used it though..just looked up out of curiosity:
http://code.google.com/p/jnativehook/
I have a java application and i want to wait for a key to be pressed to perform another action. Till now i have found:
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT ) {
//Right arrow key code
}
}
but i want this to be inside a loop or something that waits until the key matches.
Please help.
UPDATE:
I want the program to be waiting until the key is pressed and when it is pressed i want to trigger another action. For example:
public void something(){
//do something
wait until a key is pressed
if( key pressed is a arrow key){
something();
}else{
wait for the key to be pressed
}
}
public void dootherthing(){
//do other thing
}
I am working in swing and i don't want the GUI to be irresponsible. i.e. when i call the dootherthing by click of a button. It should do it and the waiting should be over.
Swing (and most GUIs) are event driven environments. That is, something happens, you react to it.
Having a loop waiting for some kind of action is kind of counter intuitive (IMHO).
Generally speaking, you should avoid KeyListeners if you can. They have issues with key focus. The key bindings API has ways to over come this limitation.
It allows you to register a KeyStroke against an Action and allows your program to sit back and wait till something happens...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class KeyBindings {
public static void main(String[] args) {
new KeyBindings();
}
public KeyBindings() {
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 {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
add(new KeyPane(KeyEvent.VK_UP, 0), gbc);
gbc.gridy = 2;
add(new KeyPane(KeyEvent.VK_DOWN, 0), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(new KeyPane(KeyEvent.VK_LEFT, 0), gbc);
gbc.gridx = 2;
add(new KeyPane(KeyEvent.VK_RIGHT, 0), gbc);
}
}
public class KeyPane extends JPanel {
public KeyPane(int keyCode, int modifier) {
setBorder(new LineBorder(Color.DARK_GRAY));
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(keyCode, modifier, false), "keyPressed");
im.put(KeyStroke.getKeyStroke(keyCode, modifier, true), "keyReleased");
am.put("keyPressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
setBackground(Color.RED);
}
});
am.put("keyReleased", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
setBackground(UIManager.getColor("Panel.background"));
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}
}
}
You can write a key listener
I found a Java tutorial that includes a Java WebStart sample and the source code. Looks like the winner is KeyEvent.getKeyLocation()
KeyEvent.KEY_LOCATION_STANDARD
KeyEvent.KEY_LOCATION_LEFT
KeyEvent.KEY_LOCATION_RIGHT
KeyEvent.KEY_LOCATION_NUMPAD
KeyEvent.KEY_LOCATION_UNKNOWN
References:
How to tell which SHIFT key was pressed?
keyPressed method will always be invoked whenever a key is pressed then why do you need it to be inside a loop?
From description it seems you are developing a typing game if it is so you can use Timeline class in JavaFX.
I have developed a very simple typing game in JavaFX have a look at it (code not properly organized).
For a program, I was using a KeyListener to add something to an ArrayList when pressing the button '1'. Objects in this list are being visualised constantly. With the KeyListener, this worked fluently, even when keeping the button pressed.
Later, I added a JMenuBar to the GUI. Adding something to the ArrayList now has an own JMenuItem with its accelerator set to the KeyStroke '1' and an ActionListener which performs the same stuff than the KeyListener before. However, the performance now is very bad. Keeping '1' pressed is going to lag extremely, it's very slow compared to the KeyListener.
Why is it so slow? Am I doing something wrong? Is there a better way?
...
AL al = new AL();
menu.add(createMenuItem("Add", KeyEvent.VK_1, al));
}
private JMenuItem createMenuItem(String text, int key, ActionListener al){
JMenuItem menuItem = new JMenuItem(text);
menuItem.setAccelerator(KeyStroke.getKeyStroke(key, 0));
menuItem.addActionListener(al);
return menuItem;
}
private class AL implements ActionListener{
public void actionPerformed(ActionEvent e){
int keycode = ((JMenuItem)e.getSource()).getAccelerator().getKeyCode();
bla(keycode);
}
}
It looks like the slowdown is how the menu accelerators are handled. It might be L&F or even OS since when I profile it, there is no hotspot in the Java code (WindowsXP) dependent. A workaround could be to add the key binding to the root pane instead of using an menu accelerator.
Press '1' to trigger KeyListener on button (fast)
Press '2' to trigger menu accelerator (slow)
Press '3' to trigger KeyBinding on button (fast)
Press '4' to trigger KeyBinding on root pane (fast)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class TestKeySpeed {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
final JTextArea area = new JTextArea(20, 40);
area.setEditable(false);
JButton button = new JButton("Just something that has focus");
button.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_1) {
area.append("1");
}
}
});
AbstractAction action = new AbstractAction("Add") {
{
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('2'));
}
#Override
public void actionPerformed(ActionEvent e) {
area.append("2");
}
};
button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke('3'), "add3");
button.getActionMap().put("add3", action);
JMenu menu = new JMenu("File");
menu.add(action);
JMenuBar bar = new JMenuBar();
bar.add(menu);
JFrame frame = new JFrame("Test");
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke('4'), "add4");
frame.getRootPane().getActionMap().put("add4", action);
frame.setJMenuBar(bar);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(area));
frame.getContentPane().add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
button.requestFocusInWindow();
}
});
}
}
Something else is slowing your application. This example remains responsive with over a dozen Key Bindings. One useful approach is to let menu items and other components share the same actions, as shown here and here.
Addendum: Instead of implementing ActionListener, implement Action by extending AbstractAction, which will make it easier to manage the accelerator key.