/// <summary> /// Checks for equiality of windows keys /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { if (obj is KeyMappingEntry) { KeyMappingEntry entry = (KeyMappingEntry)obj; return((entry.WindowsKey == WindowsKey) && (entry.WindowsModifiers == WindowsModifiers)); } else { return(false); } }
/// <summary> /// Loads key mappting table from text file from the specified stream /// </summary> /// <param name="in_stream">Stream conatining the key mapping text file</param> public void LoadKeyMappingTable(Stream in_stream) { KeyMappingCollection key_mapping = new KeyMappingCollection(); using (StreamReader reader = new StreamReader(in_stream)) { while (!reader.EndOfStream) { int pos; string line = reader.ReadLine(); // remove spaces line = line.Replace(" ", ""); line = line.Replace("\t", ""); // remove comment pos = line.IndexOf(';'); if (pos >= 0) { line = line.Substring(0, pos); } // create keyboard entry string[] fields = line.Split('|'); if (fields.Length == 5) { Key windows_key = (Key)Enum.Parse(typeof(Key), fields[0], true); ModifierKeys modifier_keys = (ModifierKeys)Enum.Parse(typeof(ModifierKeys), fields[1], true); int row = int.Parse(fields[2]); int col = int.Parse(fields[3]); KeyModifiers key_modifiers = (KeyModifiers)Enum.Parse(typeof(KeyModifiers), fields[4], true); key_mapping.Add(new KeyMappingEntry(windows_key, modifier_keys, row, col, key_modifiers)); // Add Shift + key if (key_modifiers.HasFlag(KeyModifiers.KeepShift)) { key_mapping.Add(new KeyMappingEntry(windows_key, modifier_keys | ModifierKeys.Shift, row, col, key_modifiers)); } // Add Ctrl + key if (key_modifiers.HasFlag(KeyModifiers.KeepCtrl)) { key_mapping.Add(new KeyMappingEntry(windows_key, modifier_keys | ModifierKeys.Control, row, col, key_modifiers)); } // Add Alt + key if (key_modifiers.HasFlag(KeyModifiers.KeepAlt)) { key_mapping.Add(new KeyMappingEntry(windows_key, modifier_keys | ModifierKeys.Alt, row, col, key_modifiers)); } } } } // cache control keys m_shift_key = null; m_ctrl_key = null; m_alt_key = null; KeyMappingEntry shift_key = new KeyMappingEntry(Key.LeftShift, ModifierKeys.Shift); if (key_mapping.Contains(shift_key)) { m_shift_key = key_mapping[shift_key.GetHashCode()]; } KeyMappingEntry ctrl_key = new KeyMappingEntry(Key.LeftCtrl, ModifierKeys.Control); if (key_mapping.Contains(ctrl_key)) { m_ctrl_key = key_mapping[ctrl_key.GetHashCode()]; } KeyMappingEntry alt_key = new KeyMappingEntry(Key.LeftAlt, ModifierKeys.Alt); if (key_mapping.Contains(alt_key)) { m_ctrl_key = key_mapping[alt_key.GetHashCode()]; } m_key_mapping = key_mapping; }
/// <summary> /// Updates injected key, advaces to the nxt character with the desired rate /// </summary> private void UpdateInjectedKeyboardMatrix() { // do nothing if injection is not active if (m_keyboard_injection_pos < 0) { return; } // handle injection character rate if (m_tvc.GetTicksSince(m_keyboard_injection_timestamp) < m_keyboard_injection_rate) { return; } // update ticks m_keyboard_injection_timestamp = m_tvc.GetCPUTicks(); // check for end of the string if (m_keyboard_injection_pos >= m_keyboard_injection_string.Length) { // reset keyboard matrix for (int i = 0; i < m_keyboard_matrix.Length; i++) { m_keyboard_matrix[i] = 0xff; } // exit injection mode m_keyboard_injection_pos = -1; } else { bool wait = false; while (m_keyboard_injection_pos >= 0 && m_keyboard_injection_pos < m_keyboard_injection_string.Length && !wait) { // get the next command and character to inject int char_pos = m_keyboard_injection_string.IndexOf(',', m_keyboard_injection_pos); char command; string key; if (char_pos >= 0) { command = m_keyboard_injection_string[m_keyboard_injection_pos]; key = m_keyboard_injection_string.Substring(m_keyboard_injection_pos + 1, char_pos - m_keyboard_injection_pos - 1); m_keyboard_injection_pos = char_pos + 1; } else { command = m_keyboard_injection_string[m_keyboard_injection_pos]; key = m_keyboard_injection_string.Substring(m_keyboard_injection_pos + 1); m_keyboard_injection_pos = m_keyboard_injection_string.Length; } // find keyboard entry for the command's key KeyMappingEntry key_map_entry = null; if (!string.IsNullOrEmpty(key)) { Key key_code = (Key)Enum.Parse(typeof(Key), key, true); KeyMappingEntry windows_key = new KeyMappingEntry(key_code, ModifierKeys.None, 0, 0, KeyModifiers.None); if (m_key_mapping.Contains(windows_key)) { key_map_entry = m_key_mapping[windows_key.GetHashCode()]; } } // determine command switch (char.ToUpper(command)) { // down case 'D': if (key_map_entry != null) { m_keyboard_matrix[key_map_entry.Row] &= (byte)(~(1 << key_map_entry.Column)); } break; // up case 'U': if (key_map_entry != null) { m_keyboard_matrix[key_map_entry.Row] |= (byte)(1 << key_map_entry.Column); } break; // wait case 'W': wait = true; break; } } } }
/// <summary> /// Update TVC hardware keyboard matrix content /// </summary> private void UpdateKeyboardMatrix() { // do not update when injection is in progress if (m_keyboard_injection_pos >= 0) { return; } //////////////////////// // init keyboard matrix byte[] keyboard_matrix = new byte[KeyboardRowCount]; for (int i = 0; i < keyboard_matrix.Length; i++) { keyboard_matrix[i] = 0xff; } if (m_keyboard_injection_pos >= 0) { Key pressed_key; if (m_tvc.GetTicksSince(m_keyboard_injection_timestamp) > m_keyboard_injection_rate) { m_keyboard_injection_timestamp = m_tvc.GetCPUTicks(); m_keyboard_injection_pos++; if (m_keyboard_injection_pos >= m_keyboard_injection_string.Length) { m_keyboard_injection_pos = -1; } } if (m_keyboard_injection_pos >= 0) { pressed_key = (Key)Enum.Parse(typeof(Key), m_keyboard_injection_string.Substring(m_keyboard_injection_pos, 1), true); KeyMappingEntry windows_key = new KeyMappingEntry(pressed_key, ModifierKeys.None, 0, 0, KeyModifiers.None); if (m_key_mapping.Contains(windows_key)) { KeyMappingEntry entry = m_key_mapping[windows_key.GetHashCode()]; keyboard_matrix[entry.Row] &= (byte)(~(1 << entry.Column)); } } } else { ///////////////////////////////// // Add pressed keys to the matrix KeyModifiers modifiers = KeyModifiers.KeepAll; for (int i = 0; i < PressedKeyCount; i++) { if (m_pressed_keys[i] != Key.None) { KeyMappingEntry windows_key = new KeyMappingEntry(m_pressed_keys[i], Keyboard.Modifiers, 0, 0, KeyModifiers.None); if (m_key_mapping.Contains(windows_key)) { KeyMappingEntry entry = m_key_mapping[windows_key.GetHashCode()]; keyboard_matrix[entry.Row] &= (byte)(~(1 << entry.Column)); modifiers = entry.Modifiers; } } } //////////////////// // Handle modifiers // shift key if (m_shift_key != null) { if ((modifiers & KeyModifiers.AddShift) != 0) { keyboard_matrix[m_shift_key.Row] &= (byte)(~(1 << m_shift_key.Column)); } if ((modifiers & KeyModifiers.RemoveShift) != 0) { keyboard_matrix[m_shift_key.Row] |= (byte)(1 << m_shift_key.Column); } } // Ctrl shift key if (m_ctrl_key != null) { if ((modifiers & KeyModifiers.AddCtrl) != 0) { keyboard_matrix[m_ctrl_key.Row] &= (byte)(~(1 << m_ctrl_key.Column)); } if ((modifiers & KeyModifiers.RemoveCtrl) != 0) { keyboard_matrix[m_ctrl_key.Row] |= (byte)(1 << m_ctrl_key.Column); } } // Alt shift key if (m_alt_key != null) { if ((modifiers & KeyModifiers.AddAlt) != 0) { keyboard_matrix[m_alt_key.Row] &= (byte)(~(1 << m_alt_key.Column)); } if ((modifiers & KeyModifiers.RemoveAlt) != 0) { keyboard_matrix[m_alt_key.Row] |= (byte)(1 << m_alt_key.Column); } } } //////////////////////// // store keyboard matrix for (int i = 0; i < KeyboardRowCount; i++) { m_keyboard_matrix[i] = keyboard_matrix[i]; #if DEBUG_TVC_KEYBOARD Debug.Write(m_keyboard_matrix[i].ToString("x2") + " "); #endif } #if DEBUG_TVC_KEYBOARD Debug.WriteLine(""); #endif }