Mouse Listener outside a JPanel? - java

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?

Related

Java Swing Key Handlers sporadically not working

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

make Java GUI with many interactive image buttons

I am attempting to create a a java GUI for use on Zedboard with the 7" touchscreen display. The GUI I am creating is supposed to mimic exactly (though scaled down) a physical console with many interactive buttons.
My question is what would be the best method in making the buttons interactive, my first thought was to cut out the buttons of the console and have each one a separate image that can be set as interactive, but I feel there may be another simpler method.
Thanks
LDY
For the console mimicking, you could take an image of the entire console and then listen to touch event at specific points in the image which corresponds to a button. Based on where the touch event occurs, you could do different actions.
For this you need to get the coordinates of the touch event and check if it corresponds to any of the buttons on the console.

How would I listen for a LongClick on an overlay item in a Google MapView?

I'm a novice Android monkey and I've hit a snag trying to implement a feature on my app. What I want to do is have my users be able to long click an overlay item (it's a bus stop) and add that marker (stop number and address) to their favorites tab. The problem is... how do I listen for and handle a long click(tap) on an overlay item??
Currently I'm #overriding the onTap function in my Overlay class to handle regular taps, but I don't see an onLongTap function to override in the documentation... Can anyone set me along the right path? Am I missing something obvious here? Thanks for reading this
P.S. This is my first SO question :-)
You probably want to override onTouchEvent(MotionEvent, MapView) for more complex user interactions. For a 'long' tap, you should start some sort of timer that will indicate how long the user has been pressing on a specific Overlay item. Once a certain threshold is reached - I believe the default delay for a 'long' press is roughly 1500 ms - you then execute the relevant code.
Alternatively, you could take a look at including the mapview-overlay-manager project, which, from the reads of it, should offer exactly what your looking for (and then some):
OnOverlayGestureListener
Simplified OnGestureListener. A ManagedOverlayer uses its own build-in GestureDetector that fires
events like:
onSingleTap(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)
onDoubleTap(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)
onLongPress(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)
onZoom(ZoomEvent, ManagedOverlay) onScrolled(...)

Capturing events in X11 even after the loss of focus

I am trying to develop an application which responds to multiple digital pens (IRIS Pens) so that if any of the pen writes on paper; I relay the output to a single screen. Thus making a multi-input whiteboard for myself.
In Ubuntu these pens are recognized as mouse and thus can be handled in a similar manner as mouse events are handled.
So now what I plan to do is to handle these events in C/C++ using XLib and pass these events to a Java Swing application using JNI callback. I am able to do this but when the X11 window looses focus no events are transferred to the Swing frame. I also tried to use the root window in X11 but it does not seem to work.
Any help would be really appreciated. Thanking you in advance.
How about maximizing the C/X11 window in front of the Java one, and making it transparent? You should be able to see the Java window while still focusing on the C/X11 one.
Since you are using Ubuntu, you can achieve this using the "Opacity, brightness and saturation" plugin for Compiz. It is in the compiz-plugins-main package, and you can activate it with Compiz Settings Manager (from the compizconfig-settings-manager package). When you activate the plugin, alt+wheel is bound by default to change the transparency of the focused window.
Try reading the mouse directly. I don't remember the exact location, but you should find it in something like '/dev/input/mouseX', where X is the number of your device, ranging from 0 to n-1 devices..
When you read the packet, your application should block until the mouse moves and then your read function will return a raw mouse packet which describes the delta (which is probably more useful then the screen coordinates, in your case) and the mouse button statuses.
The raw packet can be decoded as described here: http://www.computer-engineering.org/ps2mouse/
Create a modal dialog and set it to XmDIALOG_SYSTEM_MODAL (the actual name of the property depends on your toolkit: Motif, Gtk, Qt, ...). Dialogs like this block the whole display and can never loose focus.
The drawback is of course that you can't do anything else while this dialog is on the screen.
This entry in the X11 FAQ might help.

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