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

前端 未结 2 1952
忘掉有多难
忘掉有多难 2020-12-15 13:34

I wanna send key stroke to a program even if it is running in background. But I can do this only for NOTEPAD like this,

[DllImport(\"user32.dll\")]
protecte         


        
相关标签:
2条回答
  • 2020-12-15 13:53

    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.

    0 讨论(0)
  • 2020-12-15 13:56

    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.

    0 讨论(0)
提交回复
热议问题