-1

I'm creating a small person program that I want to run in the background until I press Ctrl+Shft+L

It's a JFrame window and I have setVisible(false) when something happens. After it's no longer visible, I need it to listen for the above keys and then display the JFrame again.

I've tried KeyEventDispatcher and even JNativeHook and both didn't work.

How do I listen to keys in the background?

Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • Why did ``KeyEventDispatcher`` and JNativeHook not work? – Binkan Salaryman Jul 07 '15 at 15:37
  • A KeyEventDispatcher will never work since Java doesn't have access to the KeyEvents if the window does not have focus. – camickr Jul 07 '15 at 15:40
  • 1
    If you are trying to listen to the keys while your Java program does not have focus, then I don't believe there is really any good way of doing this in java. Without some sort of plug-in or OS API, your best bet would be C++ or another language that is much closer to the OS level and can possibly link into OS key handlers – AngelTrs Jul 07 '15 at 15:41
  • @Haloboy Therefore it is important to know why JNativeHook didn't work – Binkan Salaryman Jul 07 '15 at 15:42
  • [Don't repost your question](http://meta.stackexchange.com/questions/7046/how-do-i-get-attention-for-old-unanswered-questions) – Binkan Salaryman Jul 07 '15 at 15:43
  • what OS you running? – AngelTrs Jul 07 '15 at 15:47
  • @BinkanSalaryman I'm not sure why it didn't work. I basically used their example code and it just didn't do anything. As for reposting it, they are two different questions. – Spedwards Jul 07 '15 at 17:03
  • I'd be use SystemTray with JPopupMenu or by add ActionListener to TrayIcon – mKorbel Jul 07 '15 at 18:36

1 Answers1

-3

I would suggest using a KeyListener. Here is the documentation: http://docs.oracle.com/javase/7/docs/api/

Falco
  • 20
  • 2
  • Java can only listen to KeyEvents when the window has focus. If the window did have focus you should use `Key Bindings`, not a KeyListener. (1-). – camickr Jul 07 '15 at 15:39
  • A ``KeyListener`` doesn't work ouside a window and the documentation is at http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html – Binkan Salaryman Jul 07 '15 at 15:40