Silverlight application needs mouse click to activate keyboard

Go To StackoverFlow.com

2

I'm using VS2010,C# to develop a Silverlight online game, I use keyboard for getting user input, but users must initially click on silverligh canvas to activate keyboard (I have only one canvas in my scene which is not full screen, there is nothing else), how can I initially give focus to keyboard so that whenever the game starts user can play with keyboard (with no need to click on canvas), it is how I've set up my keyboard:

        public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(Page_Loaded);
    }


        void Page_Loaded(object sender, RoutedEventArgs e)
    {
        this.KeyDown += new KeyEventHandler(Page_KeyDown);
    }

        void Page_KeyDown(object sender, KeyEventArgs e)
    {
    ....
    }
2012-04-04 16:47
by Ali_dotNet


1

Try setting the focus first to your Silverlight plugin as described here: http://blog.falafel.com/Blogs/josh-eastburn/2011/03/10/Setting_Focus_on_the_Silverlight_Plugin_Object

Then apply the subsequent focus to the control you wish to accept the keyboard inputs.

An additional tip that's worked for me is to set the DOM focus to the plugin after it's instantiated in the DOM tree by running an initial script on the document ready function:

$('#silverlightPlugin').on(function(){
    $(this).focus();
}
2012-04-04 17:00
by KodeKreachor
what is the control that is accepting my keyboard input here? my canvas - Ali_dotNet 2012-04-04 17:04
It's hard to say without seeing your code, but I'm assuming that you have an element in your Silverlight application that is expected to trigger key board events (such as a textbox). Whatever element you want to fire the key events is what you'd want to set the focus to after your plugin receives focus - KodeKreachor 2012-04-04 17:15
Ads