aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/buttons.rs
blob: 63956200b09c2a982cc56c83d9f01620afe60331 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
extern crate ctru;

use ctru::gfx::Gfx;
use ctru::console::Console;
use ctru::services::apt::Apt;
use ctru::services::hid::{Hid, KeyPad};

fn main() {
    // Setup services
    let apt = Apt::init().unwrap();
    let hid = Hid::init().unwrap();
    let gfx = Gfx::default();
    let console = Console::default();

    println!("Hi there! Try pressing a button");
    println!("\x1b[29;16HPress Start to exit");

    // This struct will contain the keys that we held on the previous frame
    let mut old_keys = KeyPad::empty();

    while apt.main_loop() {
        // Scan for user input on the current frame.
        hid.scan_input();

        // Get information about which keys were held down on this frame
        let keys = hid.keys_held();

        // We only want to print when the keys we're holding now are different
        // from what they were on the previous frame
        if keys != old_keys {

            // Clear the screen
            console.clear();

            // We print these again because we just cleared the screen above
            println!("Hi there! Try pressing a button");
            println!("\x1b[29;16HPress Start to exit");

            // Move the cursor back to the top of the screen
            println!("\x1b[3;0H");

            // Print to the screen depending on which keys were held.
            //
            // The .contains() method checks for all of the provided keys,
            // and the .intersects() method checks for any of the provided keys.
            //
            // You can also use the .bits() method to do direct comparisons on
            // the underlying bits

            if keys.contains(KeyPad::KEY_A) {
                println!("You held A!");
            }
            if keys.bits() & KeyPad::KEY_B.bits() != 0 {
                println!("You held B!");
            }
            if keys.contains(KeyPad::KEY_X | KeyPad::KEY_Y) {
                println!("You held X and Y!");
            }
            if keys.intersects(KeyPad::KEY_L | KeyPad::KEY_R | KeyPad::KEY_ZL | KeyPad::KEY_ZR) {
                println!("You held a shoulder button!");
            }
            if keys.intersects(KeyPad::KEY_START) {
                println!("See ya!");
                break
            }
        }

        // Save our current key presses for the next frame
        old_keys = keys;

        // Flush and swap framebuffers
        gfx.flush_buffers();
        gfx.swap_buffers();
        gfx.wait_for_vblank();
    }
}