cant send key stroke only to Word Through my program that use KeyboardHook
-
hi, i have my program that Captures weighing and send it to a open window. its work excellent , except with a single software - **Microsoft Word** I 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 HookedKeys = new List(); 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() {
-
hi, i have my program that Captures weighing and send it to a open window. its work excellent , except with a single software - **Microsoft Word** I 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 HookedKeys = new List(); 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() {
No, and please don't repeat the question. Can you find the Window of Word? That is where you start; if the Window cannot be found, you cannot post anything to it. Now, I'd also like to know what "everything" means, to prevent from advising something you already tried.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
hi, i have my program that Captures weighing and send it to a open window. its work excellent , except with a single software - **Microsoft Word** I 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 HookedKeys = new List(); 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() {
- Why are you even using a keyboard hook? I fail to see how capturing keystrokes system-wide has anything to do with pushing keystrokes to Word. 2) When you call your
Start
method, what is the value ofNAME
? If that value doesn't match EXACTLY the name of the window you're trying to find, it's not going to work. Your ignoring any error that is set if the return value fromFindWindow
is null (zero in your case). To get that error you have to call the Win32 functionGetLastError
and examine the returned value. Read the documentation on FindWindow and GetWindowText for the pitfalls of using these functions.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak - Why are you even using a keyboard hook? I fail to see how capturing keystrokes system-wide has anything to do with pushing keystrokes to Word. 2) When you call your
-
- Why are you even using a keyboard hook? I fail to see how capturing keystrokes system-wide has anything to do with pushing keystrokes to Word. 2) When you call your
Start
method, what is the value ofNAME
? If that value doesn't match EXACTLY the name of the window you're trying to find, it's not going to work. Your ignoring any error that is set if the return value fromFindWindow
is null (zero in your case). To get that error you have to call the Win32 functionGetLastError
and examine the returned value. Read the documentation on FindWindow and GetWindowText for the pitfalls of using these functions.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak - Why are you even using a keyboard hook? I fail to see how capturing keystrokes system-wide has anything to do with pushing keystrokes to Word. 2) When you call your
-
goldsoft wrote:
still not working...
I don't think Dave is going to fix your code; he gave some hints on how you can fix it. If you are stuck then point out where, and if the explanation is unclear, ask for more :) Still looks like FindWindow is getting a title that does not exist in the collection of open Windows.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
goldsoft wrote:
still not working...
I don't think Dave is going to fix your code; he gave some hints on how you can fix it. If you are stuck then point out where, and if the explanation is unclear, ask for more :) Still looks like FindWindow is getting a title that does not exist in the collection of open Windows.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
thanks again for the help, the problem is when i send key stroke on open Word windows - my program Freezes without any error. but if i send key stroke on any other open windows (excel or explorer or notepad...) its work excellent. I just feel lost, I've tried everything and have no idea what the problem is.
-
No, and please don't repeat the question. Can you find the Window of Word? That is where you start; if the Window cannot be found, you cannot post anything to it. Now, I'd also like to know what "everything" means, to prevent from advising something you already tried.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
thanks again for the help, the problem is when i send key stroke on open Word windows - my program Freezes without any error. but if i send key stroke on any other open windows (excel or explorer or notepad...) its work excellent. I just feel lost, I've tried everything and have no idea what the problem is.
I'd recommend getting the classname for the Word-window. Should be obtainable using WinSpy. Example usage[^] If FindWindow fails, you probably want to know why it failed; some tips on how are described here[^].
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)