Java - keyboard state after leaving window - java

The title might be a little misleading, didnt know how to put my problem short.
Basically what im doing is im using keyboardlistener to find out which keys are down and according to that im moving my game character.
The problem is, when you click out of the window, while holding down a key my listener doesnt register the keyReleased event.
I tried to fix it by using mouse listener and the mouseExited event, but that doesnt fix it all the time, sometimes it does sometimes it doesnt.
Heres my implementation:
Keyboard:
public void mouseLeftWindow()
{
for(int i =0;i<KEY_COUNT;i++)
{
keys[i] = false;
}
}
#Override
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = true;
}
}
#Override
public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = false;
}
}
where keys[] is a boolean[] describing, which codes are pressed
mouse:
#Override
public void mouseExited(MouseEvent e)
{
mouseMoved(e);
keyboard.mouseLeftWindow();
}

Your program will listen for further key events even when your mouse exited the component. That means you set everything to false on exit but if a key is still pressed it will be set to true immediately again. I think you are looking for a FocusListener instead of a MouseListener.
addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
}
#Override
public void focusLost(FocusEvent e) {
keyboard.mouseLeftWindow();
}
});

Related

Detect the Windows key modifier

How can I detect the Windows key modifier for KeyEvent? I have add the code:
textField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if ((e.getKeyCode() & KeyEvent.VK_ESCAPE) == KeyEvent.VK_ESCAPE) {
textField.setText("");
}
}
});
But the problem is, when I use the Windows zoom and try to exit from it using Win + Escape, if focus is in TextField, its content clears. I've tried filter by e.getModifiersEx(), but it returns 0. The only way I've found is to detect whether Windows pressed or not, is to create boolean field and change it's value when Windows pressed/released.
So, is there any way to get the Windows key pressure state from KeyEvent for ESCAPE released event?
The way I used for myself:
AbstractAction escapeAction = AbstractAction() {
public void actionPerfomed(ActionEvent e) {
setText("");
}
}
textField.addCaretListener(new CaretListener() {
#Override
public void caretUpdate(CaretEvent e) {
if (textField.getText() == null || textField.getText().isEmpty()) {
textField.getActionMap().remove("escape");
textField.getInputMap().remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
} else {
textField.getActionMap().put("escape", escapeAction);
textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), escapeAction);
}
}
});

Keys & Buttons compatibiliy Java

I have made my own version of Tetris in Java and i have added the possibility of moving the shapes both with JButtons and with certain keyboard keys. The code snippet i used is the following:
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
moveLeft();
}
});
rightButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
moveRight();
}
});
rotateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
rotateMovingShape();
}
});
myPanel.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == event.VK_A)
{
moveLeft();
}
if (keyCode == event.VK_D)
{
moveRight();
}
if (keyCode == event.VK_S)
{
rotateMovingShape();
}
}
});
The problem i have is that after i use the JButtons, i cannot longer control the shapes with the keyboard keys. I suspect it has something to do with gaining/losing focus, but i am not sure. Could anyone tell me what's going on?
You get that problem, because you use KeyListener, instead of that you need to use Key Bindings. For example:
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A,0), "aPressed");
component.getActionMap().put("aPressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("a key");
}
});
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0), "dPressed");
component.getActionMap().put("dPressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("d key");
}
});
// other bindings
where component is your JPanel.
A KeyListener only receives key events when the component has the keyboard focus. Clicking on the buttons transfers the focus to them and away from your panel, so you don't get the events. Any one of the following approaches will solve this:
Call setFocusable(false); on the buttons so that they won't steal the focus.
Add the KeyListener to the buttons too.
Use key bindings instead of a KeyListener, so that you can catch the key press whether the component has focus or not.

KeyListener only sometimes works

