怎么用AttachThreadInput捕捉其他进程窗口的按键消息?
我的代码:HWND hWnd = ::GetForegroundWindow();
hWnd = ::FindWindow(NULL, "无标题 - 记事本");
if (hWnd == NULL)
{
return 0;
}
HWND hEditWnd = ::FindWindowEx(hWnd, NULL, "Edit", NULL)
if(hEditWnd == NULL)
{
return 0;
}
DWORD dwPid;
dwPid = GetWindowThreadProcessId(hCodeWnd, NULL);
AttachThreadInput(GetCurrentThreadId(), dwPid, TRUE);//在这开始获取从记事本发来的案件消息 本来是要用PeekMessage呢 但是试了下不行,有知道怎么得到的吗? 系统钩子就免了 有别的办法吗?

解决方案 »

  1.   

    AttachThreadInput(GetCurrentThreadId(), dwPid, TRUE);
    的方法 我查了下百度百科
    本函数(既“连接线程输入函数”)允许线程和进程共享输入队列。连接了线程后,输入焦点、窗口激活、鼠标捕获、键盘状态以及输入队列状态都会进入共享状态。   调用这个函数时,会重设键盘状态。
    我就是想知道在记事本的 Edit里的按键状态,他不是共享消息队列吗?应该能搞到吧?
      

  2.   

    搞不到,看这段文字,虽然是1993年的,但我认为是对这个api解释最准确的
    AttachThreadInputThe AttachThreadInput function allows a thread to synchronize its input processing with another thread. Normally, windows created in different threads process input independently of each other, and they are not synchronized with the input processing of other threads. By using this function, a thread can attach its input processing to another thread. This allows a thread to share its input states and, for example, to call the SetFocus function to set the keyboard focus to a window of a different thread. In Windows 3.1, this capability is not possible.Creating a window can force an implicit AttachThreadInput when the parent windows of the window being created in the second thread were created in the first thread. When windows are created or set with a parent-child relationship between threads, the mouse and keyboard queues are attached.Because this function synchronizes input processing, you cannot use AttachThreadInput to attach to the Task Manager. The Task Manager must remain unsynchronized at all times in order to allow the user to bring an application to the foreground and to kill an application. In addition to the Task Manager, shell and system threads cannot be attached.As mentioned above, a call to AttachThreadInput allows, among other things, the sharing of key state information between two threads. It is important to note that the key state, which can be ascertained by calls to GetKeyState or GetKeyboardState, is reset after a call to AttachThreadInput. It is also important to note that AttachThreadInput does not take over the input queue from another thread. This means that the two threads still have separate input, and a call to AttachThreadInput will not force all window messages to filter into the same queue. If you need to pass all window messages from one message loop to another message loop, you will need to pass those messages on manually by sending the message yourself.Known ProblemsCurrently, if you start an application that calls AttachThreadInput to a thread in another process and reset the keyboard state via SetKeyboardState after detaching the threads, the call to SetKeyboardState will return TRUE, indicating success, but the key state is not successfully set. If the thread is in the same process, however, the call to SetKeyboardState does succeed and works as expected. This occurs because, when one thread is attached to another thread, the system creates a temporary message queue to keep a copy of the current key state information of the queue to which you are attaching. If you then set the key state, only the temporary queue key state is updated and the function call succeeds. Once you detach input processing, the temporary queue is removed and the key state change information is lost—it reverts to what it was before the attach. To work around this problem, you can either stay attached or use hooks. Microsoft is currently looking into a fix for this problem.
      

  3.   


    It is important to note that the key state, which can be ascertained by calls to GetKeyState or GetKeyboardState, is reset after a call to AttachThreadInput. It is also important to note that AttachThreadInput does not take over the input queue from another thread. This means that the two threads still have separate input, and a call to AttachThreadInput will not force all window messages to filter into the same queue. If you need to pass all window messages from one message loop to another message loop, you will need to pass those messages on manually by sending the message yourself.
      

  4.   


    到底要获取什么消息?AttachThreadInput 只能关联虚拟输入队列 也就是键盘鼠标的按键消息 干嘛要用PeekMessage?  这个函数无法关联投送消息队列 应答消息队列 还有发送消息队列  peekMessage 会把消息送到投送消息队列
      

  5.   

    SetWindowsHookEx(WH_KEYBOARD_LL)键盘钩子
      

  6.   

    楼主应该是看了 windows核心编程 所产生的误解吧,这个方法是行不通的。
      

  7.   

    一个老的API, 用来确认input queue的状态(KeyState等)的. (不能从input queue中读取)
    例如调用GetKeyState/SetKeyState等.http://stackoverflow.com/questions/1964076/how-can-i-handle-window-messages-from-a-separate-thread截获windows消息, 还是SetWindowsHookEx(推荐)
    或者自己WriteProcessMemory