How to capture the pressing of a key with the program running in the background

Asked

Viewed 619 times

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 key pressed by the user even with the program running in the background.

Thank you.

2 answers

1

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

I advise you to take a look at API Jnativehook, I used some time ago to develop applications that communicated outside of JVM. This API solves your problem!

-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  
            }                 
        };  
    });  
  • But where do I add the JTextField ??

  • in scope, instantiating

  • 1

    I’m sorry, I couldn’t, I could give you a full example ??

Browser other questions tagged

You are not signed in. Login or sign up in order to post.