Search code examples
javakeyboardawt

Key event doesn't dispatch


I'm trying to make an autoclicker (with an activating shortcut) with Java but autoclicker toggle shortcut doesn't work.

My code:

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher((e) -> {
  System.out.println(121212); // doesn't work
  if(e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == 74) {
    active = !active;

    new Thread(() -> {
      while(active) {
        try {
          int pre = (1000 / cps + jitterRandomValue / 2) / 3;

          bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
          TimeUnit.MILLISECONDS.sleep(pre + jitterRandom.nextInt(jitterRandomValue));
          bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
          TimeUnit.MILLISECONDS.sleep(pre * 2 + jitterRandom.nextInt(jitterRandomValue));
        } catch(Throwable ex) {
          ex.printStackTrace();
        }
      }
    }).start();

    return true;
  }

  return false;
});

My algorithm holds the mouse button 1/3 of the "clicktime". Keycode 74 is the j key (or it's not?).

EDIT: I think I have to find a keyboard manager that works without focus at my app. If there's not maybe I can use GLFW (with a native interface)?


Solution

  • Ok. Now I'm using com.github.tulskiy:jkeymaster:1.2 and my global shortcut works fine. I think the problem is solved.