How to Capture a Keystroke with the Program Running in the Background

6

The code I have so far is this:

public static void main(String[] args) 
{
    SystemTray tray = SystemTray.getSystemTray();  
    Image image = new BufferedImage(10, 10, 10);
    String tooltip = "Oi amiguinho";  
    PopupMenu menu = new PopupMenu("Pop Up");  

    TrayIcon icon = new TrayIcon(image, tooltip, menu);  
    try 
    {
        tray.add(icon);
    } 
    catch (AWTException e) 
    {
        e.printStackTrace();
    } 


}

And I would like to know how I can capture a user-pressed key even with the program running in the background.

Thank you.

    
asked by anonymous 28.08.2015 / 21:18

2 answers

1

When developing an application using Java's "native" events, it does not recognize when it is outside the JVM. That is, running in the background.

I'd advise you to take a look at the JNativeHook API , I used for some time to develop applications that communicated outside the JVM. This API solves your problem!

    
06.09.2015 / 22:01
-4
    seuJTextField.addKeyListener(new KeyAdapter() {  
        public void keyPressed(java.awt.event.KeyEvent e) {  
            if(e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {  
                //Aqui é seu código  
            }                 
        };  
    });  
    
28.08.2015 / 21:56