/// <summary>
 /// Type the given string into whatever the target TextBox (or similar control) is.
 /// </summary>
 /// <param name="sWhat"></param>
 public void Inject(string sWhat)
 {
     if (TargetWindow != null)
     {
         ((Window)TargetWindow).Focus();
         TextBox txtTarget = TargetWindow.ControlToInjectInto as TextBox;
         if (txtTarget != null)
         {
             if (txtTarget.IsEnabled && !txtTarget.IsReadOnly)
             {
                 if (IsFlowDirectionLeftToRight ||
                     (!IsFlowDirectionLeftToRight && !IsForcingLeftToRight))
                 {
                     txtTarget.InsertText(sWhat);
                 }
                 else  // the FlowDirection is backwards, but the user wants to force it to insert LeftToRight.
                 {
                     txtTarget.InsertTextLeftToRight(sWhat);
                 }
                 MakeKeyPressSound();
             }
             else
             {
                 if (!txtTarget.IsEnabled)
                 {
                     SetStatusTextTo("Cannot type into disabled field");
                 }
                 else
                 {
                     SetStatusTextTo("Cannot type into readonly field");
                 }
             }
         }
         else
         {
             RichTextBox richTextBox = TargetWindow.ControlToInjectInto as RichTextBox;
             if (richTextBox != null)
             {
                 if (richTextBox.IsEnabled && !richTextBox.IsReadOnly)
                 {
                     richTextBox.InsertText(sWhat);
                     MakeKeyPressSound();
                 }
                 else
                 {
                     if (!txtTarget.IsEnabled)
                     {
                         SetStatusTextTo("Cannot type into disabled field");
                     }
                     else
                     {
                         SetStatusTextTo("Cannot type into readonly field");
                     }
                 }
             }
             else // let's hope it's an IInjectableControl
             {
                 IInjectableControl targetControl = TargetWindow.ControlToInjectInto
                                                    as IInjectableControl;
                 if (targetControl != null)
                 {
                     targetControl.InsertText(sWhat);
                     MakeKeyPressSound();
                 }
             }
             //else   // if you have other text-entry controls such as a rich-text box, include them here.
             //{
             //    FsWpfControls.FsRichTextBox.FsRichTextBox txtrTarget
             //     = TargetWindow.ControlToInjectInto as FsWpfControls.FsRichTextBox.FsRichTextBox;
             //    if (txtrTarget != null)
             //    {
             //        txtrTarget.InsertThis(sWhat);
             //    }
         }
     }
 }
        public void ExecuteBackspaceCommand()
        {
            Debug.WriteLine("KeyboardViewModel.ExecuteBackspaceCommand");

            if (TargetWindow != null)
            {
#if !SILVERLIGHT
                ((Window)TargetWindow).Focus();
#endif
                TextBox txtTarget = TargetWindow.ControlToInjectInto as TextBox;
                if (txtTarget != null)
                {
                    // Use the built-in Backspace command. Create it only once, and remove it when this VirtualKeyboard window is closing.
                    //TODO
#if !SILVERLIGHT
                    if (_backspaceCommandBinding == null)
                    {
                        _backspaceCommandBinding = new CommandBinding(EditingCommands.Backspace);
                        txtTarget.CommandBindings.Add(_backspaceCommandBinding);
                    }
                    _backspaceCommandBinding.Command.Execute(null);
#endif
                    MakeBackspaceSound();

                    // Perhaps I should return to the following code, if it will work in SL. ?

                    // This was how I did the Backspace operation without using the command..
                    //int iCaretIndex = txtTarget.CaretIndex;
                    //if (iCaretIndex > 0)
                    //{
                    //    string sOriginalContent = txtTarget.Text;
                    //    int nLength = sOriginalContent.Length;
                    //    if (iCaretIndex >= nLength)
                    //    {
                    //        txtTarget.Text = sOriginalContent.Substring(0, nLength - 1);
                    //    }
                    //    else
                    //    {
                    //        string sPartBeforeCaret = sOriginalContent.Substring(0, iCaretIndex - 1);
                    //        string sPartAfterCaret = sOriginalContent.Substring(iCaretIndex, nLength - iCaretIndex);
                    //        txtTarget.Text = sPartBeforeCaret + sPartAfterCaret;
                    //    }
                    //    txtTarget.CaretIndex = iCaretIndex - 1;
                    //}
                }
                else // let's hope it's an IInjectableControl
                {
                    IInjectableControl targetControl = TargetWindow.ControlToInjectInto as IInjectableControl;
                    if (targetControl != null)
                    {
                        targetControl.Backspace();
                        MakeBackspaceSound();
                    }
                    //else
                    //{
                    //    FsWpfControls.FsRichTextBox.FsRichTextBox txtrTarget = TargetWindow.ControlToInjectInto as FsWpfControls.FsRichTextBox.FsRichTextBox;
                    //    if (txtrTarget != null)
                    //    {
                    //        txtrTarget.InsertThis(chWhat.ToString());
                    //    }
                }
            }
            //textbox.Text = sOriginalContent.Insert(iCaretIndex, sStuffToInsert);
        }