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. Threads and KeyBoard Hook

Threads and KeyBoard Hook

Scheduled Pinned Locked Moved C#
question
6 Posts 2 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.
  • R Offline
    R Offline
    ranzask
    wrote on last edited by
    #1

    Hi all, i am using a clasic low-level hook for the keyboard and runing it in the main class: kBHook = new KeyBoardHook(); kBHook.KeyPress+=new KeyPressEventHandler(kBHook_KeyPress);/// kBHook.KeyDown+=new KeyEventHandler(kBHook_KeyDown); kBHook.KeyUp+=new KeyEventHandler(kBHook_KeyUp); so KeyBoardHook is another class on my project. it happens that when my program is busy doing calculations the keyboard class misses KEYUP/KEYDOWN messages. so my questions are: 1. how can i make another thread that always listen to messages? 2. can i try to hook to keyboard in a way that i will always get the messages (i need my program to get those msgs before others) Thank you,

    R.Z

    J 1 Reply Last reply
    0
    • R ranzask

      Hi all, i am using a clasic low-level hook for the keyboard and runing it in the main class: kBHook = new KeyBoardHook(); kBHook.KeyPress+=new KeyPressEventHandler(kBHook_KeyPress);/// kBHook.KeyDown+=new KeyEventHandler(kBHook_KeyDown); kBHook.KeyUp+=new KeyEventHandler(kBHook_KeyUp); so KeyBoardHook is another class on my project. it happens that when my program is busy doing calculations the keyboard class misses KEYUP/KEYDOWN messages. so my questions are: 1. how can i make another thread that always listen to messages? 2. can i try to hook to keyboard in a way that i will always get the messages (i need my program to get those msgs before others) Thank you,

      R.Z

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      I'm not sure about #2, but for #1, the only thing you'll most likely need is to set the apartment state of the thread to STA. You can do this using the SetApartmentState method on the Thread class.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      R 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        I'm not sure about #2, but for #1, the only thing you'll most likely need is to set the apartment state of the thread to STA. You can do this using the SetApartmentState method on the Thread class.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        R Offline
        R Offline
        ranzask
        wrote on last edited by
        #3

        Hi, thanks for quick reply. i don't start my program as a Thread t=new Thread... the program's thread starts like this (and it is STA) : [STAThread] static void Main() { Application.Run(new Form1()); } any ideas?

        R.Z

        J 1 Reply Last reply
        0
        • R ranzask

          Hi, thanks for quick reply. i don't start my program as a Thread t=new Thread... the program's thread starts like this (and it is STA) : [STAThread] static void Main() { Application.Run(new Form1()); } any ideas?

          R.Z

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Sounds like the keyboard hook class is not functional. couple things to check: if you're using native Win32 hooks, I assume you have a callback function? Well, your delegate to that function must be manually kept alive, IRRC. Aside from that, all I can suggest is narrowing the problem down: are you sure it only occurs after work is occurring? Are the events not being sent at all? what happens after the work is completed, do the hooks resume working?

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          R 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Sounds like the keyboard hook class is not functional. couple things to check: if you're using native Win32 hooks, I assume you have a callback function? Well, your delegate to that function must be manually kept alive, IRRC. Aside from that, all I can suggest is narrowing the problem down: are you sure it only occurs after work is occurring? Are the events not being sent at all? what happens after the work is completed, do the hooks resume working?

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            R Offline
            R Offline
            ranzask
            wrote on last edited by
            #5

            my keyboard class is supposed to prevent keys from being sent to the current active window. when there is no heavy work on my program it blocks all chars and Notepad do not show any chars. when i write (for testing) Thread.Sleep(1000) in the KeyDown event in the main program , Notepad then display the characters even that i blocked them (return 1 on the hook). i have seen then even if my Hook gets the message later, the Notepad already displaying the char. this is the main core of the listening: KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]),0); the SetWindowsHookEx defined like that: [DllImport("user32.dll", harSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall, etLastError=true)] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); Whats is IRRC and how do i do that? Thanks,

            R.Z

            J 1 Reply Last reply
            0
            • R ranzask

              my keyboard class is supposed to prevent keys from being sent to the current active window. when there is no heavy work on my program it blocks all chars and Notepad do not show any chars. when i write (for testing) Thread.Sleep(1000) in the KeyDown event in the main program , Notepad then display the characters even that i blocked them (return 1 on the hook). i have seen then even if my Hook gets the message later, the Notepad already displaying the char. this is the main core of the listening: KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]),0); the SetWindowsHookEx defined like that: [DllImport("user32.dll", harSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall, etLastError=true)] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); Whats is IRRC and how do i do that? Thanks,

              R.Z

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              IIRC is "if I recall correctly". :) When you pass a delegate instance in for the lpfn parameter of SetWindowsHookEx, make that delegate be a field in your class so that it doesn't get disposed; as I recall, since the object is being passed into a native method, then never used again, the garbage collector cleans up the delegate, causing your hook to fail. So one thing to check is make sure the delegate you pass into SetWindowsHookEx stays alive. Windows may have some mechanism so that if the process eating the keyboard is not responding (i.e. busy do to lots of work) it may send out the key events to other processes. Can you make your work occur on another thread, thus keeping the hook procedure thread free? That'd be ideal.

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              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