2

I am trying to make a hotkey program that runs in the background, and I can not figure out how to make it check when a certain key is pressed (In this case the minus key). I need the program to wait for a key to be pressed and then the program checks if that key is the minus key.

An example of my program as is now in pseudo code is somewhere along these lines.

while(true)
    {
        int pressedKey = userInputedKey;
        if(pressedKey == KeyEvent.VK_MINUS)
        {
            //action to be executed
        }
    }

I have found a lot of suggestions on other threads to use KeyListener but I might not be using it properly, so if your response is to use KeyListener please explain how to use it thoroughly (to be Exact I don't understand how when a KeyEvent is instantiated it already has the input of whatever key was pressed as the keyCode in that KeyEvent, so if I use it in the call to the KeyListener I will not get the proper results). Granted I do not know much about KeyEvent or KeyListener, so I might have not been attempting at the right way around my problem by using a KeyListener.

loserlobster
  • 23
  • 1
  • 5
  • Please refer to [How do I check if the user is pressing a key?](http://stackoverflow.com/questions/18037576/how-do-i-check-if-the-user-is-pressing-a-key) Or [How to detect if a key was pressed in Java?](http://stackoverflow.com/questions/23841127/how-to-detect-if-a-key-was-pressed-in-java) –  Dec 25 '15 at 07:03
  • How do I check if the user is pressing a key? is the one of the thread I had been using for trying to solve my problems, though the top answer did not solve his problem (as shown by the comments of the person who asked the question). – loserlobster Dec 25 '15 at 07:03
  • Something has to intercept the key event and it has to have the focus, so it can't run the in background. Imagine you have 5 programs open and you give the focus to one. You can't have all 5 intercept key events, only the one in focus. – user1803551 Dec 25 '15 at 10:23
  • Also, don't repeat the question in the comments, writing it once is enough. – user1803551 Dec 25 '15 at 10:24

2 Answers2

1

You can use the KeyEvent to get the key code, and then check it.

E.g:

someCompunent.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_MINUS) {
           // do the stuff
        }
    }
});
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • With the way I have my program designed as of now I have no component to addKeyListener(). The program as shown is pretty much all the code I have for it currently. What component should I use, or should I use one at all? – loserlobster Dec 25 '15 at 07:20
  • No I am trying to have it run as a background process so that it will work on all programs. – loserlobster Dec 25 '15 at 07:30
  • Where the program is going to be run, you said it is not in JFrame, then where? inside IDE? when you run the program there must something to run in. – Bahramdun Adil Dec 25 '15 at 07:33
  • As of right now I use he windows cmd to run the program, but my goal was when I was finished with it to create a jar file to run it off of. – loserlobster Dec 25 '15 at 07:42
  • I got it. But KeyListener is only for Swing, in the command line you cannot use this way. But you can use, the `BufferedReader in = new BufferedReader(new InputStreamReader(System.in));` and check for every input from user, you can try it if solved your problem. Good Luck! – Bahramdun Adil Dec 25 '15 at 07:56
1

If you want to use global hotkeys, when program catch keypress event in any application - try use JKeyMaster library (supports global hotkweys in Windows, Linux and MAC OS)

Javasick
  • 2,753
  • 1
  • 23
  • 35