如何获得适合给定 KeyDown 事件的角色?

发布于 2024-10-14 13:48:26 字数 36 浏览 6 评论 0原文

如何获取适合 WPF 中给定 KeyDown 事件的字符?

How do you get the character appropriate for the given KeyDown event in WPF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难如初 2024-10-21 13:48:26
 public enum MapType : uint
    {
        MAPVK_VK_TO_VSC = 0x0,
        MAPVK_VSC_TO_VK = 0x1,
        MAPVK_VK_TO_CHAR = 0x2,
        MAPVK_VSC_TO_VK_EX = 0x3,
    }
    [DllImport("user32.dll")]
    public static extern bool GetKeyboardState(byte[] lpKeyState);
    [DllImport("user32.dll")]
    public static extern uint MapVirtualKey(uint uCode, MapType uMapType);
    [DllImport("user32.dll")]
    public static extern int ToUnicode(
     uint wVirtKey,
     uint wScanCode,
     byte[] lpKeyState,
     [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)] 
        StringBuilder pwszBuff,
     int cchBuff,
     uint wFlags);

    public static char GetCharFromKey(Key key)
    {
        char ch = ' ';

        int virtualKey = KeyInterop.VirtualKeyFromKey(key);
        byte[] keyboardState = new byte[256];
        GetKeyboardState(keyboardState);

        uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
        StringBuilder stringBuilder = new StringBuilder(2);

        int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
        switch (result)
        {
            case -1:
                break;
            case 0:
                break;
            case 1:
                {
                    ch = stringBuilder[0];
                    break;
                }
            default:
                {
                    ch = stringBuilder[0];
                    break;
                }
        }
        return ch;
    }
 public enum MapType : uint
    {
        MAPVK_VK_TO_VSC = 0x0,
        MAPVK_VSC_TO_VK = 0x1,
        MAPVK_VK_TO_CHAR = 0x2,
        MAPVK_VSC_TO_VK_EX = 0x3,
    }
    [DllImport("user32.dll")]
    public static extern bool GetKeyboardState(byte[] lpKeyState);
    [DllImport("user32.dll")]
    public static extern uint MapVirtualKey(uint uCode, MapType uMapType);
    [DllImport("user32.dll")]
    public static extern int ToUnicode(
     uint wVirtKey,
     uint wScanCode,
     byte[] lpKeyState,
     [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)] 
        StringBuilder pwszBuff,
     int cchBuff,
     uint wFlags);

    public static char GetCharFromKey(Key key)
    {
        char ch = ' ';

        int virtualKey = KeyInterop.VirtualKeyFromKey(key);
        byte[] keyboardState = new byte[256];
        GetKeyboardState(keyboardState);

        uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
        StringBuilder stringBuilder = new StringBuilder(2);

        int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
        switch (result)
        {
            case -1:
                break;
            case 0:
                break;
            case 1:
                {
                    ch = stringBuilder[0];
                    break;
                }
            default:
                {
                    ch = stringBuilder[0];
                    break;
                }
        }
        return ch;
    }
尴尬癌患者 2024-10-21 13:48:26

试试这个:

var virtualKey = (uint)KeyInterop.VirtualKeyFromKey(e.Key);
var keyCode = MapVirtualKey(virtualKey, 0);

导入函数 MapVirtualKey

[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode, uint uMapType);

Try this:

var virtualKey = (uint)KeyInterop.VirtualKeyFromKey(e.Key);
var keyCode = MapVirtualKey(virtualKey, 0);

Import function MapVirtualKey

[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode, uint uMapType);
电影里的梦 2024-10-21 13:48:26

并非所有 KeyDown 事件都对应于单个可见字符,例如 Backspace 和 Enter。您必须将事件参数的内容与您想要关联的内容进行比较。例如,在我的 US QUERTY 键盘上,左上角的键是反勾号键,但对于另一个键盘来说,它可能看起来像其他键。

Not all KeyDown events correspond to a single, visible character -- for instance, Backspace and Enter. You will have to compare the event arg's contents with what you want to associate. For example, on my US QUERTY keyboard, the key at the top left is a back-tick, but it might look like something else to another keyboard.

嘿哥们儿 2024-10-21 13:48:26
private void UserControl1_KeyDown_1(object sender, KeyEventArgs e)
{   
    FieldInfo field = e.GetType().GetField
    (
       "_scanCode",
       BindingFlags.NonPublic |
       BindingFlags.Instance
    );

    Int32 scancode = (Int32)field.GetValue(e);

    char c = (char)scancode;
}
private void UserControl1_KeyDown_1(object sender, KeyEventArgs e)
{   
    FieldInfo field = e.GetType().GetField
    (
       "_scanCode",
       BindingFlags.NonPublic |
       BindingFlags.Instance
    );

    Int32 scancode = (Int32)field.GetValue(e);

    char c = (char)scancode;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文