如何为任何 运行 控制台进程捕获特定的键盘序列?

How to trap specific keyboard sequence for any running console process?

我的目标是捕获 Ctrl+Shift+key 序列(其中 key 将由我的应用程序定义)当任何 运行 控制台进程具有键盘焦点时。然后在这样的击键后,我需要将一些特定的文本插入到该控制台应用程序中。

问题是:一旦注入目标控制台进程,我该如何拦截它的键盘击键?

I was hoping that my test dll would be injected and called from within the process that was receiving the keyboard input at the time, but unfortunately it wasn't. It was always called from within my own process that called SetWindowsHookEx() to install the hook.

该警告在文档中有明确说明:

KeyboardProc

This hook may be called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

LowLevelKeyboardProc

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

并且由于两个挂钩都没有提供进程信息,因此它们无法帮助您了解哪个进程将接收击键。您可以在这些挂钩中做的最接近的事情是使用 GetForegroundWindow()GetWindowThreadProcessId(),但这并不能保证您确定的进程 ID 与实际接收击键的进程 ID 相同。

Then my thinking was to install WH_CBT global hook instead and trap instances when nCode == HCBT_SETFOCUS for when any window will receive keyboard focus. Then I will somehow need to establish that that window belongs to a console app by its HWND. And then use Dll Insjection method to run my code in its process

给定一个 HWND,您可以使用 GetWindowThreadProcessId() 确定它的线程 ID 和进程 ID,并通过调用 GetClassName() 确定它是否是一个控制台 window 并查看它是否是 "ConsoleWindowClass".

但是,这只能告诉您 window 本身是否是控制台 window。它不会告诉您哪个进程当前正在 window 中显示 UI。可能是:

  1. cmd.exe 本身(或在 %COMSPEC% 环境变量中指定的任何命令处理器)。

  2. cmd.exe 启动的控制台应用程序,因此与属于 cmd.exe 的控制台 window 交互。

  3. 直接启动的控制台应用程序,因此具有由 OS 为其创建的专用控制台 window。

  4. 通过 AllocConsole().

  5. 创建自己的控制台 window 的非控制台进程
  6. 通过 AttachConsole().

  7. 与另一个进程的控制台 window 关联的非控制台进程

不过我想这并不重要。如果控制台 window 属于 cmd.exe,它将首先接收击键,然后将它们委托给当前显示在 window 中的任何子进程。如果它是专用于另一个进程的控制台,则该进程将直接接收击键。因此,无论哪种方式,您都应该能够挂钩实际创建控制台 HWND 的进程 ID,因为这应该是第一个看到击键的进程。

The question I have though is once injected into a target console process how do I intercept its keyboard keystrokes?

您仍然可以像以前一样使用键盘钩子。您可以创建与特定线程 ID 关联的键盘挂钩,因此使用创建控制台 window 的线程 ID,因为这是唯一可以直接与 window 交互并接收输入的线程消息。只需确保安装键盘钩子的线程有一个消息循环。您是将该线程远程注入控制台进程,还是将其留在您自己的进程中,这取决于您。您应该能够以任何方式接收键盘事件。