Exemplo n.º 1
0
        public static bool IsMappingPending(KeyMapping map, MappingFilter filter)
        {
            if (_unsavedMappings.Contains(map))
            {
                return(false);
            }

            // Did this mapping exist at boot or logon time?
            switch (filter)
            {
            case MappingFilter.ClearedUser:
            case MappingFilter.ClearedBoot:
                return(true);    // A cleared mapping is by definition pending

            case MappingFilter.All:
                return(!(_savedBootMappings.Contains(map) | _savedUserMappings.Contains(map)));

            case MappingFilter.Boot:
                return(!(_savedBootMappings.Contains(map)));

            case MappingFilter.User:
                return(!(_savedUserMappings.Contains(map)));
            }

            return(true);
        }
Exemplo n.º 2
0
	    public KeyPictureBox(int scancode, int extended, BlankButton button, float scale, int horizontalStretch, int verticalStretch)
		{
			_scancode = scancode;
			_extended = extended;
			_button = button;
			_scale = scale;
			_horizontalStretch = horizontalStretch;
			_verticalStretch = verticalStretch;
			_dragIconScale = 0.75F;
			_dragbox = Rectangle.Empty;
			
			Map = MappingsManager.GetKeyMapping(_scancode, _extended);

			_mapped = (Map.To.Scancode != -1);

			this.AllowDrop = true;

			// Box controls itself.
			this.DragOver += KeyPictureBoxDragOver;
			this.DragDrop += KeyPictureBoxDragDrop;
			this.DragLeave += KeyPictureBoxDragLeave;
			this.GiveFeedback += KeyPictureBoxGiveFeedback;
			this.MouseDown += KeyPictureBoxMouseDown;
			this.MouseMove += KeyPictureBoxMouseMove;
			this.MouseUp += KeyPictureBoxMouseUp;
			this.QueryContinueDrag += KeyPictureBoxQueryContinueDrag;

			DrawKey();
			this.Width = this.Image.Width;
			this.Height = this.Image.Height;
        }
Exemplo n.º 3
0
        private static Collection <KeyMapping> GetMappingsFromScancodeMap(byte[] map, MappingType type)
        {
            // Transform the byte array into keymappings

            var maps = new Collection <KeyMapping>();

            int count  = 0;
            int length = map.GetLength(0);

            // How many mappings are there?
            // (Make sure there are at least 8 bytes in the array)

            if (length > 8)
            {
                count = map[8] - 1;
            }

            if (count == 0)
            {
                return(maps);
            }

            const int start = 12;

            for (int i = 0; i < count; i++)
            {
                // Make sure we don't extend beyond array bounds
                if (length >= (start + (i * 4) + 3))
                {
                    // First pair is the action - what the mapped key does.
                    int word1 = map[start + (i * 4)];
                    int word2 = map[start + (i * 4) + 1];

                    var tokey = new Key(word1, word2);

                    // Second pair is the physical key which performs the new action
                    word1 = map[start + (i * 4) + 2];
                    word2 = map[start + (i * 4) + 3];

                    var fromkey = new Key(word1, word2);

                    var mapping = new KeyMapping(fromkey, tokey);

                    if (mapping.IsValid())
                    {
                        mapping.SetType(type);
                        maps.Add(mapping);
                    }
                    else
                    // Just ignore it and hope it goes away.
                    // A manually added - or garbled - entry could be invalid.
                    {
                    }
                }
            }

            return(maps);
        }
Exemplo n.º 4
0
        public static void DeleteMapping(KeyMapping map)
        {
            if (!map.IsValid())
                throw new ArgumentException("Can't delete an invalid map");
            PushMappingsOntoUndoStack();

            if (mappings.Contains(map))
            {
                mappings.Remove(map);
            }

            RaiseMappingsChangedEvent();
        }
Exemplo n.º 5
0
        public static void DeleteMapping(KeyMapping map)
        {
            if (!map.IsValid())
            {
                throw new ArgumentException("Can't delete an invalid map");
            }
            PushMappingsOntoUndoStack();

            if (mappings.Contains(map))
            {
                mappings.Remove(map);
            }

            RaiseMappingsChangedEvent();
        }
