示例#1
0
        /// <summary>
        /// A keydown event was detected.  Check if this is one of the keys
        /// we are interested in.  The valid keys are defined as the 'source'
        /// attribute in a switch object
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">args</param>
        private void KeyboardHook_KeyDown(object sender, KeyEventArgs e)
        {
            if (actuatorState != State.Running)
            {
                return;
            }

            var s = String.Format("Keydown {0}.  Alt: {1} Ctrl: {2}", e.KeyCode, e.Alt, e.Control);

            Log.Debug(s);

            // check if this is one of the keys we recognize.  If so, trigger
            // a switch-activated event
            var actuatorSwitch = findActuatorSwitch(e.KeyCode.ToString());

            if (actuatorSwitch != null)
            {
                e.Handled                 = true;
                actuatorSwitch.Action     = SwitchAction.Down;
                actuatorSwitch.Confidence = 100;
                OnSwitchActivated(actuatorSwitch);
            }
            else
            {
                KeyStateTracker.KeyDown(e.KeyCode);

                // Get the status of ctrl, shift and alt keys from
                // the tracker.  Then format the final key. For
                // eg:  Ctrl+Alt+T
                string hotKey = KeyStateTracker.GetKeyPressedStatus();
                if (String.IsNullOrEmpty(hotKey))
                {
                    hotKey = e.KeyCode.ToString();
                }
                else
                {
                    hotKey += "+" + e.KeyCode.ToString();
                }

                Log.Debug("KeyStateTracker.KeyString: " + hotKey);

                // check which switch handles this hotkey and trigger it
                actuatorSwitch = findActuatorSwitch(hotKey);
                if (actuatorSwitch != null)
                {
                    actuatorSwitch.Action     = SwitchAction.Trigger;
                    actuatorSwitch.Confidence = 100;
                    OnSwitchTriggered(actuatorSwitch);
                }
                else
                {
                    if (EvtKeyDown != null)
                    {
                        EvtKeyDown(this, e);
                    }
                }
            }
        }