tags:

views:

51

answers:

2

In .Net there is a feature called KeyPreview. How can I do this in WinApi. Maybe I don't necessarily need this but my hWnd has WM_KEYDOWN, but it does not receive it when my Text Box has focus. How can I achieve this? Thanks

*Using pure Win32API...

is there an alternative, how could I handle the Text Box's WM_KEYDOWN?

A: 
  1. If you use MFC you can look at PreTranslateMessage (I am not sure regarding to PreTranslateMessage, but you can easily to verify whether to enough to override the function).
  2. You can set keyboard hook.
VitalyVal
Please, not keyboard hook. Filtering messages in the message dispatch loop is a reasonable option but only if no modal message loop is active.
Ben Voigt
A: 

You can try subclassing the edit control. Either "instance subclassing", to trap messages for only one window, or "global subclassing" to trap messages for all windows of that class (in your application, not system-wide).

The example here (http://msdn.microsoft.com/en-us/library/ms997565.aspx) shows how to subclass an edit control and trap the WM_GETDLGCODE message -it wouldn't be that difficult to change it to WM_KEYDOWN.

You may have problems previewing the keys used for dialog commands, e.g. TAB or RETURN, as they may be trapped higher up the food chain. You may need to look at changing WM_GETDLGCODE as well.

Pat Wallace