private void cmdSendKeyboardCommand_Click(object sender, RoutedEventArgs e) { string keysToSend = txtKeysToSend.Text; if (!string.IsNullOrEmpty(keysToSend)) { // Send keys 1 by 1. //foreach (char c in keysToSend) //{ // UserKeyboardCommand keyboardCommand = new UserKeyboardCommand(c.ToString()); // MemoryStream ms = new MemoryStream(); // formatter = new BinaryFormatter(); // formatter.Serialize(ms, keyboardCommand); // byte[] keyboardCommandBytes = ms.GetBuffer(); // ms.Close(); // clientSocket.Send(keyboardCommandBytes); //} //Send all keys together. UserKeyboardCommand keyboardCommand = new UserKeyboardCommand(keysToSend); MemoryStream ms = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, keyboardCommand); byte[] mouseCommandBytes = ms.GetBuffer(); ms.Close(); clientSocket.Send(mouseCommandBytes); FocusManager.SetFocusedElement(this, txtKeysToSend); } }
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { // Get the key pressed. Key key = e.Key; // Write the key in the debug window. Debug.WriteLine(key + " (" + e.SystemKey + ")"); // Initialize the keyborad command as null. UserKeyboardCommand keyboardCommand = null; switch (key) { // Send lower case of alphabetic characters. case Key.A: case Key.B: case Key.C: case Key.D: case Key.E: case Key.F: case Key.G: case Key.H: case Key.I: case Key.J: case Key.K: case Key.L: case Key.M: case Key.N: case Key.O: case Key.P: case Key.Q: case Key.R: case Key.S: case Key.T: case Key.U: case Key.V: case Key.W: case Key.X: case Key.Y: case Key.Z: keyboardCommand = new UserKeyboardCommand(e.Key.ToString().ToLower()); break; // These commands can be represents as {[Command]}. case Key.Left: case Key.Right: case Key.Up: case Key.Down: case Key.Add: case Key.Subtract: case Key.Multiply: case Key.Divide: case Key.F1: case Key.F2: case Key.F3: case Key.F4: case Key.F5: case Key.F6: case Key.F7: case Key.F8: case Key.F9: case Key.F10: case Key.F11: case Key.F12: case Key.F13: case Key.F14: case Key.F15: case Key.F16: case Key.Tab: case Key.Delete: case Key.Home: case Key.NumLock: keyboardCommand = new UserKeyboardCommand("{" + e.Key.ToString().ToUpper() + "}"); break; // Digits on the main area. case Key.D0: case Key.D1: case Key.D2: case Key.D3: case Key.D4: case Key.D5: case Key.D6: case Key.D7: case Key.D8: case Key.D9: keyboardCommand = new UserKeyboardCommand(e.Key.ToString()[1].ToString()); break; // Digits on the num pad. case Key.NumPad0: case Key.NumPad1: case Key.NumPad2: case Key.NumPad3: case Key.NumPad4: case Key.NumPad5: case Key.NumPad6: case Key.NumPad7: case Key.NumPad8: case Key.NumPad9: keyboardCommand = new UserKeyboardCommand(e.Key.ToString()[6].ToString()); break; // Special commands which needs special string command. case Key.Space: keyboardCommand = new UserKeyboardCommand(" "); break; case Key.Enter: keyboardCommand = new UserKeyboardCommand("{ENTER}"); break; case Key.Back: keyboardCommand = new UserKeyboardCommand("{BACKSPACE}"); break; case Key.Escape: keyboardCommand = new UserKeyboardCommand("{ESC}"); break; case Key.LeftCtrl: case Key.RightCtrl: keyboardCommand = new UserKeyboardCommand("^"); break; case Key.LeftShift: case Key.RightShift: keyboardCommand = new UserKeyboardCommand("+"); break; case Key.LeftAlt: case Key.RightAlt: keyboardCommand = new UserKeyboardCommand("%"); break; case Key.Oem3: keyboardCommand = new UserKeyboardCommand("`"); break; case Key.OemMinus: keyboardCommand = new UserKeyboardCommand("-"); break; case Key.OemPlus: keyboardCommand = new UserKeyboardCommand("="); break; case Key.OemOpenBrackets: keyboardCommand = new UserKeyboardCommand("["); break; case Key.Oem6: keyboardCommand = new UserKeyboardCommand("]"); break; case Key.Oem5: keyboardCommand = new UserKeyboardCommand(@"\"); break; case Key.Oem1: keyboardCommand = new UserKeyboardCommand(";"); break; case Key.OemQuotes: keyboardCommand = new UserKeyboardCommand("'"); break; case Key.OemComma: keyboardCommand = new UserKeyboardCommand(","); break; case Key.OemPeriod: keyboardCommand = new UserKeyboardCommand("."); break; case Key.OemQuestion: keyboardCommand = new UserKeyboardCommand("/"); break; case Key.Decimal: keyboardCommand = new UserKeyboardCommand("."); break; } // Get the modifier keys the user pressed. ModifierKeys modifier = e.KeyboardDevice.Modifiers; // Configurations for conbination keys. if (modifier == (ModifierKeys.Control | ModifierKeys.Shift)) { keyboardCommand = new UserKeyboardCommand("^+"); } else if (modifier == (ModifierKeys.Control | ModifierKeys.Alt)) { keyboardCommand = new UserKeyboardCommand("^%"); } else if (modifier == (ModifierKeys.Alt | ModifierKeys.Shift)) { keyboardCommand = new UserKeyboardCommand("%^"); } else if ((keyboardCommand != null) && (modifier == ModifierKeys.Shift) && (key != Key.LeftShift) && (key != Key.RightShift)) { keyboardCommand = new UserKeyboardCommand("+" + keyboardCommand.Key); } else if ((keyboardCommand != null) && (modifier == ModifierKeys.Control) && (key != Key.LeftCtrl) && (key != Key.RightCtrl)) { keyboardCommand = new UserKeyboardCommand("^" + keyboardCommand.Key); } else if ((keyboardCommand != null) && (modifier == ModifierKeys.Alt) && (key != Key.LeftAlt) && (key != Key.RightAlt)) { keyboardCommand = new UserKeyboardCommand("%" + keyboardCommand.Key); } // Send the keyboard command if it is null. if (keyboardCommand != null) { // Serialize the command. MemoryStream ms = new MemoryStream(); formatter.Serialize(ms, keyboardCommand); byte[] keyboardCommandBytes = ms.GetBuffer(); ms.Close(); // Send the command. clientSocket.Send(keyboardCommandBytes); } }