1

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":

enter image description here

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) } 
2
  • 2
    Most likely because you’re holding the alt key at the same time, Wordpad takes that into account. What if you add a delay of a second before sending keypresses and make sure the alt hasn’t opened the menus? Commented Dec 20, 2020 at 8:16
  • Tested it out and if you wil provide an answer then I will accept it. So simple and yet I did not think of this at all! :-) I think a 250ms delay would be enough for this to work. Commented Dec 20, 2020 at 8:27

1 Answer 1

3

When you use SendKeys.Send in a response to a key press then the keys you send may be combined with the physical keys you’re holding at that moment. In this case you’re holding Alt, so Wordpad assumes you pressed Alt-Ctrl-V instead of just Ctrl-V. Also Alt opens menu, so sending other keys may relate to hotkeys there.

Adding a delay will remove this issue, and usually when sending key presses it would be done as not relating to other key presses so it won’t be a problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.