3

我正在尝试开发一个软件,它将接受键盘输入,使用输入并返回输入的输入的一些其他字符交换。例如,如果我输入:“abcd”并将交换规则定义为任何俄语字母,那么我希望输出为:“ский”。

我正在使用的代码如下:

namespace hook_form
{
    public partial class Form1 : Form
    {
        //Keyboard API constants

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x0101;
        private const int WM_SYSKEYUP = 0x0105;
        private const int WM_SYSKEYDOWN = 0x0104;
        private HookHandlerDelegate proc;
        private IntPtr hookID = IntPtr.Zero;
        private delegate IntPtr HookHandlerDelegate(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);

        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            int scanCode;
            public int flags;
            int time;
            int dwExtraInfo;
        }

        private IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool AllowKey = false;

            switch (lParam.vkCode)
            {
                case (65|97|66|98): // key codes for "a/b/c/d" etc. goes here
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
                    AllowKey = true;
                    SendKeys.Send("\u0997"); // code for russian letters here....
                    break;
            }

            MessageBox.Show(lParam.vkCode.ToString());

            if (AllowKey == false)
                return (System.IntPtr)1;

            return CallNextHookEx(hookID, nCode, wParam, ref lParam);
        }
        #region DllImports

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, 
        HookHandlerDelegate lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, 
        IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName); 
        #endregion

        private void buttonStart_Click(object sender, EventArgs e)
        {
            proc = new HookHandlerDelegate(HookCallback);
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                hookID = SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            UnhookWindowsHookEx(hookID);
        }
    }
}

我得到的输出为:“aсbкcиdй”,我的意思是输入的字母(英文)不会被代码消耗(我的意思是它们不应该出现在输出中),即使我返回了(System.IntPtr)1

我觉得是脱钩的问题。谁能帮我解决这个问题?

如果不可能,那么任何人都可以向我推荐任何使用键盘挂钩的开源软件吗?

4

2 回答 2

1

尽量不要呼吁CallNextHookEx你想吞下的事件。

编辑:所以我看到的一个问题是按键会生成两条消息 WM_KEYDOWN 和 WM_KEYUP。您正在为这两种情况注入新的键盘消息,并复制它。Tim Barrass 链接中的示例显示了如何更好地做到这一点。但是,这并不能完全解释您所看到的行为。

于 2010-12-01T14:53:05.607 回答
0

在发送您想要的新字符之前发送退格键如何?

AllowKey = true;
SendKeys.Send("{BS}");
SendKeys.Send("\u0997"); // code for russian letters here....
break;
于 2010-12-01T14:46:43.373 回答