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);
}