Detecting Loss of Keyboard Focus in a Silverlight Application
I have a keyboard-driven Silverlight game and I want it to pause when it loses keyboard focus (for example, the user clicks on a different part of the hosting webpage or goes to a different browser tab).
I used this in Silverlight 1.1, subscribing to the LostFocus event on my RootVisual UserControl, but in the last two versions of Silverlight, I found that this event seems to fire unexpectedly right after the button is clicked in my application (in Silverlight 2 it was triggered once, in Silverlight 3 twice!).
Is there a way in javascript on the hosting page or in Silverlight to more accurately detect focus loss?
source to share
I finally found a solution to this problem. The RoutedEventArgs property on the LostFocus event has an OriginalSource property that allows me to ignore any LostFocus events that originate from the RootVisual children.
void Page_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource == this)
{
Pause();
}
}
source to share