How to use keyboard keys in background when application is minimized in JAVA

I am working in JAVA application which requires that - Get all the keys which is pressed by user in separate txt file. - Get the keys in background when Application is minimized.

I have no idea how to do this currently I am Working in following Code.

public class KeyChecker1 extends JFrame 
{
    public static JLabel keyLabel = new JLabel("Hit any key");

    public KeyChecker1() 
    {
        super("Hit a Key");
        KeyMonitor1 monitor = new KeyMonitor1(this);
        addKeyListener(monitor);

        setFocusable(true);
        setVisible(true);
    }
    public static void main(String[] arguments) 
    {
        new KeyChecker1();
    }
}
class KeyMonitor1 extends KeyAdapter 
{
    KeyChecker1 display;
    public static Thread t1key, t2;
    KeyMonitor1(KeyChecker1 display) 
    {
        this.display = display;

    }
    public void keyPressed(final KeyEvent event) 
    {

        t1key = new Thread()
        {
             @Override
             public void run()
             {
                try 
                {                                
                    boolean caps_lock = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
                    switch (event.getKeyCode()) 
                    {
                        case KeyEvent.VK_F1:
                            System.out.print("[F1]");
                            break;
                        case KeyEvent.VK_F2:
                            System.out.print("[F2]");
                            break;
                        case KeyEvent.VK_F3:
                            System.out.print("[F3]");
                            break;
                        case KeyEvent.VK_F4:
                            System.out.print("[F4]");
                            break;
                        case KeyEvent.VK_F5:
                            System.out.print("[F5]");
                            break;
                        case KeyEvent.VK_F6:
                            System.out.print("[F6]");
                            break;
                        case KeyEvent.VK_F7:
                            System.out.print("[F7]");
                            break;
                        case KeyEvent.VK_F8:
                            System.out.print("[F8]");
                            break;
                        case KeyEvent.VK_F9:
                            System.out.print("[F9]");
                            break;
                        case KeyEvent.VK_F10:
                            System.out.print("[F10]");
                            break;
                                 .
                                 .
                                 .
                      }
                }
                catch (Exception ex) 
                {
                    Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
                }
             }
          };
          t1key.start();
    }
}

It working well when application is running but I want to Run the same functionality when the application is running in background (Minimized).

Please help me for this.

Thanks in advance.

Answers


To answer the question, it is not possible using pure Java. You must implement platform dependent keyboard and mouse hooks using operating system specific API's (like the infamous SetWindowsHookEx on Windows) and then convert the native platform dependent data back to something Java can understand. To simplify this process, I created https://github.com/kwhat/jnativehook as part of my graduate thesis because I needed to solve this issue in a portable way for my undergraduate thesis.


Need Your Help

Are there any examples for how to draw a line chart in Core Plot on the iPhone?

iphone core-plot

Is there some sample code or a tutorial out there about how to draw a line chart using the Core Plot framework? I can't find anything using Google.

How do you interrupt a Semaphore from a JUnit thread?

java multithreading junit semaphore

I have an implementation of a method that I want to test.