Hello.

I'm trying to write an application, which after pressing a key does a particular action. The thing is I need the application to receive key events even if JFrame isn't itself focused (is iconified).

I've read somewhere that it cannot be done - is that true?


If it is impossible.. is possible for window to get permanent focus - when it loses focus, it gains it back. (simple overriding method isFocusOwner didn't help :P )


Regards, Zibo.

I don't think this is possible, but if you find a way please do post it here!

some tricky, but looks like iconified

package demo;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

class BadPixelFrame extends JFrame implements KeyListener {

    public BadPixelFrame(String filename, int waitTime) {
        setUndecorated(true);
        getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        setAlwaysOnTop(true);
        JLabel l = new JLabel(new ImageIcon(filename));
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
        addKeyListener(this);
        addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                setVisible(false);
                dispose();
            }
        });
        final int pause = waitTime;
        final Runnable closerRunner = new Runnable() {

            public void run() {
                setVisible(false);
                dispose();
            }
        };
        Runnable waitRunner = new Runnable() {

            public void run() {
                try {
                    Thread.sleep(pause);
                    SwingUtilities.invokeAndWait(closerRunner);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        setVisible(true);
        Thread th = new Thread(waitRunner, "th");
        th.start();
    }

    public void keyTyped(KeyEvent e) {
        System.out.println("                        keyTyped " + e);
    }

    public void keyPressed(KeyEvent e) {
        System.out.println("                      keyPressed " + e);
    }

    public void keyReleased(KeyEvent e) {
        System.out.println("                     keyReleased " + e);
    }

    public static void main(String args[]) {
        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

            public void eventDispatched(AWTEvent event) {
                System.out.println("AWTEventListener:eventDispatched " + event);
            }
        }, -1);//AWTEvent.KEY_EVENT_MASK
        String filename = "c:\\1pixelx1pixel.gif";
        int waitTime = 60000;
        new BadPixelFrame(filename, waitTime);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.