示例#1
0
        //adds a key to Keys, returns true if the key has not been pressed recently
        private static bool AddKey(ScreenKey key)
        {
            int index = Keys.FindIndex(k => k.Name == key.Name);

            if (index >= 0 && index < Keys.Count)            //the key has been pressed before
            {
                Keys[index].KeyDownState = KEYDOWNREFRESH;
                return(false);
            }
            else                //'new' key

            //check for pause, and exit before all else
            {
                if (Scene.Current != null && key.Name == PauseKeyName)
                {
                    //toggle the pause
                    if (Scene.Current.PAUSED)
                    {
                        Resume();
                    }
                    else
                    {
                        Pause();
                    }
                }
                else if (Scene.Current != null && key.Name == ExitKeyName && Scene.Current.PAUSED)
                {
                    Resume();
                    Scene.Current.Stop();
                    Environment.Exit(0);
                }
                else
                {
                    //check for key if looking for specific key
                    if (lookingForKey != "")
                    {
                        if (lookingForKey == key.Name)
                        {
                            //found the key, run the action and turn input back to normal
                            lookingForKey = "";

                            onFound();
                        }
                    }
                    else
                    {                       //not looking for any key so just do normal input
                        key.KeyDownState = KEYDOWNFRESH;
                        Keys.Add(key);
                    }
                }

                return(true);
            }
        }
示例#2
0
        public static void Start()
        {
            if (Active)
            {
                return;
            }

            Active = true;

            //set up the thread that gets input from the user
            keyInputThread = new Thread(new ThreadStart(() => {
                while (Active)
                {
                    //wait until a key is pressed
                    while (!Console.KeyAvailable)
                    {
                        Thread.Sleep(1);
                    }

                    //key has been pressed...
                    ConsoleKeyInfo info = Console.ReadKey(true);
                    string name         = info.Key.ToString();
                    ScreenKey key       = new ScreenKey(name, info.KeyChar);

                    AddKey(key);
                }
            }));

            //set up the thread that managers events
            keyEventManagerThread = new Thread(new ThreadStart(() => {
                while (Active)
                {
                    watch.Start();
                    for (int i = Keys.Count - 1; i >= 0; i--)
                    {
                        if (--Keys[i].KeyDownState <= 0)
                        {
                            RemoveKey(Keys[i]);
                        }
                    }
                    watch.Stop();
                    Thread.Sleep(Math.Max(0, 30 - watch.Elapsed.Milliseconds));
                    watch.Reset();
                }
            }));

            //start both of the threads
            keyInputThread.Name = "KeyInput";
            keyInputThread.Start();

            keyEventManagerThread.Name = "KeyEventManager";
            keyEventManagerThread.Start();
        }
示例#3
0
 //removes a key
 private static void RemoveKey(ScreenKey key)
 {
     Keys.Remove(key);
 }