Im currenrtly working on a autoclicker program. I wanted to press e.g. F10
to start or stop the autoclicking. How can i do this? And is it too possible to get the keypressings if the user has currently opened an other program and the autoclicker is minimized?

- 205,037
- 37
- 486
- 720

- 2,050
- 2
- 14
- 16
2 Answers
Whilst I could write you a whole chunk of code, I strongly believe nothing beats Oracle's Tutorials
You simply have to implement the KeyListener interface and use the methods required in that interface:
The method names are self-explanatory and the KeyEventDemo shows a great way of how these are implemented with comments in each segment to explain what's going on.
In relation to your post, you may consider just adding a global hotkey in your project to start/stop your program whenever F10
is pressed based on the current state of the program.
For the second part of your question, I won't have a direct answer as I haven't come across such a scenario yet. However I scouted around and came across this StackOverflow question which seems to cover your question and if not, check out this question aswell, the second question proposes the idea I gave you of using a global hotkey but that would have to be written in C
.
Hope this helps
-
1Don't use a KeyListener. Swing was designed to be used with Key Bindings. – camickr Sep 07 '14 at 14:10
You have the right tag (key-bindings). With Swing, generally you want to use Key Bindings over lower level KeyListener
. Here's a brief statement from the link (with some arguments):
Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.
So as far as your question:
"I wanted to press e.g. F10 to start or stop the autoclicking. How can i do this?"
You basically want to bind the key to the content pane of the of the frame. This will allow an F10 action to occur no matter which component is focused (as long as you use the correct `InputMap
Here is an example. Make sure to read the tutorial How to use Key Bindings for further explanations.
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.*;
public class KeyBindDemo {
private JButton clicker;
public KeyBindDemo() {
JFrame frame = new JFrame();
JPanel contentPane = (JPanel)frame.getContentPane();
addKeyBind(contentPane, "F10");
clicker = new JButton("Clicker");
contentPane.add(clicker);
frame.setLayout(new GridBagLayout());
frame.setSize(300, 300); // you should pack() instead.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Action disableButtonAction = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
clicker.setEnabled(!clicker.isEnabled());
}
};
private static final String DISABLE_CLICKER = "disableClicker";
private void addKeyBind(JComponent contentPane, String key) {
InputMap iMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap aMap = contentPane.getActionMap();
iMap.put(KeyStroke.getKeyStroke(key), DISABLE_CLICKER);
aMap.put(DISABLE_CLICKER, disableButtonAction);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new KeyBindDemo();
}
});
}
}
"And is it too possible to get the keypressings if the user has currently opened an other program and the autoclicker is minimized?"
Not really sure what that means. You need to give a better explanation.

- 205,037
- 37
- 486
- 720
-
1*"Not really sure what that means."* I read it as 'Can the app. receive events when it is not the focused app.?'. ..And the answer is 'no'. – Andrew Thompson Sep 07 '14 at 13:25