Take the 2-minute tour ×

I'm trying to come up with a solution for setting up a notification when focus enters a text field. The end goal in mind is to recreate the type of functionality you see on mobile devices with on screen keyboards.

So far I've been exploring SetWinEventHook with EVENT_OBJECT_FOCUS and GetGUIThreadInfo with GUI_CARETBLINKING.

From the docs:


An object has received the keyboard focus. The system sends this event for the following user interface elements: list-view control, menu bar, pop-up menu, switch window, tab control, tree view control, and window object.

GUI_CARETBLINKING The caret's blink state. This bit is set if the caret is visible.

Using these methods I've come up with this solution:

void TextInputHelper::setupEventHook(FREContext iCtx)
    ctx = iCtx;

    handleEventObjectFocus, 0, 0,


void CALLBACK handleEventObjectFocus(HWINEVENTHOOK hook, DWORD evt, HWND hwnd,
                                 LONG idObj, LONG idChild,    DWORD thread, DWORD time)
    GUITHREADINFO threadInfo;
    threadInfo.cbSize = sizeof(GUITHREADINFO);

    BOOL result = GetGUIThreadInfo(thread, &threadInfo);

    if(threadInfo.flags & GUI_CARETBLINKING)

        //text field focus


This does seem to work in some cases but its definitely not reliable. Programs like Notepad an IE seem to work fine but others like Firefox do not. This also will not work for things like text fields on websites because it doesn't look like handleEventObjectFocus will get called.

Does anyone know of another way to approach this problem? I've been searching around and it seems like I might be looking for something in the Accessibility APIs but I haven't been able to dig up to much on it.



To clarify, I'm looking to receive a notification when focus enters any text field. This application is a win32 dll and will never have focus its self.

share|improve this question
The solution you implemented is about as reliable as it gets. It works well for Windows controls, but will not for any custom control implementation. Firefox, for example, employs Qt for its GUI which doesn't use Windows controls at all, but rather implements all the logic and rendering itself. –  IInspectable Jan 19 '13 at 10:44

3 Answers 3

up vote 1 down vote accepted

If you're using standard Windows controls WM_SETFOCUS should do the trick. No need to get fancy with hooking etc.

EDIT: For system wide behavior you can check out SetWindowsHookEx. To catch events system-wide you need to use it from within a DLL. You can use a combination of hooks including one that catches WM_SETFOCUS.

share|improve this answer
-1 This works if you control the source code of the application you are going to modify. This question is about augmenting text input for existing applications. –  IInspectable Jan 18 '13 at 8:31
Sorry but then you need to make that more clear in the initial question, see edit. –  demorge Jan 18 '13 at 10:55
thanks @demorge ,Tim is right I'm looking for a global os hook, I tried to make that more clear in the original question. I will read up on this after work. Do you happen to have any examples? I don't see any focus params in the SetWinHookEx docs –  f-a Jan 18 '13 at 13:26
@demorge: I don't see how SetWindowsHookEx is any more capable than SetWinEventHook as a solution to processing WM_SETFOCUS messages in another process. Since you still don't address the question I'm afraid I have to leave the downvote. –  IInspectable Jan 19 '13 at 10:48

If you're trying to supply an alternative text input method, you should look into "IME" - "Input Method Editor". These are directly supported by the OS.

share|improve this answer
For an example see Using an Input Method Editor in a Game. –  IInspectable Jan 19 '13 at 10:58
Thanks, I'll look into this. However I'm not working with input. I'm trying to notify a remote client that a text input now has focus. –  f-a Jan 21 '13 at 3:30

You can catch the entrance into a text field using the EN_SETFOCUS notification.

WM_FOCUS is for the window itself, not for controls that are in it. Otherwise said, if you want to use WM_FOCUS, you'll have to subclass your EDIT field. It's not necessary here.

EDIT: it wasn't completely clear that you wanted a system-wide behavior. IN that case you have to use an hook (cf. SetWindowsHookEx) as explained in the answer above. Sorry.

share|improve this answer
-1 Same applies here, this works if you control the source code of the application you are going to modify. This is not the case here, so the answer is not. –  IInspectable Jan 18 '13 at 8:32

Your Answer


