如何将'System.Windows.Input.Key'转换为'System.Windows.Forms.Keys'?

时间:2009-07-20 11:27:14

标签: .net wpf

我正在使用WPF开发应用程序,但有些组件是使用WinForms编写的。我不希望这些组件从WPF部分拉出关键手势并将它们转换为Keys enum(在WinForms中使用)。

是否有内置转换器? (可能不是) 你知道“比大开关案例更容易”的方法吗?

4 个答案:

答案 0 :(得分:55)

Keys formsKey = ...;
Key wpfKey = ...;
wpfKey = KeyInterop.KeyFromVirtualKey((int)formsKey);
formsKey = (Keys)KeyInterop.VirtualKeyFromKey(wpfKey);

KeyInterop class是“密钥”,加上Windows窗体Keys枚举与Win 32虚拟密钥代码具有相同的整数值。

答案 1 :(得分:3)

万一人们在7年后仍然遇到修饰问题,这是我迄今为止的解决方案:

public static class KeyEventExts
{
    public static System.Windows.Forms.KeyEventArgs ToWinforms(this System.Windows.Input.KeyEventArgs keyEventArgs)
    {
        // So far this ternary remained pointless, might be useful in some very specific cases though
        var wpfKey = keyEventArgs.Key == System.Windows.Input.Key.System ? keyEventArgs.SystemKey : keyEventArgs.Key;
        var winformModifiers = keyEventArgs.KeyboardDevice.Modifiers.ToWinforms();
        var winformKeys = (System.Windows.Forms.Keys)System.Windows.Input.KeyInterop.VirtualKeyFromKey(wpfKey);
        return new System.Windows.Forms.KeyEventArgs(winformKeys | winformModifiers);
    }

    public static System.Windows.Forms.Keys ToWinforms(this System.Windows.Input.ModifierKeys modifier)
    {
        var retVal = System.Windows.Forms.Keys.None;
        if(modifier.HasFlag(System.Windows.Input.ModifierKeys.Alt))
        {
            retVal |= System.Windows.Forms.Keys.Alt;
        }
        if (modifier.HasFlag(System.Windows.Input.ModifierKeys.Control))
        {
            retVal |= System.Windows.Forms.Keys.Control;
        }
        if (modifier.HasFlag(System.Windows.Input.ModifierKeys.None))
        {
            // Pointless I know
            retVal |= System.Windows.Forms.Keys.None;
        }
        if (modifier.HasFlag(System.Windows.Input.ModifierKeys.Shift))
        {
            retVal |= System.Windows.Forms.Keys.Shift;
        }
        if (modifier.HasFlag(System.Windows.Input.ModifierKeys.Windows))
        {
            // Not supported lel
        }
        return retVal;
    }
}

答案 2 :(得分:0)

如果要转换修饰符,请在查看KeyEventArgs时使用SystemKey:

System.Windows.Input.KeyEventArgs args;
System.Windows.Input.Key wpfKey= args.Key == Key.System ? args.SystemKey : args.Key;
formsKey = (System.Windows.Forms.Keys)KeyInterop.VirtualKeyFromKey(wpfKey);

答案 3 :(得分:0)

要将WPF Key enumeration转换为相应的WinForms Keys enumeration,请使用Enum类的静态成员TryParse

Enum.TryParse(wpfKeyEnum.ToString(), out System.Windows.Forms.Keys winFormsKeyEnum)

WPF修饰符(ModifierKeys enumeration)的转换方式与Windows键相同。与WPF的Windows.Input.ModifierKeys枚举相反,Windows.Forms.Keys枚举区分左Windows键和右Windows键,并定义相应的LWinRWin字段。

此转换方法可双向使用。

示例

该示例将WPF键向上事件的KeyModifierKeys枚举转换为相应的WinForms Keys枚举。

Windows.Input.KeySystem.Windows.Forms.Keys

private void OnPreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{ 
  // Convert key
  if (Enum.TryParse(e.Key.ToString(), out System.Windows.Forms.Keys winFormsKey))
  {
    MessageBox.Show(winFormsKey + "=" + (int) winFormsKey); // A=65
  }
}

Windows.Input.ModifierKeysSystem.Windows.Forms.Keys

private void OnPreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{ 
  ModifierKeys modifiers = e.KeyboardDevice.Modifiers;

  IEnumerable<ModifierKeys> pressedModifierKeys  = Enum.GetValues(modifiers.GetType())
    .Cast<ModifierKeys>()
    .Where(modifiers.HasFlag);

  // The ModifierKeys enumeration has a FlagsAttribute attribute
  foreach (ModifierKeys modifier in pressedModifierKeys)
  {
    if (Enum.TryParse(modifier.ToString(), out System.Windows.Forms.Keys winFormsModifierKey))
    {
      MessageBox.Show(winFormsModifierKey + "=" + (int) winFormsModifierKey); // Alt=262144
    }
  }
}