gpt4 book ai didi

c# - 如何用c#解开keyboard-hook开发一个击键修改软件

转载 作者:行者123 更新时间:2023-11-30 18:06:59 25 4
gpt4 key购买 nike

我正在尝试开发一种软​​件,它将接受键盘输入、使用输入并返回一些其他字符而不交换键入的输入。例如,如果我键入:“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

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

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

最佳答案

尽量不要为您想吞下的事件调用 CallNextHookEx

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

关于c# - 如何用c#解开keyboard-hook开发一个击键修改软件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4323219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com