Thread: How to make a menu that uses arrow keys

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    1

    How to make a menu that uses arrow keys

    My program uses a menu that easily helps a person to navigate trough out it but im stuck on a part and i cant figure it out. i cant go to menu3 with the arrows (P.S please only use c code not c++)

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>


    void sleep(long int interval)
    {
    for(int i=0;i<interval*1000;i++);
    }


    void menu();




    unsigned char a;


    char MenuPt1();
    char MenuPt2();
    char MenuPt3();


    //////////////////////////////////////////////////////////////////////////////
    char MenuPt1()
    {

    system("cls");
    printf("WHAT DO YOU WANT TO DO?\n\n");
    printf("%c",218);
    for(int i=0;i<11;i++)
    {
    printf("%c",196);
    }
    printf("%c\n",191);
    printf("%cPLAY A SONG",179);
    printf("%c\n",179);
    printf("%c",192);
    for(int i=0;i<11;i++)
    {
    printf("%c",196);
    }
    printf("%c\n",217);
    printf("\n");

    printf(" LISTEN TO A SONG\n\n\n");
    printf(" LEARN A SONG.\n\n\n");
    printf(" PRESS ESC TO EXIT");

    if (a==13){printf("q");}
    }
    ///////////////////////////////////////////////////////////////////////////////
    char MenuPt2()
    {
    system("cls");
    printf("WHAT DO YOU WANT TO DO?\n\n");
    printf("\n PLAY A SONG\n\n");

    printf("%c",218);
    for(int i=0;i<16;i++)
    {
    printf("%c",196);
    }
    printf("%c\n",191);
    printf("%cLISTEN TO A SONG",179);
    printf("%c\n",179);
    printf("%c",192);
    for(int i=0;i<16;i++)
    {
    printf("%c",196);
    }
    printf("%c\n",217);
    printf("\n");

    printf(" LEARN A SONG.\n\n\n");
    printf(" PRESS ESC TO EXIT");}
    ///////////////////////////////////////////////////////////////////////////////
    void menu()
    {
    int menu1choice;
    int var1;
    int var2;
    int var3;
    int A;
    int B;
    int C;


    system("cls");
    printf("WHAT DO YOU WANT TO DO?\n\n");


    printf("%c",218);
    for(int i=0;i<11;i++)
    {
    printf("%c",196);
    }
    printf("%c\n",191);
    printf("%cPLAY A SONG",179);
    printf("%c\n",179);
    printf("%c",192);
    for(int i=0;i<11;i++)
    {
    printf("%c",196);
    }
    printf("%c\n",217);
    printf("\n");

    printf(" LISTEN TO A SONG\n\n\n");
    printf(" LEARN A SONG.\n\n\n");
    printf(" PRESS ESC TO EXIT");}
    ////////////////////////////////////////////////////////////////////////////////


    int main()
    {
    printf(" %c",201);//print left top corner
    for(int i=0;i<20;i++)
    {
    printf("%c",205);sleep(10000);
    }
    printf("%c\n",187);//print top right corner
    printf(" %cWELCOME TO MUSISCORE",186);//print vertical borders
    printf("%c\n",186);
    printf(" %c",200);//print bottom left corner
    for(int i=0;i<20;i++)
    {
    printf("%c",205);sleep(10000);
    }
    printf("%c\n",188);//print bottom right corner
    printf("\n");
    printf("Hello. Welcome to Musiscore\n");
    printf("Here is where you learn to play music\n");
    printf("Press ENTER key to continue...\n");
    getchar();
    menu();


    while(a != 27)
    {

    //system("cls");
    a=getch();
    /*for detect the function\arrow keys
    we must call the getch() again
    testing if a is '0' or '0xE0'
    if (a==0 || a==0xE0) a=getch();*/

    if (a==27) //ESC for exit the 'while'
    break;
    else if (a==72)
    {system("cls");
    MenuPt1();}
    else if (a==80)
    {system("cls");
    MenuPt2();}
    else
    printf("",(int) a);
    }
    return 0;
    }
    Attached Files Attached Files
    Last edited by Alexander Gomes; 02-12-2015 at 08:26 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,615
    CAN YOUR PROGRAM YELL AT ME SOME MORE?

    Using the arrow keys is not easy. It depends on what platform the code will run on, and the code will only work on that platform. i.e. Code for windows is different than code for Macs or Linux, etc.

    The first step is to write code that can get a key - try reading this, you may be able to adapt the code:
    FAQ > How can I get input without having the user hit [Enter]? - Cprogramming.com

    The second step is detecting the arrow key. When you type a functional key while your getChar() function is executing, you may see some weird results. Function keys (like the arrows) are a combination of key codes. There is a code that tells you a function key is coming first, and then a second that is actually the arrow direction pushed. I don't remember the codes offhand so you will have to test to find out what they are first, then use them in your code, or maybe someone else will come along.

    Oh and Posting code? Read this first!
    Last edited by whiteflags; 02-12-2015 at 08:40 PM.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Edit your post and indent the code properly, there are sites online to do it, if you really can't do it yourself (your text editor should have autoindent based on language)

  4. #4
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    ..or you can use a real function, based on hardware signals.
    If you are working on Linux, use the WinAPI function GetAsyncKeyState();

  5. #5
    Registered Superuser nul's Avatar
    Join Date
    Nov 2014
    Location
    Earth
    Posts
    53
    ^^ I think you meant to say "if you are not on linux[..]".

    As far as the original question, take a look at something like the ncurses library. Not sure what the windows equivalent is.

  6. #6
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Omg yes. Sorry.. I have no clue now of the reason why I said linux.
    Could be something to do with the fact I hate Windows.

  7. #7
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Curses was created to help one do just this, portably, decades ago.

    POSIX users already have one installed (almost certainly), either ncurses (in Linux) or some BSD variant on the BSD systems and Macs. For DOS and Windows, there is PDCurses, which is not only free but in public domain. You don't need to compile it yourself, either, there are Windows DLLs you can download there.

    On Linux distributions, you probably do need to install the development version of the curses library; currently libncurses5-dev (Debian variants like Mint and Ubuntu) or libncurses-devel (RPM-based distributions) and/or their wide-character variants libncurses5w-dev or libncursesw-devel.

    Here is a trivial example:
    Code:
    #include <locale.h>
    #include "curses.h"
    
    int main()
    {
        int c;
    
        /* Initialize locale, for wide-character support. */
        setlocale(LC_ALL, "");
    
        /* Start curses mode. */
        initscr();
    
        /* No line buffering. */
        raw();
    
        /* No echoing keypresses to the screen. */
        noecho();
    
        /* We want F1 et cetera. */
        keypad(stdscr, TRUE);
    
        /* We "print" stuff to the screen,
         * moving the cursor to the next line '\n',
         * and to the beginning of that line '\r'. */
        printw("Press Q to quit.\n\r");
    
        /* We do need to tell the curses library
         * to update the changes made thus far,
         * i.e. that all changes are visible on screen. */
        refresh();
    
        while (1) {
    
            /* Wait for user input (one keypress). */
            c = getch();
    
            if (c == 'Q' || c == 'q')
                break;
    
            switch (c) {
            case KEY_UP:     printw("Up\n\r"); break;
            case KEY_DOWN:   printw("Down\n\r"); break;
            case KEY_LEFT:   printw("Left\n\r"); break;
            case KEY_RIGHT:  printw("Right\n\r"); break;
            case '\n':       printw("Enter\n\r"); break;
            case ' ':        printw("Space\n\r"); break;
            default:         printw("Code 0x%x\n\r", c);
            }
    
            refresh();
        }
    
        /* End curses mode. */
        endwin();
    
        return 0;
    }
    In Linux, you can compile and run the above using
    Code:
    gcc -Wall -I/usr/include/ncursesw example.c -lncursesw -o example
    ./example
    In Windows, the command depends on which compiler you are using, and where you extracted the PDCurses library and header files, but the command should be very similar. If you use one of the IDEs, their user community almost certainly has a curse template project for you to use. (I don't use Windows myself.)

    I like the wide-character variant (and used it in the example above), because I can use Unicode box-drawing characters like ╔╗, or even emoticons like 😎, as long as my source code is written using the UTF-8 character set. That means that if I add printw("Here's a kitty for you: 😺\n\r"); before the while loop, it'll show up just like a kitty face in all correctly-configured terminal emulators -- even in PuTTY (if configured to use UTF-8 to connect to this machine).

    I'm not certain if PDcurses handles that correctly for the Windows command-line prompt, though. If you use Windows, you could check and let us know!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make a cursor move using the arrow keys with ncurses?
    By Dave Couture in forum C Programming
    Replies: 3
    Last Post: 09-12-2012, 07:40 PM
  2. Selecting Menu using keyboard arrow keys
    By darkmessiah in forum C Programming
    Replies: 5
    Last Post: 06-07-2005, 11:07 AM
  3. arrow keys, ctrl and alt keys
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2002, 03:53 PM
  4. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 11:49 AM
  5. Menu system (Using the arrow keys)
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2001, 04:31 PM