Exemplo n.º 6
0
        public static void DeleteMapping(KeyMapping map, MappingFilter filter)
        {
            if (!map.IsValid())
            {
                throw new ArgumentException("Can't delete an invalid map");
            }

            PushMappingsOntoUndoStack();

            switch (filter)
            {
            case MappingFilter.All:
                // Could be mapped in both HKCU and HKLM so
                // remove the HKCU mapping first, but not both..

                if (_userMappings.Contains(map))
                {
                    _userMappings.Remove(map);
                }
                else if (_bootMappings.Contains(map))
                {
                    _bootMappings.Remove(map);
                }
                break;

            case MappingFilter.Boot:
                if (_bootMappings.Contains(map))
                {
                    _bootMappings.Remove(map);
                }
                break;

            case MappingFilter.User:
                if (_userMappings.Contains(map))
                {
                    _userMappings.Remove(map);
                }
                break;

            default:
                break;
            }

            RaiseMappingsChangedEvent();
        }
Exemplo n.º 7
0
        public static void ShowEditMappingForm(KeyMapping km, bool useCapture)
        {
            AddEditMapping mf = new AddEditMapping(km, useCapture);

            Properties.Settings userSettings = new Properties.Settings();

            Point savedLocation = userSettings.EditMappingFormLocation;

            if (savedLocation.IsEmpty == false)
            {
                mf.Location = savedLocation;
            }
            else
            {
                PositionChildForm(mf, ChildFormPosition.MiddleLeft);
            }

            mf.ShowDialog(_mainForm);
        }
Exemplo n.º 8
0
        public static bool IsMapped(KeyMapping map, MappingFilter filter)
        {
            Collection <KeyMapping> maps;

            switch (filter)
            {
            case MappingFilter.All:
                maps = _allMappings;
                break;

            case MappingFilter.Boot:
                maps = _bootMappings;
                break;

            case MappingFilter.Current:
                maps = currentFilteredMappings;
                break;

            case MappingFilter.User:
                maps = _userMappings;
                break;

            case MappingFilter.ClearedUser:
                maps = clearedUserMappings;
                break;

            case MappingFilter.ClearedBoot:
                maps = clearedBootMappings;
                break;

            default:
                maps = new Collection <KeyMapping>();
                break;
            }

            return(maps.Contains(map));
        }
Exemplo n.º 9
0
        private static byte[] GetMappingsAsByteArray(Collection <KeyMapping> maps)
        {
            // Turn mappings into a byte[]
            int count = maps.Count;
            int size  = (16 + (count * 4));

            // Check they are all zero.

            var bytemappings = new byte[size];

            // Allow for the null mapping at the end
            bytemappings[8] = (byte)(count + 1);

            const int start = 12;

            for (int i = 0; i < count; i++)
            {
                // Make sure we don't extend beyond array bounds
                if (size <= (start + (i * 4) + 3))
                {
                    break;
                }

                KeyMapping map = maps[i];

                // First pair is the action - what the mapped key does.
                bytemappings[start + (i * 4)]     = (byte)map.To.Scancode;
                bytemappings[start + (i * 4) + 1] = (byte)map.To.Extended;

                // Second pair is the physical key which performs the new action
                bytemappings[start + (i * 4) + 2] = (byte)map.From.Scancode;
                bytemappings[start + (i * 4) + 3] = (byte)map.From.Extended;
            }

            return(bytemappings);
        }
Exemplo n.º 10
0
 public static bool IsDisabledMapping(KeyMapping map)
 {
     return(map.To.Scancode == 0 && map.To.Extended == 0);
 }
Exemplo n.º 11
0
 public static bool IsEmptyMapping(KeyMapping map)
 {
     return(map.To.Scancode == -1 && map.To.Extended == -1);
 }
Exemplo n.º 12
0
 public static bool IsMapped(KeyMapping map)
 {
     return(mappings.Contains(map));
 }
Exemplo n.º 13
0
 public static void DeleteMapping(KeyMapping map)
 {
     DeleteMapping(map, Filter);
 }
Exemplo n.º 14
0
 public static bool IsMapped(KeyMapping map)
 {
     return mappings.Contains(map);
 }
