WPF using XNA, Keyboard.GetState().GetPressedKeys() always empty!

0 votes
asked Dec 4, 2010 by tdaver

I have a module written using XNA which polls the GamePad and the Keyboard on a background thread, and calls some callbacks. No Game class whatsoever. I want to use this module from a WPF application, calling Dispatcher.BeginInvoke on the callback, and makeing some changes on the display (which contains no TextBox, etc), based on the callbacks parameters. It works perfectly with the GamePad, but the Keyboard.GetState().GetPressedKeys() array is always empty! I think it has something to do with WPF message loop catching the keyboard events before I can poll them in XNA, or there is nothing for xna to set the keyboard focus to. Do you have any tips? How to pass keyboard state to XNA, so I don't have to rewrite the keyboard handling using WPF classes? thanks

1 Answer

0 votes
answered Dec 4, 2010 by tdaver

Never mind, I worked it out! The problem wasn't with focus, it was with threading! I exposed a callback on polling class, and when I wanted to call Keyboard.GetState(), I called the callback, which (in WPF) was a Dispatcher.Invoke(()=>poll.GetKeyboardState()) call, so this time the GetState() was on the same thread as the WPF control, so it worked!!! Hoorah!

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...