private void pushToTalkKeyTimer_Tick(object sender, EventArgs e) { pushToTalkKeyTimer.Interval = 10; //lowering the interval to avoid missing key presses (e.g. when an input is corrected) if (Keyboard.IsKeyDown(Keyboard.Keys.Escape)) { tbPushToTalkKey.Text = ""; pushToTalkKey = default; } else { foreach (Keyboard.Keys key in Enum.GetValues(typeof(Keyboard.Keys))) { if (Keyboard.IsKeyDown(key)) { tbPushToTalkKey.Text = key.ToString(); pushToTalkKey = key; break; } } } }
private void mainTimer_Tick(object sender, EventArgs e) { if (cbEnable.Checked) { int keysPressed = 0; if (soundHotkeys.Count > 0) //check that required keys are pressed to play sound { IntPtr foregroundWindow = Helper.GetForegroundWindow(); for (int i = 0; i < soundHotkeys.Count; i++) { keysPressed = 0; if (soundHotkeys[i].Keys.Length == 0) { continue; } if (soundHotkeys[i].WindowTitle != "" && !Helper.isForegroundWindow(foregroundWindow, soundHotkeys[i].WindowTitle)) { continue; } for (int j = 0; j < soundHotkeys[i].Keys.Length; j++) { if (Keyboard.IsKeyDown(soundHotkeys[i].Keys[j])) { keysPressed++; } } if (keysPressed == soundHotkeys[i].Keys.Length) { if (keysJustPressed == soundHotkeys[i].Keys) { continue; } if (soundHotkeys[i].Keys.Length > 0 && soundHotkeys[i].Keys.All(x => x != 0) && soundHotkeys[i].SoundLocations.Length > 0 && soundHotkeys[i].SoundLocations.Length > 0 && soundHotkeys[i].SoundLocations.Any(x => File.Exists(x))) { if (cbEnablePushToTalk.Checked && !keyUpPushToTalkKey && !Keyboard.IsKeyDown(pushToTalkKey) && Helper.isForegroundWindow((string)cbWindows.SelectedItem)) { keyUpPushToTalkKey = true; bool result = Keyboard.sendKey(pushToTalkKey, true); Thread.Sleep(100); } playKeySound(soundHotkeys[i]); return; } } else if (keysJustPressed == soundHotkeys[i].Keys) { keysJustPressed = null; } } keysPressed = 0; } if (XMLSettings.soundboardSettings.StopSoundKeys != null && XMLSettings.soundboardSettings.StopSoundKeys.Length > 0) //check that required keys are pressed to stop all sounds { for (int i = 0; i < XMLSettings.soundboardSettings.StopSoundKeys.Length; i++) { if (Keyboard.IsKeyDown(XMLSettings.soundboardSettings.StopSoundKeys[i])) { keysPressed++; } } if (keysPressed == XMLSettings.soundboardSettings.StopSoundKeys.Length) { if (keysJustPressed == null || !keysJustPressed.Intersect(XMLSettings.soundboardSettings.StopSoundKeys).Any()) { if (playbackWaveOut != null && playbackWaveOut.PlaybackState == PlaybackState.Playing) { playbackWaveOut.Stop(); } keysJustPressed = XMLSettings.soundboardSettings.StopSoundKeys; return; } } else if (keysJustPressed == XMLSettings.soundboardSettings.StopSoundKeys) { keysJustPressed = null; } keysPressed = 0; } if (XMLSettings.soundboardSettings.LoadXMLFiles != null && XMLSettings.soundboardSettings.LoadXMLFiles.Length > 0) //check that required keys are pressed to load XML file { for (int i = 0; i < XMLSettings.soundboardSettings.LoadXMLFiles.Length; i++) { if (XMLSettings.soundboardSettings.LoadXMLFiles[i].Keys.Length == 0) { continue; } keysPressed = 0; for (int j = 0; j < XMLSettings.soundboardSettings.LoadXMLFiles[i].Keys.Length; j++) { if (Keyboard.IsKeyDown(XMLSettings.soundboardSettings.LoadXMLFiles[i].Keys[j])) { keysPressed++; } } if (keysPressed == XMLSettings.soundboardSettings.LoadXMLFiles[i].Keys.Length) { if (keysJustPressed == null || !keysJustPressed.Intersect(XMLSettings.soundboardSettings.LoadXMLFiles[i].Keys).Any()) { if (!string.IsNullOrWhiteSpace(XMLSettings.soundboardSettings.LoadXMLFiles[i].XMLLocation) && File.Exists(XMLSettings.soundboardSettings.LoadXMLFiles[i].XMLLocation)) { keysJustPressed = XMLSettings.soundboardSettings.LoadXMLFiles[i].Keys; loadXMLFile(XMLSettings.soundboardSettings.LoadXMLFiles[i].XMLLocation); } return; } } else if (keysJustPressed == XMLSettings.soundboardSettings.LoadXMLFiles[i].Keys) { keysJustPressed = null; } } keysPressed = 0; } if (keyUpPushToTalkKey) { if (!Keyboard.IsKeyDown(pushToTalkKey)) { keyUpPushToTalkKey = false; } if (playbackWaveOut.PlaybackState != PlaybackState.Playing || !Helper.isForegroundWindow((string)cbWindows.SelectedItem)) { keyUpPushToTalkKey = false; Keyboard.sendKey(pushToTalkKey, false); } } } }