我正在尝试开发一个软件,它将接受键盘输入,使用输入并返回输入的输入的一些其他字符交换。例如,如果我输入:“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
。
我觉得是脱钩的问题。谁能帮我解决这个问题?
如果不可能,那么任何人都可以向我推荐任何使用键盘挂钩的开源软件吗?