Java Swing Key Handlers sporadically not working - java

I am developing a Java Swing based video game. I am having a problem with detecting keystrokes- the 'A' (move left) and 'D' keys (move right), where it almost seems they are not firing (but happens rarely). Almost as if they have become unregistered. Now, I have setup log files to capture the keyboard events, and again one of the keyHandlers at some point does not fire. Now when this does happen, if I click into the main screen of the game with my mouse, the keyHandler that somehow got lost seems to work again. Almost as if the "Focus" was inadvertantly transferred to some other widget in the game.
Here is the code to setup the Action Maps:
topStatusPanel.getInputMap().put(KeyStroke.getKeyStroke("pressed A"),
"aCharKeyPressed");
topStatusPanel.getActionMap().put("aCharKeyPressed", aKeyActionPressed);
topStatusPanel.getInputMap().put(KeyStroke.getKeyStroke("released A"),
"aCharKeyReleased");
topStatusPanel.getActionMap().put("aCharKeyReleased", aKeyActionReleased);
topStatusPanel.getInputMap().put(KeyStroke.getKeyStroke("pressed D"),
"dCharKeyPressed");
topStatusPanel.getActionMap().put("dCharKeyPressed", dKeyActionPressed);
topStatusPanel.getInputMap().put(KeyStroke.getKeyStroke("released D"),
"dCharKeyReleased");
topStatusPanel.getActionMap().put("dCharKeyReleased", dKeyActionReleased);
MoveSubLeftActionStart aKeyActionPressed= new MoveSubLeftActionStart();
MoveSubLeftActionStop aKeyActionReleased= new MoveSubLeftActionStop();
MoveSubRightActionStart dKeyActionPressed= newMoveSubRightActionStart();
MoveSubRightActionStop dKeyActionReleased= new MoveSubRightActionStop();
Would really appreciate any ideas on this. Is there a way to force focus to a specific widget?- would I then for every other widget in the game have to set it to "unfocusable?" Could this be the issue? Appreciate any help here.

Here's an in-depth treatment on focus management and keyboard event handling.
If your JPanel contains other components, they might get the focus and not propagate the key events up the component chain.
http://docs.oracle.com/javase/6/docs/api/java/awt/doc-files/FocusSpec.html#RequestingFocus

Related

Mouse Listener outside a JPanel?

I have perused other's questions and i don't think the subject i'm about to ask for is mentioned... Here is my problem:
I have an idea for a project in Java that has a mouse listener but for actions outside a panel or any gui. The basic idea is that the application when it's running listens for double clicks and then trigger the webcam, takes a photo and then send it to an e-mail... I think that triggering the webcam, taking the photo and sending to an e-mail is doable, but i have no idea if the mouse listener I'm looking for is plausible.
So, to sum it up: Is it possible to have a mouse listener that listens to actions that are not happening on a application per se? If so, what is the function i'm looking for?

lwjgl double-click mouse events

I'm currently teaching myself LWJGL's mouse class, but there is something I still don't know how to do. I want to be able to handle double-click mouse events, but I don't know how.
It is not (AFAIK) natively supported. You have to do it yourself.
One way to do that : memorize the event date (System.curentTimeMS() or equivalent).
If the previous clic event was recent (~200 ms), it's a double-clic. else, simple clic.
Hope it helps...

MouseEvents in JFrame seem extremely unpredictable

I'm trying to code a level creator for my game in which you drag objects to their desired positions, but I'm having trouble figuring out whether or not the mouse button is being held down.
I'm using a MouseAdapter to listen for mouseClicked and mouseReleased events, but they seem to fire unpredictably. Usually, the program doesn't register either the mouse being clicked or released, but on occasion, one will be fired when it shouldn't be. A SOE will be thrown here and there, and eventually, they will be thrown repeatedly until the program is terminated. Any suggestions on working around this?
I think you're probably listening for the wrong events. MOUSE_CLICKED means MOUSE_PRESSED + MOUSE_RELEASED.
I think you probably want to be looking for MOUSE_PRESSED instead of clicked.
See the api for MouseEvent for more details: http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html
Also, the MOUSE_DRAGGED event may be of some use to you.

