Thread runing on backgrount

Hello, I make this simple code to enable on background loop to check one or more is press
keys on keyboard.

    public void StartKeyboard()
    {
        Thread TH = new Thread(Keyboardd);
        TH.SetApartmentState(ApartmentState.STA); //Set the thread to STA
        TH.Start();
    }

     public void Keyboardd()
    {
        int runing = 1;
        while (true)
        {

            Thread.Sleep(40);

            if ((Keyboard.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0 )
            {

                MessageBox.Show("LeftShift touch ");
               
            }

            RhinoApp.WriteLine("Running on background task " + runing++.ToString());//just check if Thread is runing

          
        }

    }

This code work is working on bakground but there is a problem, once I hit the Shift key one frist time
the message popup and the code is runing, but if hit Shift key again the message dont popup, but
the scrit keep runing on background.

There is something that is escaping me, but what is it?

Thanks

Ok,

I do not know why but the test I was doing does not work, the message only appears on the first press of the key.

Anyway it is not important, the intention was to monitor the keys to use while true loop, so , so I’ll leave the solution found to whomever want one similar solution:


I create a couple of Global variable to save the key state,


I make one while true loop on Threadto run in the background of the Rhino:

ex:

public void Keyboardd()
{
while (true)
{
                Thread.Sleep(40);
                // mouse buttons=====================
                if (Control.MouseButtons == MouseButtons.Left)
                {
                    GlobalVar.mouse_Left = true;
                }
                else
                {
                    GlobalVar.mouse_eLeft = false;
                }

                if (Control.MouseButtons == MouseButtons.Right)
                {
                    GlobalVar.mouse_Righ = true;
                }
                else
                {
                    GlobalVar.mouse_Righ = false;
                }

                //shift====================================================
                if (Keyboard.IsKeyDown(Key.LeftShift))
                {
                    GlobalVar.hotkey_shift = true;
                }
                else
                {
                    GlobalVar.hotkey_shift = false;
                }

                //R====================================================

                if (Keyboard.IsKeyDown(Key.R))
                {
                    GlobalVar.hotkey_r = true;
                }
                else
                {
                    GlobalVar.hotkey_r = false;
                }


                //F====================================================

                if (Keyboard.IsKeyDown(Key.F))
                {
                    GlobalVar.hotkey_f = true;

                }
                else
                {
                    GlobalVar.hotkey_f = false;
                }

            }

        }


In any part of the application can be checked the variable referring to each key:

      if (GlobalVar.mouse_Left == true)
                {
                  MessageBox.Show("Key press");
                  //code
                }

I do not know if it’s the best option, but it was the one I found to use while running for loops.

Thanks