/// <summary> /// Initializes the mouse events for the window. /// </summary> private void InitializeMouseEvents() { #region Toggle Fullscreen with Double Click Media.PreviewMouseDoubleClick += (s, e) => { if (s != Media) { return; } e.Handled = true; ToggleFullscreenCommand.Execute(); }; #endregion #region Exit fullscreen with Escape key PreviewKeyDown += (s, e) => { if (e.Key == Key.Escape && WindowStyle == WindowStyle.None) { e.Handled = true; ToggleFullscreenCommand.Execute(); } }; #endregion #region Handle Zooming with Mouse Wheel PreviewMouseWheel += (s, e) => { var transform = Media.RenderTransform as ScaleTransform; if (transform == null) { transform = new ScaleTransform(1, 1); Media.RenderTransformOrigin = new Point(0.5, 0.5); Media.RenderTransform = transform; } var delta = e.Delta / (Math.Pow(e.Delta, 2d) / 8d); transform.ScaleX += delta; transform.ScaleY += delta; if (transform.ScaleX < 0.1d || transform.ScaleY < 0.1) { transform.ScaleX = 0.1d; transform.ScaleY = 0.1d; } else if (transform.ScaleX > 5d || transform.ScaleY > 5) { transform.ScaleX = 5; transform.ScaleY = 5; } }; #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 = System.Windows.Input.Cursors.None; var sb = Player.FindResource("HideControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } else { if (Controls.Opacity != 1d) { Cursor = System.Windows.Input.Cursors.Arrow; var sb = Player.FindResource("ShowControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } }; mouseMoveTimer.Start(); #endregion }
/// <summary> /// Initializes the mouse events for the window. /// </summary> private void InitializeInputEvents() { #region Allow Keyboard Control 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); // Pause if (togglePlayPauseKeys.Contains(e.Key) && Media.IsPlaying) { PauseCommand.Execute(); return; } // Play if (togglePlayPauseKeys.Contains(e.Key) && Media.IsPlaying == false) { PlayCommand.Execute(); return; } // Seek to left if (e.Key == Key.Left) { if (Media.IsPlaying) { await Media.Pause(); } Media.Position -= Media.FrameStepDuration; } // Seek to right if (e.Key == Key.Right) { if (Media.IsPlaying) { await Media.Pause(); } Media.Position += Media.FrameStepDuration; } // 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 += (s, e) => { if (s != Media) { return; } e.Handled = true; ToggleFullscreenCommand.Execute(); }; #endregion #region Exit fullscreen with Escape key PreviewKeyDown += (s, e) => { if (e.Key == Key.Escape && WindowStyle == WindowStyle.None) { e.Handled = true; ToggleFullscreenCommand.Execute(); } }; #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 = System.Windows.Input.Cursors.None; var sb = Player.FindResource("HideControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } else { if (Controls.Opacity != 1d) { Cursor = System.Windows.Input.Cursors.Arrow; var sb = Player.FindResource("ShowControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } }; mouseMoveTimer.Start(); #endregion }
/// <summary> /// Initializes the mouse events for the window. /// </summary> private void InitializeInputEvents() { window.PreviewKeyDown += (s, e) => { Debug.WriteLine($"Key Down: {e.OriginalSource} - {e.Key}"); }; #region Toggle Fullscreen with Double Click Media.PreviewMouseDoubleClick += (s, e) => { if (s != Media) { return; } e.Handled = true; ToggleFullscreenCommand.Execute(); }; #endregion #region Exit fullscreen with Escape key PreviewKeyDown += (s, e) => { if (e.Key == Key.Escape && WindowStyle == WindowStyle.None) { e.Handled = true; ToggleFullscreenCommand.Execute(); } }; #endregion #region Handle Zooming with Mouse Wheel MouseWheel += (s, e) => { if (Media.IsOpen == false || Media.IsOpening) { return; } var delta = SnapToMultiple(e.Delta / 2000d, 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 = System.Windows.Input.Cursors.None; var sb = Player.FindResource("HideControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } else { if (Controls.Opacity != 1d) { Cursor = System.Windows.Input.Cursors.Arrow; var sb = Player.FindResource("ShowControlOpacity") as Storyboard; Storyboard.SetTarget(sb, Controls); sb.Begin(); } } }; mouseMoveTimer.Start(); #endregion }