简体   繁体   English

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

[英]Unable to use SendKeys or SendMessage for programmatically pasting non-text objects

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. 我正在使用RegisterHotKey Win32 API来侦听Ctrl_V组合键,并使用WndProc方法来处理此热键通知。 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. 现在,即使我除了调用base.WndProc(ref mesg)之外没有在此方法中执行任何操作,粘贴操作似乎也没有传递到Windows上,因此粘贴无法正常工作。 I managed to get paste of text working by explicitly calling SendKeys("^V") but it is not working for non-text data. 我通过显式调用SendKeys(“ ^ V”)设法使文本粘贴正常工作,但不适用于非文本数据。 I also tried SendMessage Win32 API as below 我还尝试了SendMessage Win32 API,如下所示

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. 我无法弄清楚如何执行我的代码,然后让Windows对图像,文件等执行粘贴。解决此问题的任何帮助将非常及时并受到高度赞赏。

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. 另外,我现在使用Alt_Shift_V作为热键,以避免与默认的粘贴命令冲突。 So pasting non-text data works fine. 因此,粘贴非文本数据可以正常工作。 However, pasting text into Visual studio and Office applications is not working. 但是,无法将文本粘贴到Visual Studio和Office应用程序中。 SendKeys("^V") seems to be interpreted in a different way in these applications. 在这些应用程序中,SendKeys(“ ^ V”)似乎以不同的方式解释。 Any idea on how to get this working? 关于如何使它正常工作的任何想法?

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 http://www.codeproject.com/KB/cs/globalhook.aspx

This would not interfere with the pasting operation :) 这不会干扰粘贴操作:)

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. 我认为您应该截获Ctrl-V Key消息(通过WndProc),执行所需的操作,然后让base.WndProc处理该密钥消息。 You could also handle the OnKeyDown event. 您还可以处理OnKeyDown事件。 In WinForms, you can set Form.KeyPreview to true in order to see messages before child controls. 在WinForms中,可以将Form.KeyPreview设置为true,以便在子控件之前查看消息。

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). 听起来您需要一个键盘挂钩(SetWindowsHookEx API)。

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

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