|
楼主 |
发表于 2023-4-29 16:27
|
显示全部楼层
老哥你好,能帮忙看看我写的代码吗?
- ```cs
- //private FontFormat lastFontFormat;
- private delegate int LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam);
- private const int WH_KEYBOARD_LL = 13;
- private const int WM_KEYDOWN = 0x0100;
- private LowLevelKeyboardProc hookProc;
- private int hookId = 0; //声明键盘钩子处理的初始值
- private IntPtr ModuleHandle = IntPtr.Zero;
- public void RegPPTHook()
- {
- if (hookId == 0)
- {
- //创建钩子回调函数对象
- hookProc = new LowLevelKeyboardProc(HookCallback);
- using(Process process = Process.GetCurrentProcess())
- using(ProcessModule module = process.MainModule)
- {
- hookId = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, GetModuleHandle(module.ModuleName), 0);
- }
- // 如果设置钩子失败
- if (hookId == 0)
- {
- Unhook();
- throw new Exception("安装键盘钩子失败");
- }
- //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
- }
- }
- //回调
- private int HookCallback(int nCode, int wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == (int)WM_KEYDOWN)
- {
- int vkCode = Marshal.ReadInt32(lParam);
- if (vkCode == (int)Keys.F3)
- {
- //MessageBox.Show("用户按下了 F3");
- //Process Test_proc = Process.GetCurrentProcess();
- //ProcessModule Test_processModule = Test_proc.MainModule;
- //string Test_modulename = Test_processModule.ModuleName;
- //IntPtr Test_ModuleHandle = GetModuleHandle(Test_modulename);
- //MessageBox.Show(
- // "当前进程是:" + Test_proc.ToString() + Environment.NewLine +
- // "当前主模块是:" + Test_processModule.ToString() + Environment.NewLine +
- // "当前主模块名称是:" + Test_modulename.ToString() + Environment.NewLine +
- // "当前主模块句柄是:" + Test_ModuleHandle.ToString()
- // );
- RepeatAction.RepeatLastAction();//重复上一次操作,功能已实现,正常使用
- }
- }
- return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
- }
- //卸载钩子
- private void Unhook()
- {
- bool retKeyboard = true;
- if (hookId != 0)
- {
- retKeyboard = UnhookWindowsHookEx(hookId);
- hookId = 0;
- }
- if (!(retKeyboard))
- throw new Exception("卸载钩子失败!");
- }
- /// <summary>
- /// 安装钩子
- /// </summary>
- /// //键盘线程钩子
- /// SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId());//指定要监听的线程idGetCurrentThreadId(),
- //键盘全局钩子,需要引用空间(using System.Reflection;)
- //SetWindowsHookEx( 13,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
- /// <param name="idHook">钩子类型,鼠标\键盘\巨集等10几种,即确定钩子监听何种消息,
- /// 监听键盘消息并且是线程钩子,消息应设为2,
- /// 如果是全局钩子监听键盘消息应设为13,
- /// 线程钩子监听鼠标消息设为7,
- /// 全局钩子监听鼠标消息设为14</param>
- /// <param name="lpfn">挂钩的函数,用来处理拦截消息的函数,全局函数。
- /// 如果dwThreadId参数为0 或是一个由别的进程创建的线程的标识,lpfn必须指向DLL中的钩子子程。 除此以外,lpfn可以指向当前进程的一段钩子子程代码。钩子函数的入口地址,当钩子钩到任何消息后便调用这个函数</param>
- /// <param name="hMod">当前进程的句柄,
- /// 为NULL:当前进程创建的一个线程,子程位于当前进程;
- /// 为0(IntPtr.Zero):钩子子程与所有的线程关联,即为全局钩子。
- /// 标识包含lpfn所指的子程的DLL。如果threadId 标识当前进程创建的一个线程,而且子程代码位于当前进程,hInstance必须为0。可以很简单的设定其为本应用程序的实例句柄。</param>
- /// <param name="dwThreadId">设置要挂接的线程ID.为0则为全局钩子</param>
- /// <returns></returns>
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
- //卸载钩子
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern bool UnhookWindowsHookEx(int hhk);
- //继续下一个钩子
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern int CallNextHookEx(IntPtr hhk, int nCode, int wParam, IntPtr lParam);
- //获取模块句柄
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr GetModuleHandle(string lpModuleName);
- // 取得当前线程编号
- [DllImport("kernel32.dll")]
- private static extern int GetCurrentThreadId();
- ```
复制代码
|
|