/// <summary> /// Initializes the mouse events for the window. /// </summary> private void InitializeInputEvents() { #region Keyboard Controls var togglePlayPauseKeys = new[] { Key.Play, Key.MediaPlayPause, Key.Space }; window.PreviewKeyDown += async(s, e) => { // Console.WriteLine($"KEY: {e.Key}, SRC: {e.OriginalSource?.GetType().Name}"); if (e.OriginalSource is TextBox) { return; } // Keep the key focus on the main window FocusManager.SetIsFocusScope(this, true); FocusManager.SetFocusedElement(this, this); if (e.Key == Key.G) { Subtitles.SetForeground(Media, System.Windows.Media.Brushes.Yellow); } // Pause if (togglePlayPauseKeys.Contains(e.Key) && Media.IsPlaying) { await PauseCommand.ExecuteAsync(); return; } // Play if (togglePlayPauseKeys.Contains(e.Key) && Media.IsPlaying == false) { await PlayCommand.ExecuteAsync(); return; } // Seek to left if (e.Key == Key.Left) { if (Media.IsPlaying) { await Media.Pause(); } Media.Position -= TimeSpan.FromMilliseconds( Media.FrameStepDuration.TotalMilliseconds * (Media.SpeedRatio >= 1 ? Media.SpeedRatio : 1)); } // Seek to right if (e.Key == Key.Right) { if (Media.IsPlaying) { await Media.Pause(); } Media.Position += TimeSpan.FromMilliseconds( Media.FrameStepDuration.TotalMilliseconds * (Media.SpeedRatio >= 1 ? Media.SpeedRatio : 1)); } // Volume Up if (e.Key == Key.Add || e.Key == Key.VolumeUp) { Media.Volume += 0.05; return; } // Volume Down if (e.Key == Key.Subtract || e.Key == Key.VolumeDown) { Media.Volume -= 0.05; return; } // Mute/Unmute if (e.Key == Key.M || e.Key == Key.VolumeMute) { Media.IsMuted = !Media.IsMuted; return; } // Increase speed if (e.Key == Key.Up) { Media.SpeedRatio += 0.05; return; } // Decrease speed if (e.Key == Key.Down) { Media.SpeedRatio -= 0.05; return; } // Reset changes if (e.Key == Key.R) { Media.SpeedRatio = 1.0; Media.Volume = 1.0; Media.Balance = 0; Media.IsMuted = false; } }; #endregion #region Toggle Fullscreen with Double Click Media.PreviewMouseDoubleClick += async(s, e) => { if (s != Media) { return; } e.Handled = true; await ToggleFullscreenCommand.ExecuteAsync(); }; #endregion #region Exit fullscreen with Escape key PreviewKeyDown += async(s, e) => { if (e.Key == Key.Escape && WindowStyle == WindowStyle.None) { e.Handled = true; await ToggleFullscreenCommand.ExecuteAsync(); } }; #endregion #region Handle Zooming with Mouse Wheel MouseWheel += (s, e) => { if (Media.IsOpen == false || Media.IsOpening) { return; } var delta = (e.Delta / 2000d).ToMultipleOf(0.05d); MediaZoom = Math.Round(MediaZoom + delta, 2); }; UrlTextBox.PreviewMouseWheel += (s, e) => { e.Handled = true; }; #endregion #region Handle Play Pause with Mouse Clicks /* * Media.PreviewMouseDown += (s, e) => * { * if (s != Media) return; * if (Media.IsOpen == false || Media.CanPause == false) return; * if (Media.IsPlaying) * PauseCommand.Execute(); * else * PlayCommand.Execute(); * }; */ #endregion #region Mouse Move Handling (Hide and Show Controls) LastMouseMoveTime = DateTime.UtcNow; MouseMove += (s, e) => { var currentPosition = e.GetPosition(window); if (currentPosition.X != LastMousePosition.X || currentPosition.Y != LastMousePosition.Y) { LastMouseMoveTime = DateTime.UtcNow; } LastMousePosition = currentPosition; }; MouseLeave += (s, e) => { LastMouseMoveTime = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(10)); }; var mouseMoveTimer = new DispatcherTimer(DispatcherPriority.Background) { Interval = TimeSpan.FromMilliseconds(150), IsEnabled = true }; mouseMoveTimer.Tick += (s, e) => { var elapsedSinceMouseMove = DateTime.UtcNow.Subtract(LastMouseMoveTime); if (elapsedSinceMouseMove.TotalMilliseconds >= 3000 && Media.IsOpen && Controls.IsMouseOver == false && OpenMediaPopup.IsOpen == false && DebugWindowPopup.IsOpen == false && SoundMenuPopup.IsOpen == false) { if (Controls.Opacity != 0d) { Cursor = Cursors.None; var sb = Player.FindResource("HideControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } else { if (Controls.Opacity != 1d) { Cursor = Cursors.Arrow; var sb = Player.FindResource("ShowControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } }; mouseMoveTimer.Start(); #endregion }