本文介绍了无法通过我使用KeyboardHook的程序向Word发送键击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 i有我的程序捕获称重并将其发送到打开的窗口。 除了使用单一软件外,其工作非常出色 - Microsoft Word 我已尝试过所有内容,但不知道为什么。 我的代码: 这是GlobalKeyBoardHook.cs类 &NBSP; 公共类GlobalKeyboardHook &NBSP; { &NBSP; &NBSP; &NBSP; [DllImport(" user32.dll")] &NBSP; &NBSP; &NBSP; static extern int CallNextHookEx(IntPtr hhk,int code,int wParam,ref keyBoardHookStruct lParam); &NBSP; &NBSP; &NBSP; [DllImport(" user32.dll")] &NBSP; &NBSP; &NBSP; static extern IntPtr SetWindowsHookEx(int idHook,LLKeyboardHook callback,IntPtr hInstance,uint theardID); &NBSP; &NBSP; &NBSP; [DllImport(" user32.dll")] &NBSP; &NBSP; &NBSP; static extern bool UnhookWindowsHookEx(IntPtr hInstance); &NBSP; &NBSP; &NBSP; [DllImport(" kernel32.dll")] &NBSP; &NBSP; &NBSP; static extern IntPtr LoadLibrary(string lpFileName); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; public delegate int LLKeyboardHook(int Code,int wParam,ref keyBoardHookStruct lParam); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; public struct keyBoardHookStruct &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; public int vkCode; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; public int scanCode; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; public int flags; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;公共时间; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; public int dwExtraInfo; &NBSP; &NBSP; &NBSP; } &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; const int WH_KEYBOARD_LL = 13; &NBSP; &NBSP; &NBSP; const int WM_KEYDOWN = 0x0100; &NBSP; &NBSP; &NBSP; const int WM_KEYUP = 0x0101; &NBSP; &NBSP; &NBSP; const int WM_SYSKEYDOWN = 0x0104; &NBSP; &NBSP; &NBSP; const int WM_SYSKEYUP = 0x0105; &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; LLKeyboardHook llkh; &NBSP; &NBSP; &NBSP;公共列表< Keys> HookedKeys =新列表< Keys>(); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; IntPtr Hook = IntPtr.Zero; &NBSP;&NBSP; &NBSP; &NBSP; &NBSP;公共事件KeyEventHandler KeyDown; &NBSP; &NBSP; &NBSP;公共事件KeyEventHandler KeyUp; &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; //这是构造函数。这是每次创建新的GlobalKeyboardHook对象时运行的代码 &NBSP; &NBSP; &NBSP; public GlobalKeyboardHook() &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; llkh = new LLKeyboardHook(HookProc); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //这启动了钩子。你可以将其留作评论,你必须手动启动它(我在教程中用按钮做的事情) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //或者删除评论标记,你的程序启动时会自动启动钩子(因为创建了一个新的GlobalKeyboardHook对象) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //这就是重复的原因,因为你开始了两次!对不起,我没注意到这个... &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // hook(); < - 选择! &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; ~GlobalKeyboardHook() &NBSP; &NBSP; &NBSP; {unhook(); } &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; public void hook() &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; IntPtr hInstance = LoadLibrary(" User32"); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Hook = SetWindowsHookEx(WH_KEYBOARD_LL,llkh,hInstance,0); &NBSP; &NBSP; &NBSP; } &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; public void unhook() &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; UnhookWindowsHookEx(钩); &NBSP; &NBSP; &NBSP; } &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; public int HookProc(int Code,int wParam,ref keyBoardHookStruct lParam) &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(代码> = 0) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Keys key =(Keys)lParam.vkCode; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(HookedKeys.Contains(key)) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; KeyEventArgs kArg = new KeyEventArgs(key); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)&&(KeyDown!= null)) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; KeyDown(this,kArg); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; else if((wParam == WM_KEYUP || wParam == WM_SYSKEYUP)&&(KeyUp!= null)) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; KeyUp(this,kArg); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(kArg.Handled) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回1; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return CallNextHookEx(Hook,Code,wParam,ref lParam); &NBSP; &NBSP; &NBSP; } // ---从这里开始程序 - &NBSP; GlobalKeyboardHook gHook; &NBSP; gHook = new GlobalKeyboardHook(); //创建一个新的GlobalKeyboardHook &NBSP; gHook.KeyDown + = new KeyEventHandler(gHook_KeyDown); &NBSP; foreach(Enum.GetValues中的键键(typeof(Keys))) &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; gHook.HookedKeys.Add(键); &NBSP; } &NBSP; gHook.hook(); &NBSP; public void gHook_KeyDown(object sender,KeyEventArgs e) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(e.KeyValue == KeyPressg) // Pause-Break // 19 &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ProccName =(GetActiveWindowTitle()。ToString()); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;启动(ProccName); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; gHook.unhook(); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; gHook.hook(); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;别的 &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP;私人字符串GetActiveWindowTitle() &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; const int nChars = 256; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; StringBuilder Buff = new StringBuilder(nChars); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; IntPtr handle = GetForegroundWindow(); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(GetWindowText(handle,Buff,nChars)> 0) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return Buff.ToString(); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return null; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP;&NBSP; &NBSP; &NBSP; &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; [DllImport(" user32.dll")] &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; static extern IntPtr GetForegroundWindow(); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; [DllImport(" user32.dll",CharSet = CharSet.Auto)] &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; static extern int GetWindowText(IntPtr hWnd,StringBuilder text,int count); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; [DllImport(" user32.dll")] &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; private static extern IntPtr FindWindow(string lpClassName,string lpWindowName); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; [DllImport(" user32.dll")] &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; private static extern bool SetForegroundWindow(IntPtr hWnd); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; public void Start(string NAME) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; MSG = lblMSG.Text.Trim(); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; IntPtr零= IntPtr.Zero; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; for(int i = 0;(i &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Thread.sleep代码(500); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; zero = FindWindow(null,NAME); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(zero!= IntPtr.Zero) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试 &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string text = lblMSG.Text.Trim(); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; text = text.Replace(" gHook",""); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // OLD &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // foreach(文字中的字符) &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // &NBSP; SendKeys.SendWait(c.ToString()); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // NEW &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(text.Length == 0)返回; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SendKeys.SendWait(文本); &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(MyParam._KeyAfter ==" Enter") &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; MyParam.FromKEY = true; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SendKeys.SendWait(QUOT; {ENTER}"); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则if(MyParam._KeyAfter ==" TAB") &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SendKeys.SendWait(QUOT; {TAB}"); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则if(MyParam._KeyAfter ==" Key Down") &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; { &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SendKeys.SendWait(QUOT; {DOWN}"); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SendKeys.Flush(); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;抓住{} &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } 任何想法为什么? 谢谢你 EDIT i来计算问题的地方: public 无效 开始 ( string NAME ) { // - > NAME包含 - > Word - 123.docx例如当Word窗口打开时 MSG = lblMSG 。 文字 。 修剪 (); IntPtr zero = IntPtr 。 零 ; for ( int i = 0 ; ( i < 60 ) && ( zero == IntPtr 。 Zero ); i ++) { 线程 。 睡眠 ( 500 ); zero = FindWindow ( null , NAME ); //< - 在Word零中始终包含0并且永远不会在代码中继续如此卡住 } //< ---程序停在此处并卡住 如果 ( zero != IntPtr 。 Zero ) { 解决方案 你好E_gold, 你的问题解决了吗? 在我身边,我不知道这个变量是什么意思 " MyParam "。 您可以分享此变量的详细信息。或者您可以将项目上传到 OneDrive 。我们可以重现这个问题并帮助你做得更好。 问候, Youjun Tang hi,i have my program that Captures weighing and send it to a open window.its work excellent , except with a single software - Microsoft WordI have tried everything and have no idea why.my code:this is the GlobalKeyBoardHook.cs class public class GlobalKeyboardHook { [DllImport("user32.dll")] static extern int CallNextHookEx(IntPtr hhk, int code, int wParam, ref keyBoardHookStruct lParam); [DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, LLKeyboardHook callback, IntPtr hInstance, uint theardID); [DllImport("user32.dll")] static extern bool UnhookWindowsHookEx(IntPtr hInstance); [DllImport("kernel32.dll")] static extern IntPtr LoadLibrary(string lpFileName); public delegate int LLKeyboardHook(int Code, int wParam, ref keyBoardHookStruct lParam); public struct keyBoardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } const int WH_KEYBOARD_LL = 13; const int WM_KEYDOWN = 0x0100; const int WM_KEYUP = 0x0101; const int WM_SYSKEYDOWN = 0x0104; const int WM_SYSKEYUP = 0x0105; LLKeyboardHook llkh; public List<Keys> HookedKeys = new List<Keys>(); IntPtr Hook = IntPtr.Zero; public event KeyEventHandler KeyDown; public event KeyEventHandler KeyUp; // This is the Constructor. This is the code that runs every time you create a new GlobalKeyboardHook object public GlobalKeyboardHook() { llkh = new LLKeyboardHook(HookProc); // This starts the hook. You can leave this as comment and you have to start it manually (the thing I do in the tutorial, with the button) // Or delete the comment mark and your hook will start automatically when your program starts (because a new GlobalKeyboardHook object is created) // That's why there are duplicates, because you start it twice! I'm sorry, I haven't noticed this... // hook(); <-- Choose! } ~GlobalKeyboardHook() { unhook(); } public void hook() { IntPtr hInstance = LoadLibrary("User32"); Hook = SetWindowsHookEx(WH_KEYBOARD_LL, llkh, hInstance, 0); } public void unhook() { UnhookWindowsHookEx(Hook); } public int HookProc(int Code, int wParam, ref keyBoardHookStruct lParam) { if (Code >= 0) { Keys key = (Keys)lParam.vkCode; if (HookedKeys.Contains(key)) { KeyEventArgs kArg = new KeyEventArgs(key); if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null)) KeyDown(this, kArg); else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null)) KeyUp(this, kArg); if (kArg.Handled) return 1; } } return CallNextHookEx(Hook, Code, wParam, ref lParam); }// --- from here start the program -- GlobalKeyboardHook gHook; gHook = new GlobalKeyboardHook(); // Create a new GlobalKeyboardHook gHook.KeyDown += new KeyEventHandler(gHook_KeyDown); foreach (Keys key in Enum.GetValues(typeof(Keys))) { gHook.HookedKeys.Add(key); } gHook.hook(); public void gHook_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == KeyPressg) //Pause-Break //19 { ProccName = (GetActiveWindowTitle().ToString()); Start(ProccName); gHook.unhook(); gHook.hook(); } else { } } private string GetActiveWindowTitle() { const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); IntPtr handle = GetForegroundWindow(); if (GetWindowText(handle, Buff, nChars) > 0) { return Buff.ToString(); } return null; } [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); public void Start(string NAME) { MSG = lblMSG.Text.Trim(); IntPtr zero = IntPtr.Zero; for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++) { Thread.Sleep(500); zero = FindWindow(null, NAME); } if (zero != IntPtr.Zero) { try { string text = lblMSG.Text.Trim(); text = text.Replace("gHook", ""); //OLD //foreach (char c in text) // SendKeys.SendWait(c.ToString()); //NEW if (text.Length == 0) return; SendKeys.SendWait(text); if (MyParam._KeyAfter == "Enter") { MyParam.FromKEY = true; SendKeys.SendWait("{ENTER}"); } else if (MyParam._KeyAfter == "TAB") { SendKeys.SendWait("{TAB}"); } else if (MyParam._KeyAfter == "Key Down") { SendKeys.SendWait("{DOWN}"); } SendKeys.Flush(); } catch { } } }Any ideas why ? thanksEDITi fount the problem place: public void Start(string NAME) { // --> NAME Contain --> Word - 123.docx for example when Word window open MSG = lblMSG.Text.Trim(); IntPtr zero = IntPtr.Zero; for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++) { Thread.Sleep(500); zero = FindWindow(null, NAME); // <-- in Word zero Always Contain 0 And never goes on in the code so stuck } // <--- the program stops here and stuck if (zero != IntPtr.Zero) { 解决方案 Hi E_gold,Is your issue solved?In my side, I don't know what is the meaning of this variable "MyParam" .You could share the detail of this variable. Or you could upload your project toOneDrive. we could reproduce the issue and help you better.Regards,Youjun Tang 这篇关于无法通过我使用KeyboardHook的程序向Word发送键击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!