System.InvalidOperationException SendKeys上的空队列

时间:2015-12-11 07:09:11

标签: c# keyboard hook keyboard-hook

我想用键盘钩子来替换另一个键,例如如果使用按“ש”然后windows应用程序得到“א”等等,这个想法工作正常,但当我设置条件使这个当前语言为“希伯来语”时替换,我的应用程序在此行System.InvalidOperationException上抛出Empty Queue SendKeys.SendWait(char.ConvertFromUtf32((int)res));,这是我的钩子方法

 private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
    {
       if (getCurrentKeyboardLangauge().Contains("he"))
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);

                if (vkCode == 160 || vkCode == 161) { shift = 1; }
                if (vkCode == 162 || vkCode == 163) { ctrl = 1; }
                if (vkCode == 164 || vkCode == 165) { alt = 1; }
                Console.WriteLine(vkCode + "_" + shift);
                int res = replaceKey.getReplacedKey(vkCode, shift);
                if (res != -1 && ctrl==0 & alt==0)
                {
                    SendKeys.SendWait(char.ConvertFromUtf32((int)res));
                    return (System.IntPtr)1;
                }else if(vkCode == 79 || vkCode == 89 ||  vkCode == 69 ||  vkCode == 81 ||  vkCode == 220 ||  vkCode == 77 ||  vkCode == 78 ||  vkCode == 66 ||  vkCode == 76 ||  vkCode == 88 ||  vkCode == 90 ||  vkCode == 226){
                    return (System.IntPtr)1;
                }
            }
            else if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Console.WriteLine(vkCode);
                if (vkCode == 160 || vkCode == 161) { shift = 0; }
                if (vkCode == 162 || vkCode == 163) { ctrl = 0; }
                if (vkCode == 164 || vkCode == 165) { alt = 0; }                
            }
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    private static string getCurrentKeyboardLangauge()
    {
        return InputLanguage.CurrentInputLanguage.Culture.Name;
    }

现在,这段代码有什么问题?请帮忙

0 个答案:

没有答案