Powershell WPF RichTextBox Surpress Backspace 不起作用

问题描述

我目前正在为一些公司流程开发一些自动化服务。我目前有一个小图形用户界面来编辑 JSON 文件,我正在捕获和监控关键输入,在可编辑的 JSON 语法之外压制它。然而,退格键给我带来了一些麻烦,我已经读到它不是由 KeyDown 捕获的,而是仅由 KeyUp 捕获的。确实如此,按下它时 KeyUp 会触发,但是无论我做什么,我都无法让它忽略输入。我已经将 .Handled 设置为 $true 和 .SurpressKeyPress,但都没有任何效果。我已经检查了处理程序末尾的值,它们是正确的。任何帮助将不胜感激!

$richTextBox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
    #Write-Host ($_ | Format-List | Out-String)
    if($global:canType -eq $false) {
        $_.Handled = $true
    }
}

$richTextBox1_KeyUp = {
    if($global:canType -eq $false) {
        $_.Handled = $true
        $_.SuppressKeyPress = $true
    }
    Write-Host ($_ | Format-List | Out-String)
}
  • 也许有人可以向我解释为什么我必须将 KeyPress 事件强制转换为 [System.Windows.Forms.KeyPressEventHandler] 以便在管道内拥有 Handled 变量。否则只能读取 keyCode 而没有其他属性
  • 编辑 脚本块分配

      $richTextBox1.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([system.int32]0,[system.int32]0))
      $richTextBox1.Name = [System.String]'richTextBox1'
      $richTextBox1.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([system.int32]510,[system.int32]182))
      $richTextBox1.TabIndex = [system.int32]0
      $richTextBox1.Text = [System.String]''
      $richTextBox1.add_SelectionChanged($richTextBox1_SelectionChanged)
      $richTextBox1.add_KeyPress($richTextBox1_KeyPress)
      $richTextBox1.add_KeyUp($richTextBox1_KeyUp)
      $richTextBox1.add_MouseUp($richTextBox1_MouseUp)
    

解决方法

所以正如 Theo 指出的那样,最好使用 KeyDown 作为事件处理程序。 但是,我遇到了一个问题。我不知道为什么,但是设置 $_.Handled = $true 在 95% 的情况下都有效,但是似乎存在一种情况,即 az 1-9 仍然通过,并且只有在您还设置 $_.SuppressKeyPress = $true

  • 来自我的代码的示例

      $richTextBox1_KeyDown = {
          if($global:canType -eq $false) {
              if(!(@('Left','Up','Right','Down') -contains $_.KeyCode) -or ($_.KeyCode -eq 'Enter')) {
                  $_.Handled = $true
                  $_.SuppressKeyPress = $true
              }
          }
      }
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...