I am writing a game and I have just tried to add the KeyListener. I have experience with java including KeyListeners but I for some reason cannot figure out why this code only works some of the time.
Here is my listener function:
public void Listener() {
System.out.println("[INFO] Listener() Ran.");
KeyListener kl = new KeyListener() {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()=='a'){
System.out.println("[DEBUG] A Pressed.");
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
};
panel.addKeyListener(kl);
System.out.println("[DEBUG] panel added KeyListener.");
}
This code works probably only 1 out of 10 times that I run it. Maybe even less. Any ideas on why this is?
The getKeyChar should be called in the keyTyped. The getKeyCode() == KeyEvent.VK_A in the other both methods.

How to select the cell on mouse click and shift in JGrid similar to (JTable)?

I have the ListSelectionListener which tells me when the cell is selected with the mouse.
JGrid grid = new JGrid();
grid.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
grid.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(final ListSelectionEvent e) {
e.getFirstIndex();
e.getLastIndex()
}
}
I want to select the sell only when the button shift is hold. How can i do it?
I need it for the multiple selection. When the user holds shift and clicks the cells it gives me getFirstIndex() and getLastIndex().
Add a KeyListener similar to this to your JGrid, assuming JGrids takes keyListeners
boolean shiftIsDown = false;
yourJGrid.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode == e.VK_SHIFT) shiftIsDown = true;
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode == e.VK_SHIFT &&
shiftIsDown == true) shiftIsDown = false;
}
public void keyTyped(KeyEvent e)
{
// nothing
}
});
Now when you get a valueChanged() event, just check to see if your boolean "shiftIsDown" value is true, and if so you can do your selection.

Showing/hiding a JPopupMenu from a JButton; FocusListener not working?

