0

I'm making a program for a game HUD GUI but java cant get global keystrokes when the window isn't in focus so, and i dont want to do any black magic with jnativeinterface as i need it to work on both Linux and windows. My idea for getting around this was to pipe output from python to java but I'm not entirely sure how to do so.

here's my code so far for reference:

I've tried assigning a java key-listener to a none existent window just because i was curious and it didn't work.

package main;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainThread extends JFrame implements KeyListener{
    private static final long serialVersionUID = 1L;
    public static void main(String[] args) throws Exception {
        System.out.println("Jeffrey was here");
        JFrame win = new JFrame();
        JPanel wind = new JPanel();
        win.add(wind);
        win.setDefaultCloseOperation(EXIT_ON_CLOSE);
        win.setVisible(true);
        win.setLocation(500, 500);
        win.setResizable(false);
        win.setSize(300,50);
        win.setAlwaysOnTop(true);
        win.setTitle("Mouse Cordinates");
        JLabel xCord = new JLabel("");
        JLabel yCord = new JLabel("");
        wind.add(xCord);
        wind.add(yCord);
        while(true) {
            Thread.sleep(30);
            PointerInfo mouse = MouseInfo.getPointerInfo();
            Point poin = mouse.getLocation();
            xCord.setText("X cordinates: " + (int) poin.getX());
            yCord.setText("Y cordinates: " + (int) poin.getY());
        }
    }
    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        int key = arg0.getKeyCode();
        if(key == KeyEvent.VK_W) {
            System.out.print("w pressed");
        }
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
}

i need it to detect when the key is pressed without the window is in focus.

  • Where is your python code? When and how is your python script started? How do the two processes (Java and Python) communicate? – Turing85 Feb 17 '19 at 20:47
  • I haven't written any python yet, the question is more of, is there a better way or is piping from python going to cause problems – Jeffrey Baker Feb 17 '19 at 20:50
  • What do you mean by "piping"? My understanding of piping is the `|` in some shell, which would imply some sort of command line parameters, but I imagine your java application already running. This is why I asked how the processes are communicatiing. To the best of my knowledge, there is no possibility to write some memory in python and the later access that memory from Java (this isn't how the JVM memory model works). You can always communicate through some file or similary, but this would mean some kind of polling... – Turing85 Feb 17 '19 at 20:55

1 Answers1

0

There are already existing libraries that give you this option (i know this is not exactly what you are looking for (python and stuff), but it might be a solution to your problem). Take a look at kristian's system hook code snippet:

public static void main(String[] args) {
    // might throw a UnsatisfiedLinkError if the native library fails to load or a
    // RuntimeException if hooking fails
    GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(true); 
    keyboardHook.addKeyListener(new GlobalKeyAdapter() {
        @Override
        public void keyPressed(GlobalKeyEvent event) {
            System.out.println(event);
            if (event.getVirtualKeyCode() == GlobalKeyEvent.VK_ESCAPE)
                run = false;
        }

        @Override
        public void keyReleased(GlobalKeyEvent event) {
            System.out.println(event);
        }
    });

And of course there is also the JNativeHook option.


Some other already existing questions to take a look:

How to capture global key presses in java

How can I write a key listener to track all keystrokes in Java?

George Z.
  • 6,643
  • 4
  • 27
  • 47