翻译键为char [英] Translate Keys to char

查看:215
本文介绍了翻译键为char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要翻译一个给定的 System.Windows.Forms.Keys 和一个 System.Windows.Forms.InputLanguage 到相应的 System.Char

I want to translate a given set of System.Windows.Forms.Keys and a System.Windows.Forms.InputLanguage to the corresponding System.Char.

尝试了一些实验 MapVirtualKeyEx ,但现在有办法考虑键盘的状态, ToUni codeEX 是一个痛苦的死键的。

Tried some experiments with MapVirtualKeyEx, but there is now way to consider keyboard state, and ToUnicodeEx is a pain with dead keys.

我的目标是函数...

static char? FromKeys(Keys keys, InputLanguage inputLanguage)
{
    // As I think what can be helpful and I got trying to find a solution for this problem:
    Keys vkCode = keys & Keys.KeyCode;
    Keys modifiers = keys & Keys.Modifiers;

    byte[] keyboardState = new byte[256];

    keyboardState[vkCode] = 1 << 7;

    if (modifiers.HasFlag(Keys.Shift))
    {
        keyboardState[(int)Keys.ShiftKey] = 1 << 7;
    }

    if (modifiers.HasFlag(Keys.Control))
    {
        keyboardState[(int)Keys.ControlKey] = 1 << 7;
    }

    if (modifiers.HasFlag(Keys.Alt))
    {
        keyboardState[(int)Keys.Menu] = 1 << 7;
    }

    // [Put your code here]
}

...这应该被称为是这样的:

FromKeys(Keys.A | Keys.Shift, InputLanguage.CurrentInputLanguage); // = 'A'
FromKeys(Keys.Escape, InputLanguage.DefaultInputLanguage); // = null
FromKeys(Keys.Oemtilde, InputLanguage.FromCulture(new CultureInfo("de-DE"))); // = 'ö'
FromKeys(Keys.E | Keys.Control | Keys.Alt, InputLanguage.FromCulture(new CultureInfo("de-DE"))); // = '€'

(请注意 Keys.Shift Keys.ShiftKey !)

如何将这个功能是什么样子?

我会选择应该提供一种答案翻译普通的 System.Windows.Forms.Keys 与改性剂的任意组合( Keys.Shift Keys.Alt Keys.Control )到 System.Char 。多个依次调用必须导致相同的字符。没有硬codeD的键盘布局,在 InputLanguage 定义使用的布局。

The answer I'll choose should provide a way to translate one "normal" System.Windows.Forms.Keys with an optional combination of the modifiers (Keys.Shift, Keys.Alt, Keys.Control) to a System.Char. Multiple successively calls have to result in the same character. No hardcoded keyboard layouts, the InputLanguage defines the layout to use.

作为帮助理解我想要为:生成一个键pressEventArgs 只用 KeyEventArgs (和正确的 InputLangauge )给出。

As a help to understand what I'm trying to to: Generate a KeyPressEventArgs with just the KeyEventArgs (and the correct InputLangauge) given.

死键的应该不会影响 FromKeys

Dead keys should not affect FromKeys!

推荐答案

下面是一张code,似乎你在找什么 - 如果我理解正确的话: - )

Here is a piece of code that seems to do what you're looking for - if I understood it correctly :-)

    public static char? FromKeys(Keys keys, InputLanguage inputLanguage)
    {
        return FromKeys(keys, inputLanguage, true);
    }

    private static char? FromKeys(Keys keys, InputLanguage inputLanguage, bool firstChance)
    {
        if (inputLanguage == null)
        {
            inputLanguage = InputLanguage.CurrentInputLanguage;
        }

        byte[] keyStates = new byte[256];

        const byte keyPressed = 0x80;
        keyStates[(int)(keys & Keys.KeyCode)] = keyPressed;
        keyStates[(int)Keys.ShiftKey] = ((keys & Keys.Shift) == Keys.Shift) ? keyPressed : (byte)0;
        keyStates[(int)Keys.ControlKey] = ((keys & Keys.Control) == Keys.Control) ? keyPressed : (byte)0;
        keyStates[(int)Keys.Menu] = ((keys & Keys.Alt) == Keys.Alt) ? keyPressed : (byte)0;

        StringBuilder sb = new StringBuilder(10);
        int ret = ToUnicodeEx(keys, 0, keyStates, sb, sb.Capacity, 0, inputLanguage.Handle);
        if (ret == 1)
            return sb[0];

        if (ret == -1)
        {
            // dead letter
            if (firstChance)
            {
                FromKeys(keys, inputLanguage, false);
            }
            return null;
        }
        return null;
    }

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern int ToUnicodeEx(Keys wVirtKey, uint wScanCode, byte[] lpKeyState, StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);

这篇关于翻译键为char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