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)
    {
    ....
    }
4

1 回答 1

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-04T17:00:11.850 回答