简体   繁体   中英

c# SendKey to a hidden window

I have a window which is hidden and I would like to send a keypress to it. An example of what I'm trying to achieve is an app that will send the key F5 to a web browser which wasn't the active window. The web browser would know to refresh the current page when the F5 keystroke is received.

I would also like to send a combination of keys to an application, eg Ctrl+S. One example of this usage could be a timed auto-save feature to use with applications which don't have autosave. This would spare me having to remember to save every 5 mins.

C# is my technology, does this sound realistic?

这篇CodeProject文章显示了如何将击键发送到外部应用程序(使用C#源代码)。

You can use WinApi for this purpose. SendMessage or PostMessage method to send desired message to your application.

Here's C# definition of SendMessage

[DllImport("user32.dll")]
public static extern int SendMessage(
    int  hWnd,     // handle to destination window
    uint Msg,      // message
    long wParam,   // first message parameter
    long lParam    // second message parameter
  );

and define the message that you want to send like:

public const uint %WM_MESSAGE_HERE% = %value%; 

Check PInvoke - great resource containing WinApi method definitions for .NET

这应该可行,请访问www.pinvoke.net并搜索sendinput ,该页面应告诉您所有您需要了解的内容,以及如何查找窗口(查找findwindow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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