【问题标题】:How to show button is pressed in UI for combination keys如何在 UI 中为组合键显示按钮被按下
【发布时间】:2015-11-01 05:32:08
【问题描述】:

其实这篇文章和this(也是我发的)不同。我的 UI 中有一个电话键盘,现在我可以显示按下相应键时按下的按钮。

private void NumDisplayBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
  switch (e.Key) 
  {
    case Key.D0:
    case Key.NumPad0:
      ZeroBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D1:
    case Key.NumPad1:
      OneBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D2:
    case Key.NumPad2:
      TwoBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D3:
    case Key.NumPad3:
      ThreeBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D4:
    case Key.NumPad4:
      FourBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D5:
    case Key.NumPad5:
      FiveBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D6:
    case Key.NumPad6:
      SixBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D7:
    case Key.NumPad7:
      SevenBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D8:
    case Key.NumPad8:
      EightBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
    case Key.D9:
    case Key.NumPad9:
      NineBtn.Style = (Style) FindResource("PressedButtonStyle");
      break;
  }

  if (((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift))
  {
    if (e.Key == Key.D3)
    {
      ThreeBtn.Style = (Style)FindResource("NormalButtonStyle");
      HashBtn.Style = (Style)FindResource("PressedButtonStyle");
    }
  }
}

private void NumDisplayBox_PreviewKeyUp(object sender, KeyEventArgs e) 
{
  switch (e.Key) 
  {
    case Key.D0:
    case Key.NumPad0:
      ZeroBtn.Style = (Style) FindResource("ButtonStyle4"); break;
    case Key.D1:
    case Key.NumPad1:
      OneBtn.Style = (Style) FindResource("ButtonStyle4"); break;

    .
    .
    .
  }
}

问题是当哈希键 (Shift + 3) 或星号键 (Shift + 8) 按下时,在 UI 中也会按下“3”按钮(用于大小写哈希键)。如何显示当 Shift + 3 按下时在 UI 上只按下哈希按钮,而不是哈希按钮和“3”按钮?

【问题讨论】:

    标签: c# wpf button keyboard


    【解决方案1】:

    检查KeyEventArgs.Modifiers 属性,仅在未设置修饰符时处理按键。

    或者,使用TextInputPreviewTextInput 事件来获取组合文本而不是实际密钥。无论如何,这可能更可取,因为它将响应输入适当文本输入的任何方法,而不是依赖于特定键(例如,您不必为所有可能的键设置大小写,例如顶部-行数字键和数字键盘键,就像现在一样)。

    【讨论】:

    • 嗨@PeterDuniho,如果我使用“TextInput”或“PreviewTextInput”,如何捕获我输入的每个单独的字符?如果我设置'if (textbox.Text == "#"',它必须是整个字符串是"#",而不是一个字符。
    • @YWah:TextInput 事件大致(非常大致)类似于 Winforms 的 KeyPress 事件。通常,字符串实际上是单个字符长。它是一个字符串,因为某些输入法实际上可以生成多字符输入。但大多数没有。是的,这意味着例如您的表达式将看起来像 e.Text == "#" 而不是例如e.Key == Key.D3 && (e.Modifiers & ModifierKeys.Shift) != 0。但这对我来说似乎不是问题。
    【解决方案2】:

    这对我有用:

    //Make the button to have "pressed" feel when the corresponding key is pressed
    
    private void NumDisplayBox_PreviewKeyDown(object sender, KeyEventArgs e) 
    {
        if (((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift))
        {
          if (e.Key == Key.D3)
            HashBtn.Style = (Style) FindResource("PressedButtonStyle");
    
          if (e.Key == Key.D8)
            StarBtn.Style = (Style) FindResource("PressedButtonStyle");
        }
    
        switch (e.Key) 
        {
          case Key.D0:
          case Key.NumPad0:
            ZeroBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          case Key.D1:
          case Key.NumPad1:
            OneBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          case Key.D2:
          case Key.NumPad2:
            TwoBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          case Key.D3:
          case Key.NumPad3:
            {
              if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
                ThreeBtn.Style = (Style) FindResource("ButtonStyle4");
    
              else
                ThreeBtn.Style = (Style) FindResource("PressedButtonStyle");
    
              break;
            }
          case Key.D4:
          case Key.NumPad4:
            FourBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          case Key.D5:
          case Key.NumPad5:
            FiveBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          case Key.D6:
          case Key.NumPad6:
            SixBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          case Key.D7:
          case Key.NumPad7:
            SevenBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          case Key.D8:
          case Key.NumPad8:
            {
              if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
                EightBtn.Style = (Style) FindResource("ButtonStyle4");
    
              else
                EightBtn.Style = (Style) FindResource("PressedButtonStyle");
    
              break;
            }
          case Key.D9:
          case Key.NumPad9:
            NineBtn.Style = (Style) FindResource("PressedButtonStyle");
            break;
          default:
            break;
        }
    }
    
    //Return back to its original style
    
    private void NumDisplayBox_PreviewKeyUp(object sender, KeyEventArgs e)
    {
      switch (e.Key) 
      {
        case Key.D0:
        case Key.NumPad0:
          ZeroBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D1:
        case Key.NumPad1:
          OneBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D2:
        case Key.NumPad2:
          TwoBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D3:
        case Key.NumPad3:
          ThreeBtn.Style = (Style) FindResource("ButtonStyle4");
          HashBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D4:
        case Key.NumPad4:
          FourBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D5:
        case Key.NumPad5:
          FiveBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D6:
        case Key.NumPad6:
          SixBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D7:
        case Key.NumPad7:
          SevenBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D8:
        case Key.NumPad8:
          EightBtn.Style = (Style) FindResource("ButtonStyle4");
          StarBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        case Key.D9:
        case Key.NumPad9:
          NineBtn.Style = (Style) FindResource("ButtonStyle4");
          break;
        default:
          break;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2015-09-20
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多