R – How to hook hotkeys in winforms or WPF from a .NET 2.0 assembly


I have assembly that targets .NET 2.0 to be compatible with a broad range of applications. When used with a desktop application (either winforms or WPF) I want to hook certain hotkeys and popup modeless dialog windows. The tricky bit is finding a solution that can be implemented under .NET 2.0 that is compatible with WPF.

For winforms I could use RegisterHotKey or implement IMessageFilter and hook WM_KEYDOWN in the PreFilterMessage method, but they don't work well with WPF.

For WPF, the InputBinding class is very nice, but this isn't available in .NET 2.0.

An ideal solution would:

  • Build in an assembly targeting .NET

  • Hook keystrokes for either WPF or Winforms

  • Provide application-wide, not system-wide scope

Best Solution

You could build a universal solution based on Windows API (System wide hook). Don Esposito has written a great article on that. Here's a link


You could write an application hook too if you don't want a system wide hook. That's actually easier to implement. Don covers that too I think.

Related Question