Send Keystrokes to a program even if its in background using c#

六月ゝ 毕业季﹏ 提交于 2019-11-29 00:25:41

You may be able to figure out the lpszClass of the program using an inspection tool such as WinSpy++. It gives you a crosshair that you can drag and position over the desired control. This was able to easily provide me with the "edit" class name for notepad.

If things aren't working, click the "More>>" button in the lower right of WinSpy++, then click the "Locate" button to view the control hierarchy; you may need to post the WM_KEYDOWN message to one of the parent or child controls instead.

I think you'll need to have the background program install a low-level keyboard hook via the win32 function SetWindowsHookEx().

Here's the MSDN documentation for SetWindowsHookEX()

http://msdn.microsoft.com/en-us/library/ms644990(v=vs.85).aspx

And here's the KB article on how to do it from C#

http://support.microsoft.com/kb/318804

This article goes into some detail, too: http://www.codeguru.com/columns/vb/article.php/c4829

I expect your app will get caught by various spyware/anti-virus software as a keyboard logger, though.

Good luck.

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