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();
}
}