KeyDown and KeyUp Multiple

I am creating a piano in C Sharp, and currently I have keyboard keys for playing sounds. For example, the A key plays Note C. The Problem I have, I want to simultaneously press several keys and make a sound. obviously, I don't want all the combinations in the keyDown class to be needed, since I would have to do thousands of if statements. Anyway, around?

+4
source share
2 answers

Windows works with only one message queue, so only a message with a key will be processed at any given time. You can do all the key events in a short period of time (0.5 seconds for instace), save all the pressed keys in a list or queue, and then play all the sounds according to the keys asynchronously (using streams). I have never done this before, but I think it should work. Hope helps ...

EDIT


Ok, let's see: first a list of where to save the keys

List<Key> _keys = new List<Key>();

Then start the timer to check the pressed keys for the time interval:

  var t = new System.Timers.Timer(500); //you may try using an smaller value t.Elapsed += t_Elapsed; t.Start(); 

Then the t_Elapsed method (note that if you are in WPF you must use DispatcherTimer , this timer is on System.Timers )

  void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (_keys.Count > 0) { //Here get all keys and play the sound using threads _keys.Clear(); } } 

And then the on down down method:

 void OnKeyDownMethod(object sender, KeyPressedEventArgs e) //not sure this is the name of the EventArgs class { _keys.Add(e.Key); //need to check } 

You can try this, hopefully it will be useful.

+1
source

You can check this link. This will help you solve your request.

Also check out this Thread

And if you are thinking of using MIDI, then check this out.

+1
source

All Articles