Reputation: 431
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Main {
static boolean check = false;
static boolean boom = true;
public static void main(String[] args) throws Exception{
do{
if(check == true){
Robot r = new Robot();
r.delay(1000);
r.keyPress(KeyEvent.VK_DECIMAL);
r.keyRelease(KeyEvent.VK_DECIMAL);
r.keyPress(KeyEvent.VK_M);
r.keyRelease(KeyEvent.VK_M);
r.keyPress(KeyEvent.VK_E);
r.keyRelease(KeyEvent.VK_E);
r.keyPress(KeyEvent.VK_N);
r.keyRelease(KeyEvent.VK_N);
r.keyPress(KeyEvent.VK_U);
r.keyRelease(KeyEvent.VK_U);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}
}while(boom == true);
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_F9){
check = true;
boom = true;
}
if(keyCode == KeyEvent.VK_F11){
check = false;
boom = false;
}
}
}
This is my code,I want it work like this: When you press F9 it should start write .menu and when pressing F11 it should stop. Any Help?
Upvotes: 0
Views: 568
Reputation: 285405
You've no GUI component visible for the KeyListener to listen to. KeyListeners require that they be added to a component within a rendered GUI and the component being listened to has focus. Your app has none of that. I don't even see a KeyListener object anywhere within your code.
If you want to create a GUI that listens for key events, consider learning how to create a Java Swing GUI at the Swing tutorials, and then consider using Key Bindings in place of a low-level listener such as a KeyListener.
If on the other hand you wish to create a general key logger program without a GUI, then I suggest you not use Java but instead some other language that allows tighter integration with the operating system.
Upvotes: 3