Search code examples
.netdelphikeyboard-hook

Multiple keyboard hooks in .NET and Delphi


I have an application with both .NET and Delphi components I register to a keyboard hook (with SetWindowsHookEx) in both components. I first register in .NET, and later in Delphi.

The problem is, the hook delegate in Delphi is called before the hook delegate in .NET.

According to MSDN, the hook chain is just a list, and as I understand the delegates should be called according to the order of registration.

Anyone has an idea what is going on here? Thanks in advance!


Solution

  • You've misunderstood. The hook overview in MSDN describes it like this (emphasis added):

    The SetWindowsHookEx function always installs a hook procedure at the beginning of a hook chain. When an event occurs that is monitored by a particular type of hook, the system calls the procedure at the beginning of the hook chain associated with the hook. Each hook procedure in the chain determines whether to pass the event to the next procedure. A hook procedure passes an event to the next procedure by calling the CallNextHookEx function.

    Therefore, it's precisely the expected behavior if your Delphi hook is installed last and is called first. There's nothing "going on" at all.