Пример #1
0
        private static bool AddMapping(KeyMapping map, bool noStackNoEventRaised)
        {
            if (!map.IsValid())
            {
                return(false);
            }

            int scancode = map.From.Scancode;
            int extended = map.From.Extended;

            // If user is remapping Left Ctrl, Left Alt, or Delete then s/he must confirm
            // that it could be goodbye to CTRL-ALT-DEL

            if ((scancode == 29 && extended == 0) || (scancode == 56 && extended == 0) ||
                (scancode == 83 && extended == 224))
            {
                string action = IsDisabledMapping(map) ? "disable " : "remap ";

                string warning = "You are attempting to " + action + map.From.Name +
                                 " which is required for CTRL-ALT-DELETE." + (char)13 +
                                 "If you continue you may not be able to log on" +
                                 " to your PC.";

                string question = "Are you really sure you want to " + action + "this key?";

                if (operatingSystemCapability.ImplementsTaskDialog)
                {
                    TaskDialogResult dr = FormsManager.ShowTaskDialog(question, warning, "Key Mapper",
                                                                      TaskDialogButtons.Yes | TaskDialogButtons.No,
                                                                      TaskDialogIcon.Question);
                    if (dr != TaskDialogResult.Yes)
                    {
                        return(false);
                    }
                }
                else
                {
                    DialogResult dr = MessageBox.Show(warning + (char)13 + (char)13 + question, "Key Mapper",
                                                      MessageBoxButtons.YesNo,
                                                      MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, 0);

                    if (dr != DialogResult.Yes)
                    {
                        return(false);
                    }
                }
            }

            // If user is remapping Pause, then suggest they will want to disable Num Lock as well.

            bool disableNumLock = false;

            if (scancode == 29 && extended == 225 && IsDisabledMapping(map) == false)
            {
                // Is Num Lock already disabled or remapped?
                bool numLockIsDisabled = false;
                bool numLockIsMapped   = false;

                foreach (KeyMapping km in _bootMappings)
                {
                    if (km.From.Scancode == 69)
                    {
                        if (IsDisabledMapping(km))
                        {
                            numLockIsDisabled = true;
                        }
                        else
                        {
                            numLockIsMapped = true;
                        }
                    }
                }

                // Num Lock could be disabled or mapped in boot
                // but overridden in user mappings.
                foreach (KeyMapping km in _userMappings)
                {
                    if (km.From.Scancode == 69)
                    {
                        if (IsDisabledMapping(km))
                        {
                            numLockIsDisabled = true;
                        }
                        else
                        {
                            numLockIsMapped = true;
                        }
                    }
                }

                if (numLockIsDisabled == false)
                {
                    string warning = "If you remap Pause, the Num Lock key will be disabled" +
                                     (numLockIsMapped
                                          ? ((char)13 + "and your existing Num Lock mapping will be removed.")
                                          : ".");

                    const string question = "Do you still want to remap Pause?";

                    if (operatingSystemCapability.ImplementsTaskDialog)
                    {
                        TaskDialogResult dr = FormsManager.ShowTaskDialog(question, warning, "Key Mapper",
                                                                          TaskDialogButtons.Yes | TaskDialogButtons.No,
                                                                          TaskDialogIcon.Question);
                        if (dr != TaskDialogResult.Yes)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        DialogResult dr = MessageBox.Show(warning + (char)13 + (char)13 + question, "Key Mapper",
                                                          MessageBoxButtons.YesNo,
                                                          MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0);

                        if (dr != DialogResult.Yes)
                        {
                            return(false);
                        }
                    }
                    disableNumLock = true;
                }
            }

            if (noStackNoEventRaised == false)
            {
                PushMappingsOntoUndoStack();
            }

            // Check for any existing mappings for this key
            // if they exist, this mapping needs to replace them.

            if (Filter == MappingFilter.Boot)
            {
                map.SetType(MappingType.Boot);

                KeyMapping existingMap = GetKeyMapping(map.From.Scancode, map.From.Extended);
                if (existingMap.IsEmpty() == false && existingMap.MapType == MappingType.Boot)
                {
                    _bootMappings.Remove(existingMap);
                }

                _bootMappings.Add(map);
            }
            else
            {
                map.SetType(MappingType.User);

                KeyMapping existingMap = GetKeyMapping(map.From.Scancode, map.From.Extended);
                if (existingMap.IsEmpty() == false && existingMap.MapType == MappingType.User)
                {
                    _userMappings.Remove(existingMap);
                }

                _userMappings.Add(map);
            }

            if (disableNumLock)
            {
                var nl = new KeyMapping(new Key(69, 0), new Key(0, 0));
                AddMapping(nl, true);
            }

            if (noStackNoEventRaised == false)
            {
                RaiseMappingsChangedEvent();
            }

            return(true);
        }