title: How can i write a dll function that uses the C# Keyboard Class
body: <p>I want to use the following code to access the state of the keyboard at a certain time.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Windows.Forms;
  using System.Windows.Input;
  
  namespace some.any
  {
  
      public class ANY_CLASS
      {
      [STAThread] //seems to do nothing here
      public static short ThisIsCalledByAnExternalProgram()
      {
          try
          {
              if (Keyboard.IsKeyDown(Key.LeftAlt))
              {
                  return 1;
              }
              else
              {
                  return 0;
              }
          }
          catch (Exception e)
          {
                  MessageBox.Show(e.ToString());
                  return 2;
           }
      }
  }
  </code></pre>
  
  <p>This code requires some dlls to compile: WindowsBase.dll and PresentationCore.dll</p>
  
  <p>Keyboard requires a STA Thread, normally i would write the [STAThread] attribute to the main function and it would work, but this code will be used as a dll, so i can not do that. My function ThisIsCalledByAnExternalProgram() would have to run as an STA but it doesnt. </p>
  
  <p>How do i get this code to work as a dll? </p>
  
  <p>EDIT:
  What happens when you call ThisIsCalledByAnExternalProgram() within a STAThread flagged method?</p>
  
  <p>When i call the function with my external program i get an exception:
  System.InvalidOperationException: ...The calling thread must be STA, because many UI components require this.
  Stack is:</p>
  
  <pre><code>System.Windows.Input.InputManager..ctor()
  System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
  ThisIsCalledByAnExternalProgram()
  </code></pre>
  
  <p>EDIT#3:
  I misread the question - ...within a STAThread flagged...  i can currently not try this one. suppose it passes and works - this would still not solve the problem since i have no control over the calling program.</p>
  
  <p>EDIT#2:
  Use a Win32 hook:
  I want to stay within .net because of portability. All global hook variants are in the end dependent on the machine below the virtual machine, i want to use the prepared Keyboard class of c#.</p>
  
  <p>It works in a different context - here is a short demo:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows.Input;
  //Requires WindowsBase.dll
  //Requires PresentationCore.dll
  
  
  namespace KeyBoardDemo
  {
      class Program
      {
          [STAThread]
          static void Main(string[] args)
          {
              while (true)
              {
                  if (Keyboard.IsKeyDown(Key.LeftAlt))
                  {
                      Console.WriteLine("LEFT ALT IS PRESSED");
                  }
                  else
                  {
                      Console.WriteLine("LEFT ALT IS NOT PRESSED");
                  }
              }
          }
      }
  }
  </code></pre>