-3

I have a program where a certain sound is played corresponding to a key pressed on the keyboard. But every time I press a key, I have to press Enter for the sound to be played. What I actually want to do is to press the key without having to press Enter and a sound should be played. How can I do this?

I'm using Windows 10. Here is my code:

while(1)
{
  cin>>button;

  switch (button)
  {
    case 'q': 
    PlaySound( TEXT("C:\\Users\\Gumm\\Downloads\\real sound of notes\\ardha chapu.wav"),NULL,SND_SYNC );
    break;

    case 'w':           
    PlaySound( TEXT("C:\\Users\\Gumm\\Downloads\\real sound of notes\\chapu"),NULL,SND_SYNC );
    break;

    case 'e':
    PlaySound( TEXT("C:\\Users\\Gumm\\Downloads\\real sound of notes\\dheem"),NULL,SND_SYNC );
    break;

    default:
    printf("No sound");
  }
}
honk
  • 9,137
  • 11
  • 75
  • 83

1 Answers1

0

Assuming you already have a working program (except mandatory pressing of return key), here is a solution specific to your problem:

#include <ncurses.h>

int kbhit(void)
{
    int ch = getch();
    if (ch != ERR) {
        ungetch(ch);
        return 1;
    }
    else
        return 0;
}

int main(void)
{
    initscr();
    cbreak();
    noecho();
    while (true) {
        if (kbhit()) {
            switch(getch())
            {
                case 10:
                    printw("Return key pressed!\n");
                    // your code to play a sound when `return` key pressed
                    break;
                case 110:   // for small `n`
                case 78:    // for capital `N`
                    printw("N or n key pressed!\n");
                    // your code to play sound when `n` or `N` key pressed
                    break;
                    // ...
            }
            refresh();
        }
    }
    return 0;
}