在当前文化中代表特殊的键盘键吗? [英] Represent an special keyboard key in the current culture?

查看:157
本文介绍了在当前文化中代表特殊的键盘键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用现代的 RawInput 技术编写了一个简单的键盘记录程序,用于注册所需的设备以进行事件/数据拦截.

I've written a simple keylogger using the modern RawInput technique registering the desired device for event/data interception.

  • 然后,我基本上使用了所有这些 Windows API 成员定义:

    Then, I'm using basically all these Windows API member definitions:

    我将非英语键盘与非英语 OS 一起使用,那么,当我尝试解析该键盘的特殊键(例如ñ)时,我的问题就开始了. strong>/Ñ字符,它被识别为System.Windows.Forms.Keys.OemTilde键, 或被识别为System.Windows.Forms.Keys.OemQuestion键的ç/Ç字符.

    I'm using a non-English keyboard with a non-English O.S, then, my problem begins when I try to parse an special key of this keyboard like a ñ/Ñ character which is recognized as an System.Windows.Forms.Keys.OemTilde key, or a ç/Ç character which is recognized as an System.Windows.Forms.Keys.OemQuestion key.

    我想使按键记录程序了解特定语言(或至少对我当前的文化 es-ES 具有正确的字符识别),但是由于缺乏相关知识,我陷入了困境开始正确地检索那些字符.

    I would like to make my keylogger lenguage-specific aware (or at least, with proper character recognition for my current culture, es-ES), but I'm stuck because lack of knowledges to start retrieving properlly those characters.

    请注意,我的目的是学习如何按Ñ字符时用 OS 用键盘进行高效/自动的操作它输入的是Ñ,我的意思是我完全了解一种解决方案,该方案意味着必须手动解析特殊字符,例如:

    Please, note that my intention is to learn how I can do it in an efficient/automated way like the O.S does with my keyboard when I press an Ñ character it types that Ñ, what I mean is that I'm totally aware of a solution that implies to perform a manual parsing of special characters like for example this:

    Select Case MyKey
    
        Case Keys.OemTilde
            char = "ñ"c
    
    End Select
    

    这不是我想要的行为,但是我可以理解,也许我需要其他东西"才能对每种keayborad字符进行良好的识别/翻译,但是我需要什么东西" ?.

    That is not the behavior that I'm looking for, but I can understand that maybe I need additional "things" to reproduce a good recognition/translation of those chars for each kind of keayborad, but what "things" I need?.

    我不确定该如何进行,因为正如我所说,我不具备该问题答案的知识(这就是我要问的原因),但是我可以想象当前键盘的知识将涉及布局,那么,我知道可以使用CultureInfo.CurrentCulture.KeyboardLayoutId属性检索当前的键盘布局.

    I'm not sure how to proceed, because as I said, I don't have the knowledges to know the answer to this problem (that's why I'm asking), but I imagine that the knowledge of the current keyboard layout will be involved, then, I know that I can retrieve the current keyboard layout with the CultureInfo.CurrentCulture.KeyboardLayoutId property.

    我知道,区域性 zh-CN 的键盘布局是 1033 ,区域性 es-ES 的键盘布局是 3082 .

    I know that the keyboard layout for culture en-US is 1033, and for culture es-ES is 3082.

    此外,请注意RAWKEYBOARD结构的MakeCode成员的文档,也许这似乎是我假装要做的提示,我不知道:

    Also, note the documentation of the the MakeCode member of the RAWKEYBOARD structure, maybe it seems to be a hint for what I pretend to do, I don't know:

    MakeCode

    MakeCode

    类型:USHORT

    按键按下时的扫描代码. 键盘溢出的扫描代码为KEYBOARD_OVERRUN_MAKE_CODE.

    The scan code from the key depression. The scan code for keyboard overrun is KEYBOARD_OVERRUN_MAKE_CODE.

    推荐答案

    但实际上这是一个猜测 这是我找到的代码.

    but actually it is a guess work Here is the code I found.

    正确的解决方案是ToUnicode WinAPI函数:

    The correct solution is the ToUnicode WinAPI function:

    [DllImport("user32.dll")]
    public static extern int ToUnicode(uint virtualKeyCode, uint scanCode,
        byte[] keyboardState,
        [Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
        StringBuilder receivingBuffer,
        int bufferSize, uint flags);
    
    
    
    static string GetCharsFromKeys(Keys keys, bool shift, bool altGr)
    {
        var buf = new StringBuilder(256);
        var keyboardState = new byte[256];
        if (shift)
            keyboardState[(int) Keys.ShiftKey] = 0xff;
        if (altGr)
        {
            keyboardState[(int) Keys.ControlKey] = 0xff;
            keyboardState[(int) Keys.Menu] = 0xff;
        }
        WinAPI.ToUnicode((uint) keys, 0, keyboardState, buf, 256, 0);
        return buf.ToString();
    }
    
    
    Console.WriteLine(GetCharsFromKeys(Keys.E, false, false));    // prints e
    Console.WriteLine(GetCharsFromKeys(Keys.E, true, false));     // prints E
    
    // Assuming British keyboard layout:
    Console.WriteLine(GetCharsFromKeys(Keys.E, false, true));     // prints é
    Console.WriteLine(GetCharsFromKeys(Keys.E, true, true));      // prints É
    

    这篇关于在当前文化中代表特殊的键盘键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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