Windows.h - 焦点进入文本输入时的通知 [英] Windows.h - Notification when focus enters a text input

查看:36
本文介绍了Windows.h - 焦点进入文本输入时的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一种解决方案,用于在焦点进入文本字段时设置通知.最终目标是重新创建您在带有屏幕键盘的移动设备上看到的功能类型.

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.

到目前为止,我一直在探索 SetWinEventHookEVENT_OBJECT_FOCUSGetGUIThreadInfoGUI_CARETBLINKING.

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

来自文档:

EVENT_OBJECT_FOCUS

EVENT_OBJECT_FOCUS

一个对象获得了键盘焦点.系统发送此事件对于以下用户界面元素:列表视图控件、菜单栏、弹出菜单、切换窗口、选项卡控件、树视图控件和窗口对象.

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 插入符号的闪烁状态.如果插入符号可见.

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;
    CoInitialize(NULL);

    evHook = SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_END, NULL,
    handleEventObjectFocus, 0, 0,
    WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

}


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
    }


}

这在某些情况下似乎确实有效,但绝对不可靠.记事本和 IE 之类的程序似乎可以正常工作,但 Firefox 之类的其他程序则不行.这也不适用于网站上的文本字段等内容,因为它看起来不会调用 handleEventObjectFocus.

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.

有谁知道解决这个问题的另一种方法?我一直在四处寻找,似乎我可能正在 Accessibility API 中寻找一些东西,但我一直没能深入挖掘它.

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.

谢谢!

编辑

澄清一下,我希望在焦点进入任何文本字段时收到通知.这个应用程序是一个 win32 dll,永远不会有它自己的焦点.

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.

推荐答案

如果您使用的是标准 Windows 控件 WM_SETFOCUS 应该可以解决问题.无需对钩子等感兴趣

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

对于系统范围的行为,您可以查看 设置WindowsHookEx.要在系统范围内捕获事件,您需要在 DLL 中使用它.您可以使用钩子的组合,包括一个捕获 WM_SETFOCUS 的钩子.

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.

这篇关于Windows.h - 焦点进入文本输入时的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