JPanel not listening to key event when there is a child component with JButton on it

I'm working on a map editor for my college project. And I had a problem that the map panel is not listening key event while it should.
This happens when I add a ToolBarPane (which extends JPanel) with JComponent such as JButton, JComboBox that implements ActionListener on it AND the map panel (which extends the JPanel) together on to the Frame (I used BorderLayout). I have System.out.println statement to test if the key press is received, and it's not printing, if I remove the ToolBar, the key listener works again, so is the mouseListenner is disabled just like the keyListener, which means I can't handle press events etc, but the mouseListener works fine and I can still handle mouse move event.
Here is a screen shot how it works without the ToolBarPane
http://img684.imageshack.us/img684/3232/sampleku.png
note that you can use the mouse to put images on the map, you can also select images using the mouse just like a laser tool, and by pressing number key you can switch between different images, this works fine until I add the ToolBarPane which shows like this:
img291.imageshack.us/img291/8020/failve.png
(please add http before that, I can only post one hyperlink)
(I can't post images here because I'm a new user)
With the ToolBarPane on I was no longer able to handle the key event.
I guess it might by that the focus as been transferred to that panel somehow, but not sure at all.
Does and body know this and can help me out?
Thanks very much
I suggest you use the InputMap and WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or something similar. Excerpt from How to Use Key Bindings:
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
The component contains (or is) the component that has the focus. This input map is commonly used for a composite component
That has worked very robustly for me. Have a look at my other post for more information and actual code examples:
Keyboard input for a game in Java
or this tutorial:
Swing: Understanding Input/Action Maps
You should NOT be using a KeyListener.
Swing was designed to use Key Bindings which is far more flexible. Check out my quick summary of Key Bindings which also includes a link to the Swing tutorial which conains far more detail.
(I can't post images here because I'm a new user)
An image doesn't help much anyway. If you need more help post your SSCCE which shows the problem (after trying the above suggestion).

JAVA-I don't want clicking on my app make other apps lose focus

Wow, what a stupid question you might say. But is it possible? I have a mouse move event in a Jpanel and it works even when the application is not in focus, now can I have something like that for the clicking event or something similar. And most importantly I don't want other apps (even something like the main menu) to lose focus when i click on my app.
I thought of the system's event queue but i'm not sure where that would lead me to.
Thanks in advance.
[EDIT - the purpose]
I want to create an app that mimics the users interactions with the system in a later time. for example a user takes the mouse and clicks and writes and my app will mimic that in say 2 hours time. ofcourse i would need a system hook for the outer events but i wanted to avoid os-dependant code so i basically capture the screen, take it to my app,for example the user clicks on an icon in the captured picture and then for making that come to life, i translate the coordinates to the real icon and click it (with a Robot) and in this way i can capture the user's events in my own app. the problem occurs when the user clicks on the main menu or right clicks (he's doing that in my app, and my app does that to the system so my app is in between) and ofcourse the real main menu will lose focus when the user tries to click on one of it's items.
sorry for my english.
I'm still not sure I follow what you are trying to do. But the concept of an app getting focus when you click on it is fundamental to the GUI and I suspect rather difficult to get around.
I just found this:
Focusable Windows
To support palette windows and input methods, client code can prevent a Window from becoming the focused Window. By transitivity, this prevents the Window or any of its descendants from becoming the focus owner. Non-focusable Windows may still own Windows that are focusable. By default, every Frame and Dialog is focusable. Every Window which is not a Frame or Dialog, but whose nearest owning Frame or Dialog is showing on the screen, and which has at least one Component in its focus traversal cycle, is also focusable by default. To make a Window non-focusable, use Window.setFocusableWindowState(false).
In this doucment http://java.sun.com/j2se/1.5.0/docs/api/java/awt/doc-files/FocusSpec.html
That sounds like it might do what you want.

Categories