是否需要为已创建的窗口使用键盘和鼠标事件? [英] Need keyboard and mouse events for already created window?

查看:91
本文介绍了是否需要为已创建的窗口使用键盘和鼠标事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我需要从现有窗口中读取事件,所以我不能只创建一个新窗口.

要读取Kbd事件,我需要使用GetRawInput,但是在使用它之前,我需要从WndProc回调中接收WM_INPUT.由于已经创建了窗口,因此无法像在对CreateWindow的调用中那样分配回调函数.那么如何获取WM_INPUT消息?

谢谢!

Hi everybody!
I need to read events from an existing window, so I cannot just create a new one.

To read Kbd events, I need to use GetRawInput, but before I can use it I need to receive WM_INPUT from the WndProc callback. Since the window has already been created I cannot assign the callback function like I could from the call to CreateWindow. So how do I get the WM_INPUT message?

Thanks!

推荐答案

如果挂钩太多(它从系统中获取所有消息,而不仅仅是从所需的窗口中获取)更合适的技术(因为第一个Petzold的时间!)是子类化:

就像这样写:

In case hooking is too much (it gets all the messages from the system, not only from the window you need) The more proper technique (since the time of the first Petzold!) is subclassing:

just write something like:

WNDPROC oldproc;
LRESULT CALLBACK yourproc(HWND h, UINT u, WPARAM w, LPARAM l)
{
   /* DO YOUR MESSAGE PROCESSING AND TEHN ... */

   return CallWindowProc(oldproc,h,u,w,l);
}



并将子类设置为



and setup the subclassing as

oldproc = (WNDPROC)SetWindowLongPtr(yourHwnd, GWL_WNDPROC, (LONG_PTR)yourproc);



然后从那里开始,yopuproc 将开始接收您的窗口消息.



and from there, yopuproc will start receiving th messages of your window.


您需要安装一个钩子. Google提供的全局键盘鼠标钩"功能,您将得到想要的东西.

有关创建CBT挂钩的一些示例代码,请看一下我写的这篇文章(几年前,但今天的核心概念仍然不变):

使用消息和简单的CBT挂钩操作Windows [
You need to install a hook. Google for "global keyboard mouse hook" and you''ll get what you want.

For some sample code on creating a CBT hook, take a look at this article I wrote (several years ago, but the core concepts remain the same today):

Manipulating Windows using messages and simple CBT hooking[^]


此功能将为您执行必要的操作: SetWindowsHookEx [
This function will do the nescessary manipulations for you: SetWindowsHookEx[^]

Hope that helps!

Cheers!


这篇关于是否需要为已创建的窗口使用键盘和鼠标事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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