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); }
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; }
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; }
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); }