Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • How to implement KeyListener


    Calculus

    Recommended Posts

    Use one of the methods in the KeyboardListener class to handle Keyboard events.

    http://dreambot.org/javadocs/org/dreambot/api/methods/event/listener/KeyboardListener.html

     

     

     
    class Script extends AbstractScript implements KeyboardListener
     
    //Worth noting this might not be exactly what you need. Just a reference.
     
    private void keyPressed(KeyEvent e) {
        log("You pressed a key!");
    }
    
    Link to comment
    Share on other sites

     

    Use one of the methods in the KeyboardListener class to handle Keyboard events.

    http://dreambot.org/javadocs/org/dreambot/api/methods/event/listener/KeyboardListener.html

     
    class Script extends AbstractScript implements KeyboardListener
     
    //Worth noting this might not be exactly what you need. Just a reference.
     
    private void keyPressed(KeyEvent e) {
        log("You pressed a key!");
    }
    

    Sadly impossible to do, as you can only extend one class, and that is not an interface class.

    Link to comment
    Share on other sites

    Sadly impossible to do, as you can only extend one class, and that is not an interface class.

     

    Ah doing the same thing with KeyListener doesn't work? If you put your code in your post it'd help anyone who would like to help you quite a bit.

    Link to comment
    Share on other sites

    • 2 months later...

    This reply may border on grave digging, but I've been running into some issues with implementing a KeyListener in one of my scripts and since this topic was never really concluded I figured I would reply to this topic rather than start a new one.

     

    First off, after some tinkering, I have been able to implement a KeyListener for my Main class, as follows:

    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.core.Instance;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    @ScriptManifest(
            author = "TheScripts",
            name = "Key/Mouse Listener, Example 1",
            description = "",
            image = "",
            version = 1.0,
            category = Category.MISC
    )
    
    public class Main extends AbstractScript implements KeyListener, MouseListener {
    
        @Override
        public void onStart() {
            Instance.getInstance().getCanvas().addKeyListener(this);
    
            // Adding a MouseListener does not seem to be required...
            // Instance.getInstance().getCanvas().addMouseListener(this);
        }
    
        @Override
        public int onLoop() {
            return 0;
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
            log("keyTyped: " + e.toString());
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            log("keyPressed: " + e.toString());
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            log("keyReleased: " + e.toString());
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            log("mouseClicked: " + e.toString());
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            log("mousePressed: " + e.toString());
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            log("mouseReleased: " + e.toString());
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
            log("mouseEntered: " + e.toString());
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
            log("mouseExited: " + e.toString());
        }
    }
    
    

    This script will capture keyboard and mouse input and print the event string to the debug console. I'm not sure if this is the best way to implement this, but it does work and will, hopefully, provide some help, or at least a starting point, to those that stumble on this topic.

     

    Now, the issue that I'm having occurs when I try to implement this in a separate class, my code is as follows:

     

    Main Class:

    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    
    @ScriptManifest(
            author = "TheScripts",
            name = "Key/Mouse Listener, Example 2",
            description = "",
            image = "",
            version = 1.0,
            category = Category.MISC
    )
    
    public class Main extends AbstractScript {
    
        public TheListener theListener = new TheListener();
    
        @Override
        public int onLoop() {
            return 0;
        }
    
    }
    
    

    TheListener Class:

    import org.dreambot.core.Instance;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import static org.dreambot.api.methods.MethodProvider.log;
    
    public class TheListener implements KeyListener, MouseListener {
    
        public TheListener() {
            Instance.getInstance().getCanvas().addKeyListener(this);
            Instance.getInstance().getCanvas().addMouseListener(this);
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
            log("keyTyped: " + e.toString());
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            log("keyPressed: " + e.toString());
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            log("keyReleased: " + e.toString());
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            log("mouseClicked: " + e.toString());
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            log("mousePressed: " + e.toString());
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            log("mouseReleased: " + e.toString());
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
            log("mouseEntered: " + e.toString());
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
            log("mouseExited: " + e.toString());
        }
    
    }
    
    

    I'm not sure why, but whenever I try to access the Instance (even something as trivial as "Instance.getInstance().isMouseInputEnabled()") in a class, that is not the Main Class, DreamBot no longer allows me to start the script. I can see the script in the Script Panel, I can select it, and I can press the start button; but the Script Panel remains open and the script does not start.

     

    This has been a significant source of frustration for the last few days, so any help would be greatly appreciated!

    Link to comment
    Share on other sites

    This reply may border on grave digging, but I've been running into some issues with implementing a KeyListener in one of my scripts and since this topic was never really concluded I figured I would reply to this topic rather than start a new one.

     

    First off, after some tinkering, I have been able to implement a KeyListener for my Main class, as follows:

     

    This script will capture keyboard and mouse input and print the event string to the debug console. I'm not sure if this is the best way to implement this, but it does work and will, hopefully, provide some help, or at least a starting point, to those that stumble on this topic.

     

    Now, the issue that I'm having occurs when I try to implement this in a separate class, my code is as follows:

     

    I'm not sure why, but whenever I try to access the Instance (even something as trivial as "Instance.getInstance().isMouseInputEnabled()") in a class, that is not the Main Class, DreamBot no longer allows me to start the script. I can see the script in the Script Panel, I can select it, and I can press the start button; but the Script Panel remains open and the script does not start.

     

    This has been a significant source of frustration for the last few days, so any help would be greatly appreciated!

     

    Nice one figuring out how to add the mouse listener! Ok, following that logic though, this is a very simple fix:

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import static org.dreambot.api.methods.MethodProvider.log;
    
    public class TheListener implements KeyListener, MouseListener {
    
        @Override
        public void keyTyped(KeyEvent e) {
            log("keyTyped: " + e.toString());
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            log("keyPressed: " + e.toString());
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            log("keyReleased: " + e.toString());
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            log("mouseClicked: " + e.toString());
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            log("mousePressed: " + e.toString());
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            log("mouseReleased: " + e.toString());
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
            log("mouseEntered: " + e.toString());
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
            log("mouseExited: " + e.toString());
        }
    }
    
    public class Test extends AbstractScript{
    
        @Override
        public void onStart() {
            TheListener listener = new TheListener();
            Instance.getInstance().getCanvas().addKeyListener(listener);
            Instance.getInstance().getCanvas().addMouseListener(listener);
        }
    
        @Override
        public int onLoop() {
            return 0;
        }
    }
    
    Link to comment
    Share on other sites

     

    Nice one figuring out how to add the mouse listener! Ok, following that logic though, this is a very simple fix:

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import static org.dreambot.api.methods.MethodProvider.log;
    
    public class TheListener implements KeyListener, MouseListener {
    
        @Override
        public void keyTyped(KeyEvent e) {
            log("keyTyped: " + e.toString());
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            log("keyPressed: " + e.toString());
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            log("keyReleased: " + e.toString());
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            log("mouseClicked: " + e.toString());
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            log("mousePressed: " + e.toString());
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            log("mouseReleased: " + e.toString());
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
            log("mouseEntered: " + e.toString());
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
            log("mouseExited: " + e.toString());
        }
    }
    
    public class Test extends AbstractScript{
    
        @Override
        public void onStart() {
            TheListener listener = new TheListener();
            Instance.getInstance().getCanvas().addKeyListener(listener);
            Instance.getInstance().getCanvas().addMouseListener(listener);
        }
    
        @Override
        public int onLoop() {
            return 0;
        }
    }
    

     

    Wow... That simple, eh? (yes, I'm Canadian)

     

    Thank you very much for that solution. That's one headache that I can now put behind me :)

     

    EDIT:

    The only thing I don't like about this solution is that the listeners need to be setup in the main class instead of the other class; this makes the other class a bit less modular. It would be ideal if you only needed to instantiate the other class and it handled setting up the listeners :/

    Link to comment
    Share on other sites

    Wow... That simple, eh? (yes, I'm Canadian)

     

    Thank you very much for that solution. That's one headache that I can now put behind me :)

     

    EDIT:

    The only thing I don't like about this solution is that the listeners need to be setup in the main class instead of the other class; this makes the other class a bit less modular. It would be ideal if you only needed to instantiate the other class and it handled setting up the listeners :/

    EZPZ:

        public class KeyMouseListener extends MouseAdapter implements KeyListener{
            private AbstractScript ctx;
            public KeyMouseListener(AbstractScript ctx) {
                this.ctx = ctx;
                ctx.getClient().getInstance().getCanvas().addMouseListener(this);
                ctx.getClient().getInstance().getCanvas().addKeyListener(this);
            }
    
            @Override
            public void mousePressed(MouseEvent e) {
                log("clicked");
            }
    
            @Override
            public void keyTyped(KeyEvent e) {
                log("keyTyped: " + e.getKeyChar());
            }
    
            @Override
            public void keyPressed(KeyEvent e) {
    
            }
    
            @Override
            public void keyReleased(KeyEvent e) {
    
            }
    
            public void closeListeners(){
                getClient().getInstance().getCanvas().removeKeyListener(this);
                getClient().getInstance().getCanvas().removeMouseListener(this);
            }
        }
    

    YOU WILL NEED TO CLOSE THE LISTENERS TOO!!!!!!! ADD THIS TO YOUR ABSTRACT SCRIPT:

        @Override
        public void onExit() {
            keyMouseListener.closeListeners();
        }
    
        private KeyMouseListener keyMouseListener;
    
        @Override
        public void onStart() {
            keyMouseListener = new KeyMouseListener(this);
        } 

    Edit:

    (These are pretty basic Java concepts)

    Link to comment
    Share on other sites

    EZPZ:

        public class KeyMouseListener extends MouseAdapter implements KeyListener{
            private AbstractScript ctx;
            public KeyMouseListener(AbstractScript ctx) {
                this.ctx = ctx;
                ctx.getClient().getInstance().getCanvas().addMouseListener(this);
                ctx.getClient().getInstance().getCanvas().addKeyListener(this);
            }
    
            @Override
            public void mousePressed(MouseEvent e) {
                log("clicked");
            }
    
            @Override
            public void keyTyped(KeyEvent e) {
                log("keyTyped: " + e.getKeyChar());
            }
    
            @Override
            public void keyPressed(KeyEvent e) {
    
            }
    
            @Override
            public void keyReleased(KeyEvent e) {
    
            }
    
            public void closeListeners(){
                getClient().getInstance().getCanvas().removeKeyListener(this);
                getClient().getInstance().getCanvas().removeMouseListener(this);
            }
        }
    

    YOU WILL NEED TO CLOSE THE LISTENERS TOO!!!!!!! ADD THIS TO YOUR ABSTRACT SCRIPT:

        @Override
        public void onExit() {
            keyMouseListener.closeListeners();
        }
    
        private KeyMouseListener keyMouseListener;
    
        @Override
        public void onStart() {
            keyMouseListener = new KeyMouseListener(this);
        } 

    Edit:

    (These are pretty basic Java concepts)

     

    Once again, thank you for the response!

     

    I've been declaring and instantiating "theListener" globally in class Main as follows:

    public class Main extends AbstractScript {
    
        private TheListener theListener = new TheListener();
    
        @Override
        public void onStart() {  }
    
        @Override
        public int onLoop() { return 0; }
    
    }
    

    This seems to be the source of the program, as soon as I instantiate "theListener" within the onStart() method the problem goes away.

    public class Main extends AbstractScript {
    
        private TheListener theListener;
    
        @Override
        public void onStart() { listener = new TheListener(); }
    
        @Override
        public int onLoop() { return 0; }
    
    }
    

    The script now runs without any issues with the following logic in the constructor for class TheListener. Here's the code for reference:

    public class TheListener implements KeyListener, MouseListener {
    
        public TheListener(){
            Instance.getInstance().getCanvas().addKeyListener(this);
            Instance.getInstance().getCanvas().addMouseListener(this);
        }
    
        @Override
        public void keyTyped(KeyEvent e) { log("keyTyped: " + e.toString()); }
    
        @Override
        public void keyPressed(KeyEvent e) { log("keyPressed: " + e.toString()); }
    
        @Override
        public void keyReleased(KeyEvent e) { log("keyReleased: " + e.toString()); }
    
        @Override
        public void mouseClicked(MouseEvent e) { log("mouseClicked: " + e.toString()); }
    
        @Override
        public void mousePressed(MouseEvent e) { log("mousePressed: " + e.toString()); }
    
        @Override
        public void mouseReleased(MouseEvent e) { log("mouseReleased: " + e.toString()); }
    
        @Override
        public void mouseEntered(MouseEvent e) { log("mouseEntered: " + e.toString()); }
    
        @Override
        public void mouseExited(MouseEvent e) { log("mouseExited: " + e.toString()); }
    }
    

    Thank you for all the support!

    Link to comment
    Share on other sites

    Your original code didnt work because you created an instance of the listener that uses a null Instance

    public class Main extends AbstractScript {
     
        public TheListener theListener = new TheListener();
     
        @Override
        public int onLoop() {
            return 0;
        }
     
    }
    
    

    The instance of Instance wasn't built yet when you created that listener, so it would be solved by putting it in the onStart.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.