I want the active window (i.e JFrame ot JDialog) to receive specific keyEvent so that whereever the focusing this keyevent is handling by the window such as ALT+F4 if you press
it wherever the focusing the active window will close,I try to override postprocesskeyEvent but it doesn't work fine
You can add a global event listener to you application using the addAWTEventListener() method in java.awt.Toolkit.
http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#addAWTEventListener%28java.awt.event.AWTEventListener,%20long%29
You will need to choose which type of events you want to receive with the event mask when you add the listener.
For example:
// Then on startup register.
AWTEventListener myGlobalKeyListener = new MyGlobalKeyListener();
Toolkey.getDefaultToolkit().addAWTEventListener(myGlobalKeyListener, AWTEvent.KEY_EVENT_MASK);
If you are trying to stop windows from shutting down your application when the user presses ALT-F4 then an event handler will not help you. I belive the operating system handles this by sending SIGTERM to the application. Java does not receive the KeyEvent for this.
The standard approach for intercepting KeyStrokes when using Swing is to use Key Bindings. Although as mentioned earlier this still won't work for Alt+F4.
If you are trying to prevent Alt+F4 from closing the window, then you need to use the setDefaltCloseOperation(...) method to do nothing. Closing an Application gives more information on this approach.
Related
The preferences of our application are shared (i.e. all users in an office have the same one). For that to work we have implemented a custom IPreferenceStore that saves and (re-)loads the data to / from a database.
Now of course we can't have the IPreferenceStore reloading while the preference window is open - at the very least we should bring some kind of message in that case or something. So I need to have a way to listen to the PreferenceDialog opening.
Since we're implementing an Eclipse application, it might suffice to listen for Shells opening and checking their title, if it's not possible to react to the preference dialog directly.
What I tried:
// this only reacts to the main window changing
PlatformUI.getWorkbench().addWindowListener(new IWindowListener() { ... });
// shells (and composites in general) are missing listeners for
// adding child controls
Shell currentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
currentShell.addListener(SWT.???, e-> System.out.println(e));
// what listeners do display even have?
Display.getDefault().addListener(SWT.???, e-> System.out.println(e));
Any thoughts?
Display.addFilter for SWT.Activate does give an event as a shell is activated (and a lot of other things as well). For some reason addListener does not work in my test.
Display.getDefault().addFilter(SWT.Activate, listener);
The widget field of the Event contains the control being activated.
I want to understand how java.awt.Dialog achieves modality (blocking other windows)?
Yeah, I tried reading the code of java.awt.Dialog, but I am kind of getting lost in it. So I want to know briefly what they do. Do they disable all events of all other windows?
Thanks
Prem
Its system dependent. On windows the main event loop is modified. You are most of the time better off opening a dialog non-modal and lock the underlying window manually (e.g. set a glasspane to the rootpane and disable it, or block all events setting a custom focus manager).
I tried a addWindowListener and implement the windowClosing, it works, when I press the close button, but when I use Cmd+Q to close, the windowClosing is not being called, how can I solve it? Do I need to detect Cmd+Q on mac, Alt + F4 on windows via key listener? Is that a general listener for closing window, whatever via the close button or keyboard, or event Ctrl+Alt+Delete or Cmd+Option+Esc to focus kill? Thanks.
I'm not sure what the situation is on Macs, but on Windows you get the windowClosing() callback from the close button; Alt-F4; and if you close the app via task manager. You don't get the callback if you use task manager to kill the process, but I wouldn't expect that anyway.
You have remembered to call setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); on your JFrame instance, haven't you?
there is one more method windowClosed()
try overriding thing method. hope it will work for you.
You can use this osx library:
com.apple.eawt.ApplicationListener
handleQuit(ApplicationEvent event)
Will probably do the trick.
Information from the docs:
Called when the application is sent the Quit event. This event is generated when the user selects Quit from the application menu, when the user types Command-Q, or when the user control clicks on your application icon in the Dock and chooses Quit. You can either accept or reject the request to quit.
Of course this solution will not work on Windows. As far as I know there is however no universal solution, so this is probably the best way to go.
Sounds like you need to add some KeyListeners and a factory to detect the one you want for a particular operating system.
Check Out
As you said windowClosing is called when you click the (x) button. I am also on a mac and the way I get the CMD+Q to send a signal to the application is using Runtime.addShutDownHook
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
// code to run when CMD+Q is pressed
}
}
Is it possible to listen for key and mouse events without having a gui selected by the user? I want to make a program that runs in the background without a gui and responds to user interaction such as pressing ctrl-t.
The window manger will track the focus and direct input according to its configuration. If your window manager decides to send input to a different program because it is enforcing a change of focus, you are out of luck.
While the program lacks focus, if it were able to obtain the mouse and keyboard events, it would be a major security hole. Basically any small unnoticeable background program could spy on the entire system, possibly even stealing passwords in the process.
Your only hope is to find an option in your window manager which will allow you to minimize the program without changing focus. Of course, whatever you type will only go the minimized program (and not affect the rest of the system).
Another technique (by no means guaranteed) is for the program to detect minimization events, and to request focus within a minimization event handler. Note that doing so would probably (If I were writing a window manager) unminimize the window.
You probably would like to look at jnativehook.
Jnativehook allows you to listen for keyboard events using JNA without an existing focused GUI.
https://github.com/chrislgarry/Apollo-11
https://github.com/kwhat/jnativehook/blob/2.2/doc/ConsumingEvents.md
Example usage:
try {
GlobalScreen.registerNativeHook()
} catch (NativeHookException ex) {
System.out.println(ex.message)
}
GlobalScreen.addNativeKeyListener(
new NativeKeyListener() {
#Override
public void nativeKeyTyped(NativeKeyEvent nativeEvent) {
System.out.println("Pressed " + nativeEvent.keyChar)
}
}
)
I want to be informed if componentShown/componentHidden event occured. I use ComponentListener, but it doesn't work as good as I need. I have found this information in official Swing tutorial:
The component-hidden and
component-shown events occur only as
the result of calls to a Component 's
setVisible method. For example, a
window might be miniaturized into an
icon (iconified) without a
component-hidden event being fired.
So I supose I have to find another solution. So how to listen every componentShown/componentHidden event?
I don't think you will be able to do this with a single listener.
You can also use a WindowListener to listen for windowIconified.