-3

So i`m trying to create a piano game. where specific keys act like the piano keys.For example: if "A" is being pressed playback audio file for note C if "s" is being pressed playback audio file for note D and so on. I wrote this:

piano:         
 if (pressedKey.Key == ConsoleKey.A) ;
             {
                 Console.Beep();


                 {
                     goto piano;

the beep will be replaced with the audio file this is just for testing. So when the program reaches this piece of code and i hit "a" it starts beeping over and over and it wont stop until the program is closed. I want it to beep once every time I hit "A". How do I achieve this? Sorry for the dumb question and bad English.

1 Answers1

0

Well, it seems like you want to play some sound on key pressing in console application, This is how you are gonna Do that!

public static void PressAnyKey()
{
    Console.WriteLine("Stroke Keys!");
    do
    {
        var mykey = Console.ReadKey(true); //Read The Key

        switch (mykey.Key)
        {
            case ConsoleKey.UpArrow:
                Console.WriteLine("UpArrow Pressed");
                //do your stuff
                break;
            case ConsoleKey.DownArrow:
                Console.WriteLine("DownArrow Pressed");
                //do your stuff
                break;
            case ConsoleKey.RightArrow:
                Console.WriteLine("RightArrow Pressed");
                //do your stuff
                break;
            case ConsoleKey.LeftArrow:
                Console.WriteLine("LeftArrow Pressed");
                //do your stuff
                break;
        }
    } while (true);
}

and if you want to emit some sound from system speaker you can use Console.Beep(Frequency,Duration) Method and int Frequency & int Duration variables to adjust it's Frequency and Duration!
this is how the Function will be after adding few lines!

public static void BeepOnKey()
{
    int frequency = 10000;
    int duration = 100;

    Console.WriteLine("Use keyboard arrows to adjust frequency and duration");
    do
    {
        while (!Console.KeyAvailable)
        {
            Console.Beep(frequency, duration);
            //this method emits beep sound from system speakers
        }

        var mykey = Console.ReadKey(true); //Read The Key

        switch (mykey.Key)
        {
            case ConsoleKey.UpArrow:
                Console.WriteLine("UpArrow Pressed");
                frequency += 100;
                frequency = Math.Min(frequency, 15000);
                //do your stuff
                break;
            case ConsoleKey.DownArrow:
                Console.WriteLine("DownArrow Pressed");
                frequency -= 100;
                frequency = Math.Max(frequency, 1000);
                //do your stuff
                break;
            case ConsoleKey.RightArrow:
                Console.WriteLine("RightArrow Pressed");
                duration += 100;
                duration = Math.Min(duration, 1000);
                //do your stuff
                break;
            case ConsoleKey.LeftArrow:
                Console.WriteLine("LeftArrow Pressed");
                duration -= 100;
                duration = Math.Max(duration, 100);
                //do your stuff
                break;
        }
    } while (true);
}
Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42