Click here to Skip to main content
14,983,071 members
Articles / Programming Languages / Kotlin
Tip/Trick
Posted 18 Apr 2021

Stats

2.1K views
18 downloads
1 bookmarked

Create a Simple Bot Creator in 33 Lines of Code

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Apr 2021CPOL5 min read
How to create a simple bot creator in Kotlin using only 33 lines of code
In this tip, you will learn to create a simple bot creator in 33 lines of code in Kotlin to control Mouse and Keyboards.

Introduction

I am a software engineer who is interested in Android development and compiler design, and I love to play some RPG games, but some of those games are designed to make you stay on the game as much as possible, So they give you some repeated and boring tasks to do and it’s maybe every day, for example, collect several items, etc, so I have searched for a bot creator to use it and automate those tasks.

I have found a good one with many features like control mouse, keyboard, screen, check pixel color with some helper statements like if, loop, etc.

But to create your script, you need to add a list of commands with the GUI that means you can write a script like any programming language and I hated this idea, So after trying this program, I got one question about how can I design one like this or maybe better than it ?!

I started searching for how to control the mouse, screen, keyboard in Java, and after some research, I got many answers and found an amazing class in Java called Robot.

Robot as you can see in Oracle documentation:

"This class is used to generate native system input events for test automation, 
self-running demos, and other applications where control of the …"

and it has many useful methods to do exactly what I want, and in this time, I have read some Compiler Design books and created many toy programming languages, and also have a good experience in JavaFX Framework, so I found that it will be amazing if I merged my experience in Compiler Design, JavaFX and Robot Class to create my amazing Bot Creator Project.

I have started to create a scripting programming language with keywords for mouse, keyboard, screen then I added support for statements so I added if, while, repeat statements, after that, I want to make it easy for the user to create a complex bot, so I have added support for variables, functions and builtin function, for example, I have added builtin function called pixelColor to get the current mouse position pixel value, after finishing this language, it’s time for JavaFX to start so I have created a simple editor for it with some buttons like run, stop, restart, etc. and this is the result I called it SnapMacro.

Image 1

After finished this project and using it in online games, I used it also to write a bot for Linkedin to add people or maybe I can use it to accept many people inventions and other things.

This is the story behind SnapMacro, by the way, Snap is the name of my scripting language, but the article has not ended here and the goal is not just telling my story but it to show you how to create a simple bot creator in just 33 lines with the same concept.

Yes, as you can see, it will just 33 lines of code!

We will not create a scripting language or support statements or variables, but we will create a simple bot creator that supports mouse, keyboard commands.

So first what we want from our small bot creator? Basically, we will support 3 types of commands and the result will be 7 commands.

Mouse Commands

mouse move x y
mouse press right | left
mouse release right | left
mouse wheel x

Keyboard Commands

keyboard press x
keyboard release x

Delay Commands

delay x

So let’s start creating our bot creator, and we will use Kotlin Programming language in this article, but you can use Java too or take the concept and use your favourite programming language it doesn’t matter.

First, we will use the Scanner class to read user input from the console and we will initialize an instance of Robot Class and we will use when to check what is the type of current command.

JavaScript
fun main() {
    val scanner = Scanner(System.`in`)
    val robot = Robot()
    while (scanner.hasNext()) {
        when (scanner.next()) {
             "mouse" -> {}
             "keyboard" -> {}
             "delay" -> {}
        }
    }
}

And now inside every type, we need to support some commands so let us start with the smallest one “delay” is has only one command which is delay x that takes time in a millisecond as an integer and uses a Robot to make delay, so the code will be like this:

JavaScript
"delay" -> robot.delay(scanner.nextInt())

Now let’s move to mouse commands and it has four commands move, click and wheel, first “press” command and it will take right or left and we will use when to check this, then we have “release” and it exactly like “press” in the structure, then we have “move” command and it takes x and y values and moves the cursor to this position so we need to scan two integers for this command, after that, we have “wheel” command and it needs just one positive or negative integer.

