title: Add "Enter" and "Escape" key presses to Powershell WPF form
body: <p>I used ISE-Sterioids template to create a simple 3 field WPF form that asks for three things:<br>
  - ID<br>
  - Email<br>
  - Reference<br>
  I have it sucessfully working with these fields when using the OK and Cancel buttons, but I would like to capter "Enter" to submit the form and "Escape" to cancel the form, but I am having difficulty adding the events. </p>
  
  <p>I have tried similar code to the technet article here; but as this isn't using WPF I think I am missing something</p>
  
  <pre><code>$objForm.KeyPreview = $True
  $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
  {$x=$objTextBox.Text;$objForm.Close()}})
  </code></pre>
  
  <p>My code here:</p>
  
  <pre><code>#region XAML window definition
  # Right-click XAML and choose WPF/Edit... to edit WPF Design
  # in your favorite WPF editing tool
  
  # Default Form Values
  $123 = 'ID'
  $toEmail = 'email address'
  $ref = "ref"
  
  $xaml = @'
  &lt;Window
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
     MinWidth="200"
     Width ="400"
     SizeToContent="Height"
     Title="Proofing script"
     Topmost="True"&gt;
      &lt;Grid Margin="10,2,10,10"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto"/&gt;
              &lt;ColumnDefinition Width="*"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;!-- &lt;TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="5"&gt;Please enter your details:&lt;/TextBlock&gt; --&gt;
  
          &lt;TextBlock Grid.Column="0" Grid.Row="1" Margin="5"&gt;&lt;Run Text="Number:"/&gt;&lt;/TextBlock&gt;
          &lt;TextBlock Grid.Column="0" Grid.Row="2" Margin="5"&gt;&lt;Run Text="To Email :"/&gt;&lt;/TextBlock&gt;
          &lt;TextBlock Grid.Column="0" Grid.Row="3" Margin="5"&gt;&lt;Run Text="Salesforce Ref:"/&gt;&lt;/TextBlock&gt;
          &lt;TextBox x:Name="TxtName" Grid.Column="1" Grid.Row="1" Margin="5"/&gt;
          &lt;TextBox x:Name="TxtEmail" Grid.Column="1" Grid.Row="2" Margin="5"/&gt;
          &lt;TextBox x:Name="ref" Grid.Column="1" Grid.Row="3" Margin="5"/&gt;
  
          &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,5,0,0" Grid.Row="4" Grid.ColumnSpan="2"&gt;
              &lt;Button x:Name="ButOk" MinWidth="80" Height="22" Margin="5" Content="OK" /&gt;
              &lt;Button x:Name="ButCancel" MinWidth="80" Height="22" Margin="5" Content="Cancel" IsCancel="True" /&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  '@
  #endregion
  
  #region Code Behind
  function Convert-XAMLtoWindow
  {
    param
    (
      [Parameter(Mandatory)]
      [string]
      $XAML,
  
      [string[]]
      $NamedElement=$null,
  
      [switch]
      $PassThru
    )
  
    Add-Type -AssemblyName PresentationFramework
  
    $reader = [XML.XMLReader]::Create([IO.StringReader]$XAML)
    $result = [Windows.Markup.XAMLReader]::Load($reader)
    foreach($Name in $NamedElement)
    {
      $result | Add-Member NoteProperty -Name $Name -Value $result.FindName($Name) -Force
    }
  
    if ($PassThru)
    {
      $result
    }
    else
    {
      $null = $window.Dispatcher.InvokeAsync{
        $result = $window.ShowDialog()
        Set-Variable -Name result -Value $result -Scope 1
      }.Wait()
      $result
    }
  }
  
  function Show-WPFWindow
  {
    param
    (
      [Parameter(Mandatory)]
      [Windows.Window]
      $Window
    )
  
    $result = $null
    $null = $window.Dispatcher.InvokeAsync{
      $result = $window.ShowDialog()
      Set-Variable -Name result -Value $result -Scope 1
    }.Wait()
    $result
  }
  #endregion Code Behind
  
  #region Convert XAML to Window
  $window = Convert-XAMLtoWindow -XAML $xaml -NamedElement 'ButCancel', 'ButOk', 'ref', 'TxtEmail', 'TxtName' -PassThru
  #endregion
  
  #region Define Event Handlers
  # Right-Click XAML Text and choose WPF/Attach Events to
  # add more handlers
  $window.ButCancel.add_Click(
    {
      $window.DialogResult = $false  
    }
  )
  
  
  $window.ButOk.add_Click(
    {
      $window.DialogResult = $true
    }
  
  )
  #endregion Event Handlers
  
  #region Manipulate Window Content
  #$window.TxtName.Text = $env:username
  $window.ref.Text = $ref
  $window.TxtName.Text = $123
  $window.TxtEmail.Text = $toEmail
  $null = $window.TxtName.Focus()
  
  #endregion
  
  # Show Window
  $result = Show-WPFWindow -Window $window
  </code></pre>
  
  <p>If i use ISE Steroids to add an event I get similar to this </p>
  
  <pre><code>$window.ButOk.add_KeyDown{
    # remove param() block if access to event information is not required
    param
    (
      [Parameter(Mandatory)][Object]$sender,
      [Parameter(Mandatory)][Windows.Input.KeyEventArgs]$e
    )
  
    # add event code here
  }
  </code></pre>