Homebrew Question Help getting Switch Homebrew apps to read button presses

Melsbacksfriend

Active Member
OP
Newcomer
Joined
Oct 28, 2018
Messages
33
Trophies
0
Age
21
XP
361
Country
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;
}
 

DocKlokMan

Plugin Dev
Member
Joined
Apr 20, 2007
Messages
3,009
Trophies
2
Age
36
XP
4,571
Country
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.
Code:
// 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;
}
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 DocKlokMan,

The Real Jdbye

*is birb*
Member
Joined
Mar 17, 2010
Messages
23,317
Trophies
4
Location
Space
XP
13,899
Country
Norway
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);
        }
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,

DocKlokMan

Plugin Dev
Member
Joined
Apr 20, 2007
Messages
3,009
Trophies
2
Age
36
XP
4,571
Country
United States
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);
        }
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.
 
  • Like
Reactions: The Real Jdbye

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Psionic Roshambo @ Psionic Roshambo: They probably burned the disks at too high a speed....