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,343
Trophies
4
Location
Space
XP
13,927
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
  • HiradeGirl @ HiradeGirl:
    That's a lot of money.
  • HiradeGirl @ HiradeGirl:
    I can pay it, but I'd rather eat better.
  • K3Nv2 @ K3Nv2:
    Nah just eat floor crumbs
  • HiradeGirl @ HiradeGirl:
    Juan's floor crumbs were always rat fur
    +1
  • BigOnYa @ BigOnYa:
    Change from real cheese, to government cheese, you'll save some money.
    +1
  • K3Nv2 @ K3Nv2:
    Gotta get that government cheese one new laptop a year
    +1
  • K3Nv2 @ K3Nv2:
    Fucking Biden making us pay full internet prices
    +1
  • BigOnYa @ BigOnYa:
    Of course there is always, OnlyFans, or a GoFundMe, to raise some money.
  • HiradeGirl @ HiradeGirl:
    @BigOnYa are you on OnlyFans?
  • K3Nv2 @ K3Nv2:
    He gets his ramming funds from onlyfans
  • BigOnYa @ BigOnYa:
    Yea but my total income is negative, lol
  • HiradeGirl @ HiradeGirl:
    I would pay for watching someone eat food from the floor.
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, stop spending the videos' budget on food
  • BigOnYa @ BigOnYa:
    No I've never even been to the site(honestly) but have heard of it
  • K3Nv2 @ K3Nv2:
    I'm half way at my savings for a new move
  • BigOnYa @ BigOnYa:
    Like a karate move? The flying dragon is cool.
  • HiradeGirl @ HiradeGirl:
    @BigOnYa if you've never been to the site how do you know about its contents?
  • Xdqwerty @ Xdqwerty:
    Can he do a shoryuken?
  • Xdqwerty @ Xdqwerty:
    @HiradeGirl, cuz of people mentioning it everywhere
    +1
  • HiradeGirl @ HiradeGirl:
    Someone here introduced me to it. Not gonna say who.
  • BigOnYa @ BigOnYa:
    Everybody knows what that site about, and you can't read normal news anymore without hearing about it
  • HiradeGirl @ HiradeGirl:
    But it's degrading and disgusting.
  • Xdqwerty @ Xdqwerty:
    @HiradeGirl, was it Juan?
  • HiradeGirl @ HiradeGirl:
    Juan who?
  • BigOnYa @ BigOnYa:
    Its just seductive pics right? I mean they don't show nudity, do they?
    BigOnYa @ BigOnYa: Its just seductive pics right? I mean they don't show nudity, do they?