Keyboard Input In Miniscript

Selfish DevSelfish Dev
3 min read

Introduction

Hey fellas, welcome back to the How to MiniScript tutorial series!

Yeah yeah, I know — it’s been a while since the last post.
But guess what? We’re back, and we’re doing going to do it better than ever.

Before we jump in, here’s a quick recap of the previous articles in this series:

If you’re new here, I suggest you check those out first.
Alright, lets get started

Getting Basics Done.

Mouse Input?

This post is all about handling keyboard input in Mini Micro.
Mouse input? Don’t worry, that’s coming up soon!
But if you’re curious, this blog already aligns a bit with that concept:

👉How to render clicks on sprite

Choosing Your Sprite.

Normally I love making my own sprites, but today… I took a shortcut.
Why? Because this gives me the perfect opportunity to show you something awesome:

Mini Micro comes with a whole library of built-in sprites!

Here’s how to explore the built-in sprite collection:

Use this command:

findFile

Then go to: /sys/pics/animals

We’re using an animal sprite today — because why not?
I picked a crocodile, but feel free to grab any animal that vibes with your coding soul.

Setting the Background

For the background, I actually made something myself (in like 3 minutes😅).

It’s not fancy, but it works for a test setup!

Setting Up the Scene

Now let’s bring everything into the display:

//Set up display
clear
display(4).mode = displayMode.sprite 
//Set up bg
bg = new Sprite
bg.image = file.loadImage("/usr/Bg.png")
display(4).sprites.push bg
bg.x = 480
bg.y = 320
// Set up crocodile sprite
crocodile = new Sprite
crocodile.image = file.loadImage("/sys/pics/animals/crocodile.png")
display(4).sprites.push crocodile
crocodile.x = 480
crocodile.y = 120
crocodile.scale = 1.5

Don’t know how to load sprites in Mini Micro?
Check out this intro post:
How to render sprite

Creating Variables

Now we need to create a variables.

speed = 5 //Handles Movement Speed

Coding Movement

MiniScript makes keyboard input super simple with key.pressed(key)function.
For example, if you want to detect a jump on the spacebar, you'd write:

if key.pressed("space") then
    //jump code
end if

But today we’re focusing on left and right movement.

speed = 5
while true
    // Handle left and right movement
    if key.pressed("left") then
        crocodile.x = crocodile.x - speed
    else if key.pressed("right") then
        crocodile.x = crocodile.x + speed
    end if
    // Keep crocodile within screen bounds (960x640)
    if crocodile.x < 0 then crocodile.x = 0
    if crocodile.x > 960 then crocodile.x = 960
    // Exit on escape key
    if key.pressed("escape") then break
    yield
end while

What’s Happening Here?

  • Left arrow pressed? → Sprite moves left by decreasing its x position by speed.

  • Right arrow pressed? → Sprite moves right by increasing its x position by speed.

  • Speed controls how fast the movement is (you can tweak it).

  • We make sure the crocodile stays within screen limits so it doesn’t disappear off-screen.

-You can exit the loop by pressing Escape.

And just like that, you’ve got a moving croc sliding left and right across your screen.

And that’s how you handle keyboard input in miniscript.

Outro

  • Which animal you choose , Drop down in comments

  • Checkout My YT channel (Thats More Awesome) — Selfish Dev , Or Become part of my cult Join My Cult

Till Then Stay Curios , Stay Selfish

0
Subscribe to my newsletter

Read articles from Selfish Dev directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Selfish Dev
Selfish Dev