Exemplo n.º 15
0
 public static bool IsDisabledMapping(KeyMapping map)
 {
     return (map.To.Scancode == 0 && map.To.Extended == 0);
 }
Exemplo n.º 16
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?";


                TaskDialogResult dr = FormsManager.ShowTaskDialog(question, warning, "Key Mapper",
                                                                  TaskDialogButtons.Yes | TaskDialogButtons.No,
                                                                  TaskDialogIcon.Question);
                if (dr != TaskDialogResult.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 mappings)
                {
                    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?";


                    TaskDialogResult dr = FormsManager.ShowTaskDialog(question, warning, "Key Mapper",
                                                                      TaskDialogButtons.Yes | TaskDialogButtons.No,
                                                                      TaskDialogIcon.Question);
                    if (dr != TaskDialogResult.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.

            KeyMapping existingMap = GetKeyMapping(map.From.Scancode, map.From.Extended);

            if (existingMap.IsEmpty() == false)
            {
                mappings.Remove(existingMap);
            }

            mappings.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);
        }
Exemplo n.º 17
0
        private void MapSelected()
        {
            if (disabled)
                // Nono - Can't map while disabled. Shouldn't be here anyway!
                return;

            if (mapped)
            {
                // Unmap.
                MappingsManager.DeleteMapping(map);
                SetMapToBlankMapping();
                Close();
                return;
            }

            if (capturingToKey)
            {
                // Ah, but have we caught a "to" key yet?
                if (!map.IsValid())
                    return;

                capturingToKey = false;
                StopCapture();
                MappingsManager.AddMapping(map);
                Close();
                return;
            }

            if (selectingFromKeyFromLists)
            {

                Key selectedKey = GetKeyFromListboxValue();

                // Have we been sent a dud??
                if (selectedKey.Scancode == 0)
                {
                    // Something went wrong.
                    map = new KeyMapping();
                }
                else
                {
                    SetMapToBlankMapping(selectedKey.Scancode, selectedKey.Extended);
                    // Need to move panel back to where it was and set the image in the picturebox
                    KeyListsPanel.Location = savedPanelLocation;

                    FromKeyPictureBox.SetImage(ButtonImages.GetButtonImage(map.From.Scancode, map.From.Extended));
                    selectingFromKeyFromLists = false;
                    keyThreshold = 1;
                    SetListOptionsComboIndex();
                    SetupForm();
                    return;

                }

            }

            if (capturingFromKey == false)
            {
                // Not mapped, not capturing From or To keys, so this is mapping from list.
                // Need to call method to create map from name.
                if (CreateMappingFromListboxValue())
                {
                    MappingsManager.AddMapping(map);
                    Close();
                }
                return;
            }
            else
            {
                // Setting the From key. Map has already been created from keypress
                capturingFromKey = false;
                StopCapture();
                direction = FadeDirection.FromBlankToUnmapped;
                SetupForm();
                Transition();
            }
        }
Exemplo n.º 18
0
        private void DisableButtonClick(object sender, EventArgs e)
        {
            // Disable or enable, close form anyway.
            if (disabled)
            {
                // Enable
                MappingsManager.DeleteMapping(map);
            }
            else
            {
                // Disable
                map = new KeyMapping(map.From, new Key(0, 0));
                MappingsManager.AddMapping(map);
            }

            Close();
        }
Exemplo n.º 19
0
 private bool CreateMappingFromListboxValue()
 {
     map = new KeyMapping(map.From, GetKeyFromListboxValue());
     return true;
 }
Exemplo n.º 20
0
 public static bool IsMappingPending(KeyMapping map)
 {
     return IsMappingPending(map, Filter);
 }
Exemplo n.º 21
0
        public static void ShowEditMappingForm(KeyMapping km, bool useCapture)
        {
            AddEditMapping mf = new AddEditMapping(km, useCapture);

            Properties.Settings userSettings = new Properties.Settings();

            Point savedLocation = userSettings.EditMappingFormLocation;

            if (savedLocation.IsEmpty == false)
                mf.Location = savedLocation;
            else
                PositionChildForm(mf, ChildFormPosition.MiddleLeft);

            mf.ShowDialog(_mainForm);
        }
