Larold’s Jubilant Junkyard” has become “Larold’s Retro Gameyard“. I’ve been working on this re-branding and migration for a while. All old tutorials should redirect you to this new site. If you see any errors, please let me know! See here for more information: What happened to Larold’s Jubilant Junkyard?

keypad input in butano game engine

Handling keypad input on the Game Boy Advance

Joypad Input with Butano is very simple. All of the hard-work is done for you. The engine provides functions for whatever you may need.

Firstly, like all Butano features, you’ll need to include the proper header file. For input, you’ll really only need to include

#include <bn_keypad.h>

After that, all of the functions you need are in the “bn::keypad” namespace. You can find the documentation for this namespace here: Butano Docs – bn::keypad namespace.

Easy Keypad Functions in Butano

Butano has easy functions for common button operations:

  • bn::keypad::<button>_pressed
  • bn::keypad::<button>_released
  • bn::keypad::<button>_held

Where <button> is one of these values:

  • up
  • down
  • left
  • right
  • start
  • select
  • a
  • b
  • l
  • r

Here are some Examples:

if(bn::keypad::a_pressed())DoSomething();
if(bn::keypad::b_pressed())DoSomething();
if(bn::keypad::start_released())DoSomething();
if(bn::keypad::select_released())DoSomething();
if(bn::keypad::left_held())DoSomething();
if(bn::keypad::right_held())DoSomething();

Butano also has three easy catch-all functions:

if(bn::keypad::any_pressed())DoSomething();
if(bn::keypad::any_released())DoSomething();
if(bn::keypad::any_held())DoSomething();

Parametrized Keypad functions

In addition to those easy functions, you can use the “bn::keypad::key_type” enumeration as an argument for parametrized versions of the above functionality:

if(bn::keypad::held(bn::keypad::key_type::LEFT))DoSomething();
if(bn::keypad::pressed(bn::keypad::key_type::A))DoSomething();
if(bn::keypad::released(bn::keypad::key_type::B))DoSomething();

Here Are the full values for the “bn::keypad::key_type” enumeration:

  • bn::keypad::key_type::LEFT
  • bn::keypad::key_type::RIGHT
  • bn::keypad::key_type::DOWN
  • bn::keypad::key_type::UP
  • bn::keypad::key_type::A
  • bn::keypad::key_type::B
  • bn::keypad::key_type::R
  • bn::keypad::key_type::L
  • bn::keypad::key_type::START
  • bn::keypad::key_type::SELECT

Here’s an example of controlling a sprite. If you dont know how to use sprites in butano, see my tutorial here: Drawing sprites on the Game Boy Advance. We’ll use the directional buttons to move the sprites position.

#include <bn_core.h>
#include <bn_keypad.h>
#include <bn_sprite_ptr.h>
#include "bn_sprite_items_testplayer.h"

int main()
{
    bn::core::init();

    // Create a basic sprite in the center of the screen
    bn::sprite_ptr playersprite = bn::sprite_items::testplayer.create_sprite(0,0);
    

    // When this part of the code is reached, the clouds background has gone out of scope and will be removed
    while(true)
    {
        bn::core::update();

        if(bn::keypad::left_held()){

            // Decease the x position to move it to the left
            playersprite.set_x(playersprite.x()-bn::fixed(2));

        }else if(bn::keypad::right_held()){

            // Increase the x position to move it to the right
            playersprite.set_x(playersprite.x()+bn::fixed(2));
        }


        if(bn::keypad::up_held()){

            // Decrease the y  position to move it upwards
            playersprite.set_y(playersprite.y()-bn::fixed(2));
        }else if(bn::keypad::down_held()){

            // Increase the y  position to move it downwards
            playersprite.set_y(playersprite.y()+bn::fixed(2));
        }

    }
}

The end result is this:

butano keypad input gif using arrow keys

That’s everything you need to know about keypad input. if you want to see more documentation, check out:

Leave a Reply

Your email address will not be published. Required fields are marked *

THANKS FOR READING!

If you have any suggestions, and/or are confused about anything: feel free to leave a comment, send me a email, or a message on social media. Constructive criticism will help the gameyard, and others like yourself. If you learned something from this tutorial, and are looking for more content, check other these other tutorials. 

Handling keypad input on the Game Boy Advance

Thanks for reading my tutorial, here are the files for that tutorial. 

Download Instructions

You’ll need the Butano Game Engine installed to run this. In addition, you’ll need the ability to run Makefiles. After you unzip the code, Make sure you update the LIBBUTANO . It should point to the “butano” folder INSIDE of your butano download folder. For me it was “c:/butano/butano”. After that, you can create your .gba file by running make.

If you want to make/request changes for the code, you can send me an email or put in a PR request on GitHub. Here’s a GitHub link for the code below.

Sign-Up for the "Gameyard Newsletter" for MOre Game Development News

If you like Retro Game Development, subscribe to the Gameyard Newsletter. Stay up-to-date with all the latest Game Boy news. It’s free, and you can unsubscribe any time!

Be sure to check your email inbox for a confirmation email.