Search code examples
javaprintln

How to print line to cursor in java (eclispe ide)?


Once upon a time, I had taught myself how to output a series of characters wherever my mouse cursor had last clicked; i.e, I was using it to spam cheats in AOEii campaigns. It pressed enter, printed the characters in "to smithereens", and pressed enter again every 0.5 seconds.

I can't for the life of me get that working again. Any knowledge on how to move the text output away from the console? This time I'm trying to use it to spam CK3 cheats in the debug console. I have the code written for the actual output, but can't move it to the correct place.


Solution

  • What you're describing is likely a virtual keypress (VK). It simulates the physical pressing of a key on a keyboard, but it isn't limited to the output console; it'll press the key wherever you've placed your cursor.

    Here's an example:

    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    public class Robot {
    
    public static void main(String[] args) throws AWTException, 
    InterruptedException {
        // TODO Auto-generated method stub
        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_H);
        r.keyRelease(KeyEvent.VK_H);
    

    The variable "r" can be any string or character you want. "Robot" is the class, but, again, you can name it as you see fit. The keypress event is what you asked about- it calls the system to virtually press the key- in this case, it will press the "h" key.

    It's also important to note that the program will not automatically release that key- it will hold it down until you tell it to let go or to press a different key. That is what the second line- keyrelease- is for. It tells the system to let go of the key, which is crucial if you need the same key twice in a row.