Small Steps II — Making Things

Use the Keyboard

Use the Keyboard

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Moving while a key is held

What you will learn

The keyboard state tells you which keys are currently held. key_down is true while a key is held down.

Try it

Example · example1.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double x = 320;
    double y = 240;

    while (window.is_open())
    {
        if (window.key_down(Key::Left))
            x = x - 2;
        if (window.key_down(Key::Right))
            x = x + 2;
        if (window.key_down(Key::Up))
            y = y - 2;
        if (window.key_down(Key::Down))
            y = y + 2;

        window.clear(Black);
        window.fill_circle(x, y, 20, Yellow);
        window.show();
    }
}

Change x and y with the arrow keys and redraw the circle at that position. The screen produced by one loop iteration is a frame.

Key::Left names the left arrow key. Hold the left and right arrow keys and check that the circle keeps moving.

Changing once per key press

What you will learn

key_pressed reports a new press. Distinguish it from key_down, which is true every frame while a key remains held.

Try it

Example · keyboard_2_example2.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    bool is_red = true;

    while (window.is_open())
    {
        if (window.key_pressed(Key::Space))
            is_red = !is_red;

        window.clear(Black);

        if (is_red)
            window.fill_circle(320, 240, 60, Red);
        else
            window.fill_circle(320, 240, 60, Blue);

        window.show();
    }
}

When Space is newly pressed, is_red = !is_red; toggles true and false. That value chooses is_red or blue. Hold Space, release it, and press it again.

key_released reports the moment a key is released. Down is convenient for movement; Pressed is convenient for a single change.

Exercise

Use W, A, S, and D instead of the arrow keys to move the circle up, left, down, and right.

Exercise starter · exercise1_starter.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double x = 320;
    double y = 240;

    while (window.is_open())
    {
        // Move with W, A, S, D.

        window.clear(Black);
        window.fill_circle(x, y, 20, Cyan);
        window.show();
    }
}

Hint

Check a letter key with window.key_down('W').

show solution
void small_main()
{
    Window window;
    window.open(640, 480);

    double x = 320;
    double y = 240;

    while (window.is_open())
    {
        if (window.key_down('A')) x = x - 2;
        if (window.key_down('D')) x = x + 2;
        if (window.key_down('W')) y = y - 2;
        if (window.key_down('S')) y = y + 2;

        window.clear(Black);
        window.fill_circle(x, y, 20, Cyan);
        window.show();
    }
}
Try More (Optional)

Exercise

Alternate the circle's radius between 20 and 60 each time Space is pressed. It must not keep changing while Space is held.

Exercise starter · keyboard_2_exercise2_starter.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    bool is_big = false;

    while (window.is_open())
    {
        // Toggle is_big when Space is pressed.

        window.clear(Black);

        if (is_big)
            window.fill_circle(320, 240, 60, Yellow);
        else
            window.fill_circle(320, 240, 20, Yellow);

        window.show();
    }
}

Hint

Use key_pressed(Key::Space) and a bool variable to alternate between two states.

show solution
void small_main()
{
    Window window;
    window.open(640, 480);

    bool is_big = false;

    while (window.is_open())
    {
        if (window.key_pressed(Key::Space))
            is_big = !is_big;

        window.clear(Black);

        if (is_big)
            window.fill_circle(320, 240, 60, Yellow);
        else
            window.fill_circle(320, 240, 20, Yellow);

        window.show();
    }
}
← Previous lessonNext lesson →