我正在尝试开发一个软件,它将接受键盘输入,消耗输入并返回键入输入的其他字符不变。例如,如果我输入:“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
。
我认为这是一个解开的问题。任何人都可以帮我解决这个问题吗?
如果不可能,那么任何人都可以推荐任何使用键盘钩子的开源软件吗?
答案 0 :(得分:1)
请勿为您要吞下的事件调用CallNextHookEx
。
编辑: 所以我看到的一个问题是按键生成两个消息WM_KEYDOWN和WM_KEYUP。您正在为两种情况注入新的键盘消息,并将其复制。 Tim Barrass链接的示例显示了如何更好地做到这一点。但这并不能完全解释您所看到的行为。
答案 1 :(得分:0)
如何在发送你想要的新角色之前发送退格键?
AllowKey = true;
SendKeys.Send("{BS}");
SendKeys.Send("\u0997"); // code for russian letters here....
break;