JavaScript
"mouse" -> when (scanner.next()) {
    "press" -> {
        when (scanner.next()) {
            "right" -> robot.mousePress(BUTTON3_DOWN_MASK)
            "left" -> robot.mousePress(BUTTON1_DOWN_MASK)
        }
    }
    "release" -> {
        when (scanner.next()) {
            "right" -> robot.mouseRelease(BUTTON3_DOWN_MASK)
            "left" -> robot.mouseRelease(BUTTON1_DOWN_MASK)
        }
    }
    "move" -> robot.mouseMove(scanner.nextInt(), scanner.nextInt())
    "wheel" -> robot.mouseWheel(scanner.nextInt())
}

And now, we finished the mouse command and we will go for keyboard commands, it has only two commands, “press” and “release” and they take character code, but it will not be very good to force the user to write the keycode like so we will read a character and convert it to a keycode, Robot has a good function that will do this job for use it called getExtendedKeyCodeForChar it takes char and return keycode as an integer, so the keyboard commands part will be like this:

JavaScript
"keyboard" -> {
    val action = scanner.next()
    val character = scanner.next()
    val key = KeyEvent.getExtendedKeyCodeForChar(character[0].toInt())
    when (action) {
        "press" -> robot.keyPress(key)
        "release" -> robot.keyRelease(key)
    }
}

And now congratulations, you have a simple bot creator so now you can write a script like that:

mouse point 500 500
mouse press left
delay 500
mouse release left
mouse wheel 100
keyboard press A
delay 500
keyboard release A
delay 500

And it will work fine. :D

And the full code is about 33 lines of code as shown below:

JavaScript
fun main() {
    val scanner = Scanner(System.`in`)
    val robot = Robot()
    while (scanner.hasNext()) {
        when (scanner.next()) {
            "mouse" -> when (scanner.next()) {
                "press" -> {
                    when (scanner.next()) {
                        "right" -> robot.mousePress(BUTTON3_DOWN_MASK)
                        "left" -> robot.mousePress(BUTTON1_DOWN_MASK)
                    }
                }
                "release" -> {
                    when (scanner.next()) {
                        "right" -> robot.mouseRelease(BUTTON3_DOWN_MASK)
                        "left" -> robot.mouseRelease(BUTTON1_DOWN_MASK)
                    }
                }
                "move" -> robot.mouseMove(scanner.nextInt(), scanner.nextInt())
                "wheel" -> robot.mouseWheel(scanner.nextInt())
            }
            "keyboard" -> {
                val action = scanner.next()
                val character = scanner.next()
                val key = KeyEvent.getExtendedKeyCodeForChar(character[0].toInt())
                when (action) {
                    "press" -> robot.keyPress(key)
                    "release" -> robot.keyRelease(key)
                }
            }
            "delay" -> robot.delay(scanner.nextInt())
        }
    }
}

If you are interested in these type of projects, you shouldn’t stop here, and try to improve it as must as you can, if you want to convert it to a scripting language, I recommend to read the book, Crafting Interpreter , that will teach you how to create a compiler and interpreter, you can also store the commands and support some new commands like restart, exit, etc.


Download The code in Java or Kotlin

Download BotCreatorJava.zip

Download BotCreatorKotlin.zip

Thanks for reading and enjoy coding. 😋

History

  • 18th April, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AmrDeveloper
Software Developer (Junior)
Egypt Egypt
Software Engineer who able to build high-performance maintainable Software tools and applications with a modern and fast UI to provide a great experience for the user, I will be very interested to work on a unique project with new challenges to push my limit and learn new things.


Comments and Discussions

 
QuestionSource code? Pin
Chris Maunder18-Apr-21 4:52
cofounderChris Maunder18-Apr-21 4:52 
AnswerRe: Source code? Pin
AmrDeveloper21-Apr-21 5:45
MemberAmrDeveloper21-Apr-21 5:45 
QuestionMessage Closed Pin
18-Apr-21 3:54
Membersookitak18-Apr-21 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.