Exemplo n.º 22
0
 public static bool IsMappingPending(KeyMapping map)
 {
     return(IsMappingPending(map, Filter));
 }
Exemplo n.º 23
0
        private void OnKeyPress(object sender, KeyMapperKeyPressedEventArgs e)
        {
            int scancode = e.Key.Scancode;
            int extended = e.Key.Extended;

              			if (capturingFromKey)
            {
                // Have we been sent a dud??
                if (scancode == 0)
                {
                    // Can't use a disabled key as From
                    map = new KeyMapping();
                }
                else
                {
                    SetMapToBlankMapping(scancode, extended);
                }
            }
            else
            {
                // Can't tell from the passed key whether it's mapped or not as
                // if it is, we get the mapped scancode and have no way of telling
                // what the original key was (it's possible 2 keys could be mapped to the
                // same key, meaning can't just do a reverse lookup.)

                // So, mapping to a mapped key is de facto allowed.

                map = new KeyMapping(map.From, new Key(scancode, extended));
            }

            SetupForm();
        }
Exemplo n.º 24
0
 public static bool AddMapping(KeyMapping map)
 {
     return(AddMapping(map, false));
 }
Exemplo n.º 25
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?";

                TaskDialogResult dr = FormsManager.ShowTaskDialog(question, warning, "Key Mapper",
                                                                  TaskDialogButtons.Yes | TaskDialogButtons.No,
                                                                  TaskDialogIcon.Question);
                if (dr != TaskDialogResult.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 mappings)
                    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?";

                    TaskDialogResult dr = FormsManager.ShowTaskDialog(question, warning, "Key Mapper",
                                                                      TaskDialogButtons.Yes | TaskDialogButtons.No,
                                                                      TaskDialogIcon.Question);
                    if (dr != TaskDialogResult.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.

            KeyMapping existingMap = GetKeyMapping(map.From.Scancode, map.From.Extended);

            if (existingMap.IsEmpty() == false)
                mappings.Remove(existingMap);

            mappings.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;
        }
Exemplo n.º 26
0
        public static bool IsMapped(KeyMapping map, MappingFilter filter)
        {
            Collection<KeyMapping> maps;

            switch (filter)
            {
                case MappingFilter.All:
                    maps = _allMappings;
                    break;
                case MappingFilter.Boot:
                    maps = _bootMappings;
                    break;
                case MappingFilter.Current:
                    maps = currentFilteredMappings;
                    break;
                case MappingFilter.User:
                    maps = _userMappings;
                    break;
                case MappingFilter.ClearedUser:
                    maps = clearedUserMappings;
                    break;
                case MappingFilter.ClearedBoot:
                    maps = clearedBootMappings;
                    break;
                default:
                    maps = new Collection<KeyMapping>();
                    break;
            }

            return maps.Contains(map);
        }
Exemplo n.º 27
0
        private static Collection<KeyMapping> GetMappingsFromScancodeMap(byte[] map)
        {
            // Transform the byte array into keymappings
            var maps = new Collection<KeyMapping>();

            int count = 0;
            int length = map.GetLength(0);

            // How many mappings are there?
            // (Make sure there are at least 8 bytes in the array)

            if (length > 8)
                count = map[8] - 1;

            if (count == 0)
            {
                return maps;
            }

            const int start = 12;

            for (int i = 0; i < count; i++)
            {
                // Make sure we don't extend beyond array bounds
                if (length >= (start + (i * 4) + 3))
                {
                    // First pair is the action - what the mapped key does.
                    int word1 = map[start + (i * 4)];
                    int word2 = map[start + (i * 4) + 1];

                    var tokey = new Key(word1, word2);

                    // Second pair is the physical key which performs the new action
                    word1 = map[start + (i * 4) + 2];
                    word2 = map[start + (i * 4) + 3];

                    var fromkey = new Key(word1, word2);

                    var mapping = new KeyMapping(fromkey, tokey);

                    if (mapping.IsValid())
                    {
                        maps.Add(mapping);
                    }
                    else
                    // Just ignore it and hope it goes away.
                    // A manually added - or garbled - entry could be invalid.
                    {
                    }
                }
            }

            return maps;
        }
