Question Help getting Switch Homebrew apps to read button presses

Discussion in 'Switch - Emulation, Homebrew & Software Projects' started by Melsbacksfriend, Sep 8, 2019.

  1. Melsbacksfriend
    OP

    Melsbacksfriend Member

    Newcomer
    2
    Oct 28, 2018
    United States
    Whenever I try to make a homebrew app that uses a while loop or for loop I can't get the buttons to work. Can you help me? Here's an example of something where + is supposed to close out but doesn't.
    // Include the most common headers from the C standard library
    #include <iostream>

    // Include the main libnx system header, for Switch development
    #include <switch.h>

    using namespace std;
    // Main program entrypoint
    int main(int argc, char* argv[])
    {

    // This example uses a text console, as a simple way to output text to the screen.
    // If you want to write a software-rendered graphics application,
    // take a look at the graphics/simplegfx example, which uses the libnx Framebuffer API instead.
    // If on the other hand you want to write an OpenGL based application,
    // take a look at the graphics/opengl set of examples, which uses EGL instead.

    // Other initialization goes here. As a demonstration, we print hello world.


    // Main loop
    while (appletMainLoop())
    {
    // Scan all the inputs. This should be done once for each frame
    consoleInit(NULL);

    hidScanInput();
    u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
    // hidKeysDown returns information about which buttons have been
    // just pressed in this frame compared to the previous one


    if (kDown & KEY_PLUS)
    break; // break in order to return to hbmenu

    // Your code goes here


    int number = 1;



    while (number >= 0) {


    cout << number << endl;
    number++;

    // Update the console, sending a new frame to the display
    consoleUpdate(NULL);
    }
    }

    // Deinitialize and clean up resources used by the console (important!)
    consoleExit(NULL);
    return 0;
    }
     
  2. AnalogMan

    AnalogMan ultraSuMoFramework Dev

    Member
    12
    Apr 20, 2007
    United States
    Put your code in a code block so it's easier to read.
    The reason your app won't exit is that it's stuck in an infinite loop here:
    Code:
            while (number >= 0) {
                cout << number << endl;
                number++;
    
            // Update the console, sending a new frame to the display
            consoleUpdate(NULL);
            }
    
    And in this loop you're neither updating kDown nor checking for its value inside that loop. If you want to be able to exit WHILE in the loop there's two ways:
    Code:
            while (number >= 0) {
                cout << number << endl;
                number++;
                hidScanInput();
                kDown = hidKeysDown(CONTROLLER_P1_AUTO);
                if (kDown & KEY_PLUS)
                    break;
                   
    
           // Update the console, sending a new frame to the display
           consoleUpdate(NULL);
           }
    
    This will break the loop and continue on with the rest of the code, which in this case is consoleExit() and return 0. If you wanted it to exit the app and NOT execute any additional code you may have, then it'd look like this:
    Code:
            while (number >= 0) {
                cout << number << endl;
                number++;
                hidScanInput();
                kDown = hidKeysDown(CONTROLLER_P1_AUTO);
                if (kDown & KEY_PLUS) {
                    consoleExit(NULL);
                    return 0;
                }
                  
    
            // Update the console, sending a new frame to the display
            consoleUpdate(NULL);
            }
    
     
    Last edited by AnalogMan, Sep 8, 2019
  3. The Real Jdbye

    The Real Jdbye Always Remember 30/07/08

    Member
    19
    GBAtemp Patron
    The Real Jdbye is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    Mar 17, 2010
    Norway
    Alola
    Better yet, just change one line of code and add another:
    Code:
            while (number >= 0 && !kDown & KEY_PLUS) {
                cout << number << endl;
                number++;
    
            // Update the console, sending a new frame to the display
            consoleUpdate(NULL);
            kDown = hidKeysDown(CONTROLLER_P1_AUTO);
            }
    
     
    Last edited by The Real Jdbye, Sep 8, 2019
  4. AnalogMan

    AnalogMan ultraSuMoFramework Dev

    Member
    12
    Apr 20, 2007
    United States
    Yep, I wasn't going for most elegant or efficient but for understandability on what they were doing, otherwise I would have abolished the while (number >= 0) loop altogether since it's not even needed.
     
    The Real Jdbye likes this.
Quick Reply
Draft saved Draft deleted
Loading...