WPF Keyboard input UI thread issue?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    WPF Keyboard input UI thread issue?

    I have a simple application which includes a default window (named Window1) with no additional xaml.

    Window1 consists of a timerclass object which contains a timer running at 60hrz.

    Code:
            
    public Window1()
            {//in Window1.xaml.cs
                MainTimerClass = new TimerClass(); //my timer class object
            }
    The Timer class consists of an object which handles keyboard operations
    Keyboard class is defined as below.

    Code:
     class KeyboardHandle
        {
            public KeyboardHandle()
            {
            }
    
            public void ProcessKeyboardEvent(ref Hashtable MappedKeyTable)
            {
                foreach (DictionaryEntry de in MappedKeyTable)
                {
                    Key TempKey = (Key)de.Key;
                    if(Keyboard.IsKeyDown(TempKey))
                    {
                        int pq = (int)de.Value;
                    }
                }
            }
    The Timer calls the ProcessKeyboard Event 60 times a second. When i hit the line of code Keyboard.IsKeyD own(TempKey)) i get the runtime exception "The calling thread must be STA, because many UI components require this"

    Basically I want my code to check a given set of keyboard keys to see if they are pressed or not. I saw that the GUI element of WPF runs on a different thread and I have to use the Dispatcher.Invo ke methods, but 1.) Why is the keyboard tied to a UI thread? two How can I use the dispatcher.Invo ke to get the keyboard state? Any ideas on how to do this? I could just use windows hooks or Wnd_Proc, but I would prefer not to... Any ideas/suggestions? Let me know if you need anything else posted.
Working...