简体   繁体   English

检测编辑控件焦点并插入字符

[英]Detect Edit Control Focus and Insert Characters

I am developing a virtual keyboard in C++ WinAPI. 我正在用C ++ WinAPI开发虚拟键盘。 It obviously needs to know when an Edit control has the focus so it can display the virtual keyboard (VK) window. 显然,它需要知道Edit控件何时具有焦点,以便它可以显示虚拟键盘(VK)窗口。 And when the user presses a key in the VK, the program needs to insert that character into another processes Edit control. 当用户按下VK中的一个键时,程序需要将该字符插入另一个进程的Edit控件中。 These present security concerns and may not even be possible in Windows OS. 这些存在安全隐患,甚至在Windows OS中是不可能的。 Thus my questions... 因此,我的问题...

  • Is it possible to know when an Edit control in another process has the focus? 是否可以知道另一个进程中的Edit控件何时具有焦点? I am thinking hooks could be the solution. 我认为挂钩可能是解决方案。 Using a global hook does present a security concern, is there a way I can just specifically say to the OS 'only tell me when a control of type 'Edit' has the focus'? 使用全局挂钩确实存在安全隐患,有什么办法我可以对操作系统专门说“仅告诉我类型为“编辑”的控件具有焦点时”吗? Is there another method I don't know about? 还有我不知道的另一种方法吗?

  • Is it possible to insert characters into another processes Edit control? 是否可以在其他进程的Edit控件中插入字符? This again presents security/etiquette concerns. 这再次提出了安全/礼节方面的问题。

Is it possible to know when an Edit control in another process has the focus? 是否可以知道另一个进程中的Edit控件何时具有焦点?

Yes, but not directly. 是的,但不是直接的。 You do indeed need a hook, either via SetWindowsHookEx() or SetWinEventHook() . 您确实确实需要通过SetWindowsHookEx()SetWinEventHook()进行钩子。

With SetWindowsHookEx() , in order to hook other processes, you must implement your hook in a DLL (and separate DLLs for 32bit and 64bit systems). 使用SetWindowsHookEx() ,要挂接其他进程,必须在DLL中实现挂接(对于32位和64位系统,则必须使用单独的DLL)。 You can use a WH_CBT hook looking for HCBT_SETFOCUS notifications, or a WH_CALLWNDPROC hook looking for WM_SETFOCUS / WM_KILLFOCUS window messages. 您可以使用WH_CBT钩子查找HCBT_SETFOCUS通知,也可以使用WH_CALLWNDPROC钩子查找WM_SETFOCUS / WM_KILLFOCUS窗口消息。

With SetWinEventHook() , you do not need a DLL to hook other processes. 使用SetWinEventHook() ,不需要DLL即可挂接其他进程。 You can register to receive EVENT_OBJECT_FOCUS events (I don't see a hook event for detecting loss of focus, though). 您可以注册接收EVENT_OBJECT_FOCUS事件(不过,我没有看到用于检测焦点丢失的钩子事件)。

is there a way I can just specifically say to the OS 'only tell me when a control of type 'Edit' has the focus'? 有什么办法可以我只对操作系统说“仅告诉我类型为“编辑”的控件具有焦点时”吗?

No. To filter out Edit controls specifically, your hook would need to call GetClassName() on the provided HWND to look for known Edit classes (not all Edit controls are named "EDIT" ). 不能。要专门过滤掉Edit控件,您的钩子需要在提供的HWND上调用GetClassName()来查找已知的Edit类(并非所有Edit控件都被命名为"EDIT" )。

Is it possible to insert characters into another processes Edit control? 是否可以在其他进程的Edit控件中插入字符?

Yes. 是。 You can use SendInput() or keybd_event() to post keystrokes to the same input queue that the keyboard driver itself posts to. 您可以使用SendInput()keybd_event()将击键发布到键盘驱动程序本身发布到的相同输入队列中。 As long as the Edit control remains focused, it will receive the keystrokes, as if the user had typed them normally. 只要“编辑”控件保持焦点,它就会接收到击键,就像用户正常键入它们一样。 This is the preferred approach. 这是首选方法。

However, the hooks do provide you with the HWND of the Edit control, so you could send WM_KEYDOWN / WM_CHAR / WM_KEYUP messages directly to the Edit control (however, watch out for these gotchas: You can't simulate keyboard input with PostMessage , and Simulating input via WM_CHAR messages may fake out the recipient but it won't fake out the input system ). 但是,这些钩子确实为您提供了Edit控件的HWND ,因此您可以WM_KEYDOWN / WM_CHAR / WM_KEYUP消息直接发送到Edit控件(但是,请注意这些陷阱: 您不能使用PostMessage模拟键盘输入 ,并且通过WM_CHAR消息模拟输入可能会伪造收件人,但不会伪造输入系统 )。 Alternatively, you can send WM_GETTEXT / WM_SETTEXT messages to the Edit control, or retrieve its IAccessible interface via AccessibleObjectFromWindow() , to manipulate the Edit control's text content as needed. 或者,您可以将WM_GETTEXT / WM_SETTEXT消息发送到Edit控件,或通过AccessibleObjectFromWindow()检索其IAccessible接口,以根据需要操纵Edit控件的文本内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM