I have a problem using Sendkeys.Send in my C# application and I really cannot understand why. When using it then it does not send what I expect to the active application. I am using it together with the global hotkey manager, https://github.com/thomaslevesque/NHotkey
I have created this simple PoC that, for my part at least, will be able to reproduce my problem. Just launch Wordpad and press the hotkey, ALT + O:
using System.Windows.Forms; using System.Diagnostics; using NHotkey.WindowsForms; namespace WindowsFormsApp5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Convert string to keys string hotkey = "Alt + O"; KeysConverter cvt; Keys key; cvt = new KeysConverter(); key = (Keys)cvt.ConvertFrom(hotkey); // Setup the hotkey HotkeyManager.Current.AddOrReplace("MyID", key, HotkeyAction); // Copy some text to the clipboard that I want to paste to the active application Clipboard.SetText("My String"); } private void HotkeyAction(object sender, NHotkey.HotkeyEventArgs e) { Debug.WriteLine("Pressed the hotkey"); SendKeys.Send("^v"); // SendKeys.Send("Test string"); e.Handled = true; } } } When I do this in Wordpad, then instead of pasting the clipboard (^v equals CTRL + V) then it tries to "Paste Special":
Even if I do the most simple thing and then just put some text in SendKeys.Send, then it seems to be messing with the menus in Wordpad? SendKeys.SendWait is not any different.
I have been trying to figure this out for quite some time now but I simply do not understand why it does that. Basically, I need to paste the clipboard on a hotkey though it doesn't need to be with this exact method so if anyone knows another way of doing it then I would appreciate some hints.
MY IMPLEMENTED SOLUTION
Based on the accepted answer then I did change my implementation slightly as I could not get it working with just a timer. I may have missed something(?) but this is working.
In basic then I change focus to my application as soon as the hotkey is detected, to avoid conflict with modifier keys (ALT etc) in the active application. I then create an invisible form and when I detect a KeyUp event, then I check for modifier keys and if none is pressed down then I enable a timer and immediately switch focus back to the originating application. After 50ms the clipboard will be pasted to the active application.
Something like this:
// Somewhere else in code but nice to know // IntPtr activeApp = GetForegroundWindow(); // get HWnd for active application // SetForegroundWindow(this.Handle); // switch to my application private System.Timers.Timer timerPasteOnHotkey = new System.Timers.Timer(); // Main public PasteOnHotkey() { InitializeComponent(); // Define the timer timerPasteOnHotkey.Elapsed += new ElapsedEventHandler(OnTimedEvent); timerPasteOnHotkey.Interval = 50; timerPasteOnHotkey.Enabled = false; // Make the form invisble this.Size = new System.Drawing.Size(0, 0); this.Opacity = 0.0; } private void PasteOnHotkey_KeyUp(object sender, KeyEventArgs e) { // Check if modifier keys are pressed bool isShift = e.Shift; bool isAlt = e.Alt; bool isControl = e.Control; // Proceed if no modifier keys are pressed down if (!isShift && !isAlt && !isControl) { Hide(); // Start the timer and change focus to the active application timerPasteOnHotkey.Enabled = true; SetForegroundWindow(activeApp); } } private void OnTimedEvent(object source, ElapsedEventArgs e) { timerPasteOnHotkey.Enabled = false; SendKeys.SendWait("^v"); // send "CTRL + v" (paste from clipboard) } 