Keybd_event下划线提供错误的输出

时间:2016-08-04 15:26:57

标签: c# keyboard-events user32

我创建了一个小程序来模拟我在Oracle中用于填充数据的键盘事件但是当我使用keybd_event引入字符下划线时,输出是错误的 如果_不是最后一个字符,当程序模拟下一个字符时自动添加字符 - 例 我需要录音09_80 对于下划线,我使用VK_RSHIFT与VK_OEM_MINUS的组合 输出为09_-80

有人可以帮助我吗? 我也尝试使用InputSimulator,但它只使用Windows 8正常运行

下面是我程序中的一些代码行, 提前致谢 最好的问候

//键盘操作

    [DllImport("user32.dll", SetLastError = true)]
    public static extern void keybd_event(int bVk, int bScan, int dwFlags, int dwExtraInfo);

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

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern short VkKeyScan(char ch);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern string SendMessage(IntPtr hWnd, int msg, string wParam, IntPtr lParam);

    public const int KEYEVENTF_EXTENDEDKEY = 0x0001;


    public const int KEYEVENTF_KEYUP = 0x0002; 

//发送CHAR

    public static void SendChar(char c)
    {
        if (bw.CancellationPending)
        {
            return;
        }  
        if(c=='_')
        {


            keysPress(VK_RSHIFT,VK_OEM_MINUS)
        }

        int vk = VkKeyScan(c);
        int sc = MapVirtualKey(vk, 0);
        PressKeyDn(vk, sc);
        ReleaseKeyUp(vk, sc);


    }

    //SEND STRING
    public static void SendString(string s)
    {

            foreach (char c in s)
            {
                SendChar(c);
            }

    }

    //PRESS KEY DOWN
    public static void PressKeyDn(int VK_Code, int SC_Code)
    {
        keybd_event(VK_Code, SC_Code, 0, 0);
    }

    //RELEASE KEY UP
    public static void ReleaseKeyUp(int VK_Code, int SC_Code)
    {
        keybd_event(VK_Code, SC_Code, KEYEVENTF_KEYUP, 0);
    }

    //Press Special Key
    public static void keyPress(int VK_key)
    {
        if (bw.CancellationPending)
        {
            return;
        }  
        int sc = MapVirtualKey(VK_key, 0);
        PressKeyDn(VK_key, sc);
        ReleaseKeyUp(VK_key, sc);

    }

    //Press Special Key Combination 2 keys
    public static void keysPress(int VK_Key1,int VK_Key2)
    {
        if (bw.CancellationPending)
        {
            return;
        }  
        int sc1 = MapVirtualKey(VK_Key1, 0);
        int sc2 = MapVirtualKey(VK_Key2, 0);
        PressKeyDn(VK_Key1, sc1);
        PressKeyDn(VK_Key2, sc2);
        ReleaseKeyUp(VK_Key2, sc2);
        ReleaseKeyUp(VK_Key1, sc1);

    }

    //Press Special Key Combination 3 keys
    public static void keysPress(int VK_Key1, int VK_Key2,int VK_Key3)
    {
        if (bw.CancellationPending)
        {
            return;
        }  
        int sc1 = MapVirtualKey(VK_Key1, 0);
        int sc2 = MapVirtualKey(VK_Key2, 0);
        int sc3 = MapVirtualKey(VK_Key3, 0);
        PressKeyDn(VK_Key1, sc1);
        PressKeyDn(VK_Key2, sc2);
        PressKeyDn(VK_Key3, sc3);
        ReleaseKeyUp(VK_Key3, sc3);
        ReleaseKeyUp(VK_Key2, sc2);
        ReleaseKeyUp(VK_Key1, sc1);
    }

1 个答案:

答案 0 :(得分:0)

好的,经过一些测试我发现问题出在哪里,我已经修好了。 _(下划线)表示第二行上的语句的延续,因此下一个char -ped以 - 开头。 我已经修复了这个问题,以这种方式排除了方法中的下一个字符。

public static void SendChar(char c)
    {
        bool und = false;
        if (c == '_')
        {
            und = true;
        }
        int vk = BMDLCommands.VkKeyScan(c);
        int sc = BMDLCommands.MapVirtualKey(vk, 0);
        if(und)
        {
            keysPress(VK_SHIFT, VK_OEM_MINUS);
            MethodInvoker met = delegate
            {
                keyPress(vk);
            };
        }
        else
        {
            BMDLCommands.PressKeyDn(vk, sc);
            BMDLCommands.ReleaseKeyUp(vk, sc);
        }
    }

现在输出完全正确。 非常感谢您的帮助