I have a game that I want to pause whenever a user navigates to another application. For example, when the enchantment menu is selected, the user presses the Windows key, Alt-Tab on another application, clicks on another application or something else that might cause the application to lose focus.
Of course, this should be trivial! I only have Page and Canvas , and I tried the GotFocus and LostFocus on Canvas , but they do not fire.
The closest I came using PointerCaptureLost on CoreWindow after capturing the pointer. This works for switching applications when the charms menu is selected, but it does not work when the Windows key is pressed.
EDIT:
With the help of Chris Bowen below, the final βsolutionβ is as follows:
public MainPage() { this.InitializeComponent(); CapturePointer(); Window.Current.CoreWindow.PointerCaptureLost += PointerCaptureLost; Window.Current.CoreWindow.PointerPressed += PointerPressed; Window.Current.VisibilityChanged += VisibilityChanged; } private void VisibilityChanged(object sender, VisibilityChangedEventArgs e) { if(e.Visible) { CapturePointer(); } else { Pause(); } } void PointerPressed(CoreWindow sender, PointerEventArgs args) { CapturePointer(); } private void CapturePointer() { if(hasCapture == false) { Window.Current.CoreWindow.SetPointerCapture(); hasCapture = true; } } void PointerCaptureLost(CoreWindow sender, PointerEventArgs args) { hasCapture = false; Pause(); } private bool hasCapture;
It still seems like they should be easier, so please let me know if you find something more elegant.
source share