Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. cant send key stroke only to Word Through my program that use KeyboardHook

cant send key stroke only to Word Through my program that use KeyboardHook

Scheduled Pinned Locked Moved C#
tutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldsoft
    wrote on last edited by
    #1

    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() {

    L D 2 Replies Last reply
    0
    • G goldsoft

      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() {

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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)

      G 1 Reply Last reply
      0
      • G goldsoft

        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() {

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3
        1. 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 of NAME? 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 from FindWindow is null (zero in your case). To get that error you have to call the Win32 function GetLastError 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

        G 1 Reply Last reply
        0
        • D Dave Kreskowiak
          1. 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 of NAME? 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 from FindWindow is null (zero in your case). To get that error you have to call the Win32 function GetLastError 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

          G Offline
          G Offline
          goldsoft
          wrote on last edited by
          #4

          thanks for the help, the name is Exact.... still not working...but only in Word

          L 1 Reply Last reply
          0
          • G goldsoft

            thanks for the help, the name is Exact.... still not working...but only in Word

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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)

            G 1 Reply Last reply
            0
            • L Lost User

              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)

              G Offline
              G Offline
              goldsoft
              wrote on last edited by
              #6

              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.

              L 1 Reply Last reply
              0
              • L Lost User

                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)

                G Offline
                G Offline
                goldsoft
                wrote on last edited by
                #7

                i update my question

                1 Reply Last reply
                0
                • G goldsoft

                  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.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups