2

I got a simple hotkey program for linux. Now when the program is highlighted it works perfectly but when i click on another app the program does not respond.Is there a way the program always respond even when it runs in background ?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ahkkeys extends JFrame {
 /**
   * 
   */
 private static final long serialVersionUID = 7564262221986249595L;
 /**
   * 
   */
 Robot r;

 public ahkkeys() {
  createAndShowGUI();
 }

 private void createAndShowGUI() {
  setTitle("Move Cursor with Keyboard");
  setDefaultCloseOperation(EXIT_ON_CLOSE);

  // hide the visibility
  setUndecorated(true);
  setOpacity(1);
  setVisible(true);

  // Create Robot object
  try {
   r = new Robot();
  } catch (Exception e) {
  }

  addKeyListener(new KeyAdapter() {
   public void keyPressed(KeyEvent e) {
    if (r == null)
     return;
    Point p = MouseInfo.getPointerInfo().getLocation();

    switch (e.getKeyCode()) {
    // 2::Click right
    case KeyEvent.VK_2:
     r.mousePress(MouseEvent.BUTTON3_MASK);
     r.mouseRelease(MouseEvent.BUTTON3_MASK);
                    break;
     // move down
    case KeyEvent.VK_3:
     r.mouseMove(p.x, p.y + 55);
                    break;
    // left click
    case KeyEvent.VK_4:
     r.mousePress(MouseEvent.BUTTON1_MASK);
     r.mouseRelease(MouseEvent.BUTTON1_MASK);
    }
   }
  });
 }

 public static void main(String args[]) {
  new ahkkeys();
 }
}
Steve Put
  • 21
  • 1

2 Answers2

0

Java does not provide this functionality completely.

Although, setting alwaysOnTop might do the work for you — not sure what you're trying to achieve.

If you need to get "deeper", you will have to use a library. Unfortunately, those libraries are not platform-independent. You can use this topic to start off with.

Community
  • 1
  • 1
0

Example, when i press "3" on my keyboard my mouse moves down. when i press "2" it right clicks. but when it does click the program stops working because i clicked in a window. Means i clicked out of the program.

I want it too work even i click in other windows beside the program window.

Steve Put
  • 21
  • 1