如何转换System.Windows.Input。关键的“System.Windows.Forms.Keys”?

时间:2021-06-11 21:10:35

I'm developing application in WPF but some components are written using WinForms. I wan't these components to pull key gesture from WPF part and convert them to Keys enum (used in WinForms).

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

Is there a built in converter for that? (probably not) Do you know "easier than big switch case" method to do that?

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

3 个解决方案

#1


52  

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

The KeyInterop class is the "key," plus the fact that the Windows Forms Keys enumeration has the same integer values as the Win 32 virtual key codes.

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

#2


1  

Just in case people still encounter the modifier problem 7 years later, here's my solution that worked so far :

仅仅是为了防止人们在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;
    }
}

#3


0  

If you want to convert modifiers, use the SystemKey if you're looking at a KeyEventArgs:

如果您想要转换修饰符,请在查看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);

#1


52  

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

The KeyInterop class is the "key," plus the fact that the Windows Forms Keys enumeration has the same integer values as the Win 32 virtual key codes.

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

#2


1  

Just in case people still encounter the modifier problem 7 years later, here's my solution that worked so far :

仅仅是为了防止人们在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;
    }
}

#3


0  

If you want to convert modifiers, use the SystemKey if you're looking at a KeyEventArgs:

如果您想要转换修饰符,请在查看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);