无法使用 SendKeys 或 SendMessage 以编程方式粘贴非文本对象

发布于 2024-08-23 01:07:32 字数 604 浏览 2 评论 0原文

我正在使用 RegisterHotKey Win32 API 来侦听 Ctrl_V 组合键,并使用 WndProc 方法来处理此热键通知。现在,即使我除了调用 base.WndProc(ref mesg) 之外不在此方法中执行任何操作,粘贴操作似乎也不会传递到 Windows,因此粘贴不起作用。我设法通过显式调用 SendKeys("^V") 来粘贴文本,但它不适用于非文本数据。我还尝试了 SendMessage Win32 API,如下所示

SendMessage(foregroundWindowHandle, 0x302, 0, 0);

,但即使这样也不起作用。

我无法弄清楚如何执行我的代码,然后让 Windows 执行图像、文件等的粘贴。解决此问题的任何帮助都将非常及时并受到高度赞赏。

更新:我发现问题是生成粘贴命令的窗口没有重新获得焦点。更正此问题后,粘贴在记事本中工作正常。另外,我现在使用 Alt_Shift_V 作为热键,以避免与默认粘贴命令发生冲突。因此粘贴非文本数据效果很好。但是,将文本粘贴到 Visual Studio 和 Office 应用程序中不起作用。 SendKeys("^V") 在这些应用程序中似乎以不同的方式解释。关于如何让它发挥作用有什么想法吗?

I am using the RegisterHotKey Win32 API to listen to the Ctrl_V key combination and using the WndProc method to handle this hot key notification. Now, even if I don't perform any operations in this method apart from calling base.WndProc(ref mesg), the Paste operation doesnt seem to be getting passed onto Windows and hence, paste is not working. I managed to get paste of text working by explicitly calling SendKeys("^V") but it is not working for non-text data. I also tried SendMessage Win32 API as below

SendMessage(foregroundWindowHandle, 0x302, 0, 0);

but even this is not working.

I am unable to figure out how to execute my code and then let Windows perform the paste for images, files etc. Any help in resolving this will be very timely and highly appreciated.

UPDATE: I figured out the problem was that the window where the Paste command was being generated wasnt getting the focus back. After correcting this, Paste is working fine for Notepad. Also, I am using Alt_Shift_V as the hot key now to avoid clashing with the default paste command. So pasting non-text data works fine. However, pasting text into Visual studio and Office applications is not working. SendKeys("^V") seems to be interpreted in a different way in these applications. Any idea on how to get this working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗心人 2024-08-30 01:07:32

不注册热键,而是注册全局挂钩。

我已经使用全局钩子做了一些与您过去所做的类似的事情,并且效果很好。

简单方便的全局钩子实现的代码可以在以下位置找到:

http://www. codeproject.com/KB/cs/globalhook.aspx

这不会干扰粘贴操作:)

Instead of registering a hotkey, register a global hook.

I have used global hooks to do something similar to what you are doing in the past and it works quite well.

Code for a simple and handy global hook implementation can be found at:

http://www.codeproject.com/KB/cs/globalhook.aspx

This would not interfere with the pasting operation :)

不念旧人 2024-08-30 01:07:32

我认为你应该拦截 Ctrl-V 键消息(通过 WndProc),做你需要的事情,然后让 base.WndProc 处理键消息。您还可以处理 OnKeyDown 事件。
在 WinForms 中,您可以将 Form.KeyPreview 设置为 true,以便在子控件之前查看消息。

I think you should intercept the Ctrl-V Key message (through WndProc), do what you need and then let the base.WndProc handle the key message. You could also handle the OnKeyDown event.
In WinForms, you can set Form.KeyPreview to true in order to see messages before child controls.

赠我空喜 2024-08-30 01:07:32

如果您只想采取一些操作然后传递消息,那么注册热键并不是解决方案。听起来您需要一个键盘挂钩(SetWindowsHookEx API)。

Registering a hotkey isn't the solution if you just want to take some action and then pass on the message. It sounds like you'd need a keyboard hook instead (the SetWindowsHookEx API).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文