How to inject text to the cursor focus

╄→гoц情女王★ 提交于 2020-01-25 08:49:05

问题


I am developing a .NET windows app that needs to insert text in the place where the cursor is. The cursor will be in a different application that I have no control over. I think the operating system needs to be used here to achieve this. Can you help please?


回答1:


Put the text you want to insert into the clipboard

Find the window's handle:

Process[] processes = Process.GetProcessesByName("notepad");
foreach (Process p in processes)
{
    IntPtr pFoundWindow = p.MainWindowHandle;
    // Do something with the handle...
}

Send a "Ctrl+V" message to the window using pinvoke, see http://www.pinvoke.net/default.aspx/user32.SendMessage




回答2:


The most easy is to use the SendKeys class on Windows.Forms.

Otherwise use the SendMessage from the windows api to send key by key (some information on this).

Do not use the clipboard, the user typically does not want that, and does not expect that.



来源:https://stackoverflow.com/questions/450491/how-to-inject-text-to-the-cursor-focus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!