I needed a JButton with an attached dropdown style menu. So I took a JPopupMenu and attached it to the JButton in the way you can see in the code below. What it needs to do is this:
show the popup when clicked
hide it if clicked a second time
hide it if an item is selected in the popup
hide it if the user clicks somewhere else in the screen
These 4 things work, but because of the boolean flag I'm using, if the user clicks somewhere else or selects an item, I have to click twice on the button before it shows up again. That's why I tried to add a FocusListener (which is absolutely not responding) to fix that and set the flag false in these cases.
EDIT: Last attempt in an answer post...
Here are the listeners: (It's in a class extending JButton, so the second listener is on the JButton.)
// Show popup on left click.
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
}
});
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (isShowingPopup) {
isShowingPopup = false;
} else {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
isShowingPopup = true;
}
}
});
I've been fighting with this for way too long now. If someone can give me a clue about what's wrong with this, it would be great!
Thanks!
Code:
public class Button extends JButton {
// Icon.
private static final ImageIcon ARROW_SOUTH = new ImageIcon("ArrowSouth.png");
// Unit popup menu.
private final JPopupMenu menu;
// Is the popup showing or not?
private boolean isShowingPopup = false;
public Button(int height) {
super(ARROW_SOUTH);
menu = new JPopupMenu(); // menu is populated somewhere else
// FocusListener on the JPopupMenu
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
}
});
// ComponentListener on the JPopupMenu
menu.addComponentListener(new ComponentListener() {
#Override
public void componentShown(ComponentEvent e) {
System.out.println("SHOWN");
}
#Override
public void componentResized(ComponentEvent e) {
System.out.println("RESIZED");
}
#Override
public void componentMoved(ComponentEvent e) {
System.out.println("MOVED");
}
#Override
public void componentHidden(ComponentEvent e) {
System.out.println("HIDDEN");
}
});
// ActionListener on the JButton
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (isShowingPopup) {
menu.requestFocus();
isShowingPopup = false;
} else {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
isShowingPopup = true;
}
}
});
// Skip when navigating with TAB.
setFocusable(true); // Was false first and should be false in the end.
menu.setFocusable(true);
}
}
Here's a variant of Amber Shah's "big hack" suggestion I just made. Without the isShowingPopup flag...
It's not bulletproof, but it works quite well until someone comes in with an incredibly slow click to close the popup (or a very fast second click to reopen it...).
public class Button extends JButton {
// Icon.
private static final ImageIcon ARROW_SOUTH = new ImageIcon("ArrowSouth.png");
// Popup menu.
private final JPopupMenu menu;
// Last time the popup closed.
private long timeLastShown = 0;
public Button(int height) {
super(ARROW_SOUTH);
menu = new JPopupMenu(); // Populated somewhere else.
// Show and hide popup on left click.
menu.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
timeLastShown = System.currentTimeMillis();
}
#Override public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {}
#Override public void popupMenuCanceled(PopupMenuEvent arg0) {}
});
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if ((System.currentTimeMillis() - timeLastShown) > 300) {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
}
}
});
// Skip when navigating with TAB.
setFocusable(false);
}
}
As I said in comments, that's not the most elegant solution, but it's horribly simple and it works in 98% of the cases.
Open to suggestions!
Here is another approach which is not too bad of a hack, if not elegant, and which, as far as I could tell, works. First, at the very top, I added a second boolean called showPopup.
The FocusListener has to be as follows:
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
isShowingPopup = true;
}
});
The isShowingPopup boolean does not get changed anywhere else--if it gains focus, it assumes it's shown and if it loses focus, it assumes it isn't.
Next, the ActionListener on the button is different:
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (showPopup) {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
menu.requestFocus();
} else {
showPopup = true;
}
}
});
Now comes the really new bit. It's a MouseListener on the button:
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
System.out.println("ispopup?: " + isShowingPopup);
if (isShowingPopup) {
showPopup = false;
}
}
#Override
public void mouseReleased(MouseEvent e) {
showPopup = true;
}
});
Basically, mousePressed gets called before the menu loses focus, so isShowingPopup reflects whether the popup was shown before the button is pressed. Then, if the menu was there, we just set showPopup to false, so that the actionPerformed method does not show the menu once it gets called (after the mouse is let go).
This behaved as expected in every case but one: if the menu was showing and the user pressed the mouse on the button but released it outside of it, actionPerformed was never called. This meant that showPopup remained false and the menu was not shown the next time the button was pressed. To fix this, the mouseReleased method resets showPopup. The mouseReleased method gets called after actionPerformed, as far as I can tell.
I played around with the resulting button for a bit, doing all the things I could think of to the button, and it worked as expected. However, I am not 100% sure that the events will always happen in the same order.
Ultimately, I think this is, at least, worth trying.
You could use the JPopupMenu.isVisible() instead of your Boolean variable to check the current state of the popup menu.
Have you tried adding a ComponentListener to the JPopupMenu, so that you know when it's been shown and hidden (and update your isShowingPopup flag accordingly)? I'm not sure listening for focus changes is necessarily the right approach.
What you need is a PopupMenuListener:
menu.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
System.out.println("MENU INVIS");
isShowingPopup = false;
}
#Override
public void popupMenuCanceled(PopupMenuEvent arg0) {
System.out.println("MENU CANCELLED");
isShowingPopup = false;
}
});
I inserted this into your code and verified that it works.
Well, I can't be sure without seeing all of your code, but is it possible that the popup never actually gets focus at all? I've had problems with things' not getting focus properly in Swing before, so it could be the culprit. Try calling setFocusable(true) on the menu and then calling requestFocus() when you make the menu appear.
I tried the Answer of Tikhon Jelvis (introducing a smart combination of focusListener and mouseListener). It does not work for me on Linux (Java7/gtk). :-(
Reading http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocus%28%29 there is written "Note that the use of this method is discouraged because its behavior is platform dependent."
It may be that the order of listener calls changed with Java7 or it changes with GTK vs Windows. I would not recommend this solution if you want to be platform independent.
BTW: I created a new account on stackoverflow to give this hint. It seems I am not allowed to comment to his answer (because of reputation). But it seems I have a button to edit it. This stackoverflow is a very funny thing. :-)

Categories

Resources