Homebrew Question Help getting Switch Homebrew apps to read button presses

Melsbacksfriend

Active Member
OP
Newcomer
Joined
Oct 28, 2018
Messages
31
Trophies
0
Age
20
XP
337
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,016
Trophies
2
Age
35
XP
4,441
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
22,373
Trophies
4
Location
Space
XP
12,076
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,016
Trophies
2
Age
35
XP
4,441
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

You may also like...

General chit-chat
Help Users
  • ZeroT21 @ ZeroT21:
    having spares never hurt
    +1
  • sombrerosonic @ sombrerosonic:
    I need to post that guide on how to hack a Slim PS2
  • M4x1mumReZ @ M4x1mumReZ:
    They're super cheap after all
  • M4x1mumReZ @ M4x1mumReZ:
    @sombrerosonic, Modchip or softmod
  • M4x1mumReZ @ M4x1mumReZ:
    Plenty of guides already out in the wild
  • ZeroT21 @ ZeroT21:
    i'd
    never hacked any of my ps2
  • sombrerosonic @ sombrerosonic:
    Softmod, Freetuna + FreeDVDboot
    +1
  • K3N1 @ K3N1:
    I got a 7 wire chipped ps1
    +1
  • ZeroT21 @ ZeroT21:
    by the time the ps3 was hacked i'd emulated ps2 games on it
  • sombrerosonic @ sombrerosonic:
    Because sometimes i need to play Sonic Mega Collection Plus
  • ZeroT21 @ ZeroT21:
    so going back to ps2 hardware seemed too much of an hassle
    +1
  • M4x1mumReZ @ M4x1mumReZ:
    @sombrerosonic, Free McBoot is all I need
  • M4x1mumReZ @ M4x1mumReZ:
    @ZeroT21, Independence Hax or Free HDBoot are the recommended ways imo
  • ZeroT21 @ ZeroT21:
    i might do it later on
  • sombrerosonic @ sombrerosonic:
    Freetuna is FreeMCboot for PS2 Slim
  • sombrerosonic @ sombrerosonic:
    Mainly 900x systems
  • sombrerosonic @ sombrerosonic:
    need to get a fat boi for PS2 hhd. I must play persona on it
  • M4x1mumReZ @ M4x1mumReZ:
    Modchipping is impossible with the PS2, even with the steadiest hands
  • ZeroT21 @ ZeroT21:
    all in know is i got 2 fat models, 1 white, 1 black
  • M4x1mumReZ @ M4x1mumReZ:
    Get an SSD adapter for your PS2 and load games onto an SSD
  • ZeroT21 @ ZeroT21:
    does ssd speeds matter all that much on such old hardware
  • M4x1mumReZ @ M4x1mumReZ:
    I do recommend getting a good SSD and none of that cheap Chinese crap
  • M4x1mumReZ @ M4x1mumReZ:
    The speeds are awful on those
    +2
  • M4x1mumReZ @ M4x1mumReZ:
    I'm using a Crucial SSD for all of my stuff
    +1
  • sombrerosonic @ sombrerosonic:
    Saving up moola so i can upgrade my PC
    +2
    sombrerosonic @ sombrerosonic: Saving up moola so i can upgrade my PC +2