It gives an error "this key is not in the dictionary" when you press Q, W ... only the keys from D1 to D0 and backsapace work. Question: why gives an error, everything seems to be correct?
public enum Notes { Do1, Re1, Mi1, Fa1, Col1, La1, Si1, Do2, Re2, Mi2, Fa2, Col2, La2, Si2, Do3, Re3, Mi3, Fa3, Col3, La3, Si3, Do4, Re4, Mi4, Fa4, Col4, La4, Si4, Do5, Re5, Mi5, Fa5, Col5, La5, Si5, Do6, DoSharp1, ReSharp1, FaSharp1, ColSharp1, LaSharp1, DoSharp2, ReSharp2, FaSharp2, ColSharp2, LaSharp2, DoSharp3, ReSharp3, FaSharp3, ColSharp3, LaSharp3, DoSharp4, ReSharp4, FaSharp4, ColSharp4, LaSharp4, DoSharp5, ReSharp5, FaSharp5, ColSharp5, LaSharp5 } private Dictionary<Notes, MediaPlayer> sounds = new Dictionary<Notes, MediaPlayer>(); private Dictionary<Keys, bool> pressStates = new Dictionary<Keys, bool>(); private Dictionary<Keys, Notes> KeySounds = new Dictionary<Keys, Notes> { { Keys.D1, Notes.Do1 }, .... //Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Π½ΠΎΡΡ ΠΏΠ΅ΡΠ΅ΡΠΈΡΠ»Π΅Π½ΠΈΡ Notes Π·Π°Π΄Π°Π΅ΠΌ ΠΊΠ»Π°Π²ΠΈΡΡ ΠΊΠ»Π°Π²ΠΈΠ°ΡΡΡΡ .... { Keys.NumPad9, Notes.LaSharp5 } }; private void Form1_Load(object sender, EventArgs e) { //ΠΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΠ΅ΠΌ ΡΠ»ΠΎΠ²Π°ΡΠΈ foreach (Notes s in Enum.GetValues(typeof(Notes)).Cast<Notes>()) { sounds.Add(s, new MediaPlayer()); pressStates.Add((Keys)s, false); } //ΠΠ°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΡΠ»ΠΎΠ²Π°ΡΡ Π·Π²ΡΠΊΠ°ΠΌΠΈ sounds[Notes.Do1].Open(new Uri("s//_1.wav", UriKind.Relative)); .... //ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Π½ΠΎΡΡ Π·Π°Π΄Π°Π΅ΠΌ Π·Π²ΡΠΊΠΎΠ²ΠΎΠΉ ΡΠ°ΠΉΠ» .... sounds[Notes.LaSharp5].Open(new Uri("s//25.wav", UriKind.Relative)); } private void Form1_KeyDown(Object sender, KeyEventArgs e) { if (!KeySounds.ContainsKey(e.KeyCode)) return; if (!pressStates[e.KeyCode]) { pressStates[e.KeyCode] = true; PlaySound(KeySounds[e.KeyCode]); } } private void Form1_KeyUp(Object sender, KeyEventArgs e) { if (!KeySounds.ContainsKey(e.KeyCode)) return; if (pressStates[e.KeyCode]) { pressStates[e.KeyCode] = false; StopSound(KeySounds[e.KeyCode]); } }
Keys? and how does it compare to theNotestype? - IgorpressStatesdictionary. It must be initialized in a separate cycle from thesounds, because now you have the wrong keys in it, but the casting is correct and the compiler does not say anything about it. - rdorn