Exemplo n.º 28
0
 public static bool IsEmptyMapping(KeyMapping map)
 {
     return (map.To.Scancode == -1 && map.To.Extended == -1);
 }
Exemplo n.º 29
0
 public static void DeleteMapping(KeyMapping map)
 {
     DeleteMapping(map, Filter);
 }
Exemplo n.º 30
0
        public static bool IsMappingPending(KeyMapping map, MappingFilter filter = MappingFilter.Boot)
        {
            if (unsavedMappings.Contains(map))
                return false;

            // Did this mapping exist at boot or logon time?
            switch (filter)
            {
                case MappingFilter.ClearedBoot:
                    return true; // A cleared mapping is by definition pending
                case MappingFilter.Boot:
                    return !(savedBootMappings.Contains(map));
            }

            return true;
        }
Exemplo n.º 31
0
        public static void DeleteMapping(KeyMapping map, MappingFilter filter)
        {
            if (!map.IsValid())
                throw new ArgumentException("Can't delete an invalid map");

            PushMappingsOntoUndoStack();

            switch (filter)
            {
                case MappingFilter.All:
                    // Could be mapped in both HKCU and HKLM so 
                    // remove the HKCU mapping first, but not both..

                    if (_userMappings.Contains(map))
                    {
                        _userMappings.Remove(map);
                    }
                    else if (_bootMappings.Contains(map))
                    {
                        _bootMappings.Remove(map);
                    }
                    break;

                case MappingFilter.Boot:
                    if (_bootMappings.Contains(map))
                    {
                        _bootMappings.Remove(map);
                    }
                    break;

                case MappingFilter.User:
                    if (_userMappings.Contains(map))
                    {
                        _userMappings.Remove(map);
                    }
                    break;
                default:
                    break;
            }

            RaiseMappingsChangedEvent();
        }
Exemplo n.º 32
0
 public static bool AddMapping(KeyMapping map)
 {
     return AddMapping(map, false);
 }
Exemplo n.º 33
0
 private void SetMapToBlankMapping()
 {
     map = MappingsManager.GetEmptyMapping(map.From);
 }
Exemplo n.º 34
0
        public AddEditMapping(KeyMapping map, bool useCapture)
        {
            InitializeComponent();

            // There are four startup states for this form.

            // 1) Choose a From key by capturing it
            // 2) Choose a mapping for a specific From key
            // 3) Display a given mapping From and To. Includes disabled.
            // 4) Select a From key from the lists

            // Map is a struct so it can't be null.
            if (map.IsEmpty())
            {
                if (useCapture)
                {
                    // We are capturing the 'from' key.
                    capturingFromKey = true;
                }
                else
                {
                    selectingFromKeyFromLists = true;
                }
            }
            else
            {
                mapped = (map.To.Scancode > 0);
                disabled = (map.To.Scancode == 0);
            }

            newMapping = !map.IsValid();

            this.map = map;

            // Default has the KeyLists panel in the frame.
            if (mapped | disabled)
            {
                SwopPanelPositions(KeyListsPanel, MappingPanel);
            }
            else if (capturingFromKey)
            {
                SwopPanelPositions(EmptyPanel, KeyListsPanel);
            }

            if (selectingFromKeyFromLists)
            {
                // Need to move the lists to the left where the box while remembering where it was
                savedPanelLocation = KeyListsPanel.Location;
                KeyListsPanel.Left = FromKeyPictureBox.Left;
                keyThreshold = -1; // Show all keys as possible map-ees
            }
            else
                keyThreshold = 1;

            SetListOptionsComboIndex();

            PopulateKeyLists();

            // Add event handlers now values have been assigned
            GroupsListbox.SelectedIndexChanged += GroupsListboxSelectedIndexChanged;
            KeysByGroupListbox.SelectedIndexChanged += KeysByGroupListboxSelectedIndexChanged;
            ListOptionsCombo.SelectedIndexChanged += ListOptionsComboSelectedIndexChanged;
            KeysByGroupListbox.DoubleClick += KeysByGroupListboxDoubleClick;

            SetupForm();
        }
Exemplo n.º 35
0
 private void SetMapToBlankMapping(int scancode, int extended)
 {
     map = MappingsManager.GetEmptyMapping(new Key(scancode, extended));
 }