0

I have the following code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

    class Program
    {
        static string originalClipboardContent;
    
        static KeyboardHook hook = new KeyboardHook();
    
        [DllImport("user32.dll")]
        public static extern bool OpenClipboard(IntPtr hWndNewOwner);
    
        [DllImport("user32.dll")]
        public static extern bool CloseClipboard();
    
        [DllImport("user32.dll")]
        public static extern bool EmptyClipboard();
    
        [DllImport("user32.dll")]
        public static extern IntPtr SetClipboardData(uint uFormat, IntPtr data);
    
        [DllImport("user32.dll")]
        public static extern IntPtr GetClipboardData(uint uFormat);
    
        const int KEYEVENTF_EXTENDEDKEY = 0x1;
        const int KEYEVENTF_KEYUP = 0x2;
    
        [STAThread]
        static void Main()
        {
            hook.KeyPressed += hotkey_Pressed;
            // Do not delve into the implementation of KeyBoardHook class, the event is called by pressing the keys and the handler method is called
            hook.RegisterHotKey(ModifierKeys.Control | ModifierKeys.Alt,
                Keys.Up);
            NotifyIcon icon = new NotifyIcon();
            icon.Icon = SystemIcons.Application;
            icon.Text = "Clipboard Manager";
            icon.Visible = true;
    
            Application.Run();
            icon.Dispose();
        }
    
        static void GetClipBoardText(out string strs)
        {
            OpenClipboard(IntPtr.Zero);
            strs = Marshal.PtrToStringUni(GetClipboardData(13));
            CloseClipboard();
        }
    
        static void SetClipBoardText(string s)
        {
            OpenClipboard(IntPtr.Zero);
    
            EmptyClipboard();
    
            IntPtr textPtr = Marshal.StringToHGlobalUni(s);
    
            SetClipboardData(13, textPtr);
    
            CloseClipboard();
        }
        static void hotkey_Pressed(object sender, EventArgs e)
        {
            GetClipBoardText(out originalClipboardContent);
    
            SendKeys.SendWait("^(INS)");
            string selected;
            GetClipBoardText(out selected);
            
            using (StreamWriter writer = new StreamWriter("selected_text.txt"))
            {
                writer.WriteLine(selected);
            }
    
            SetClipBoardText(originalClipboardContent);
        }
    }

When I press ctrl+alt+up, the method that handles this hotkey, to my surprise, does not call Ctrl+Insert and the logic I need does not work. I have tried various techniques including keybd_event from user32.dll , ctrl+c instead of ctrl+insert but none of that helped. What is the problem? Is it related to window protection?:

pijio
  • 41
  • 5
  • You meant to say that hotkey_Pressed isn't called? You may want to elaborate on the KeyboardHook class then. I'm not aware that that this is Standard Framework stuff. And if that is the problem remove the stuff fiddling with the clipboard as they are not part of that problem. That might be also a problem but then a problem for a different question. – Ralf Jan 18 '23 at 11:03
  • Not really. hotkey_Pressed is called, but keystrokes CTRL+INSERT inside this method do not occur (as it seems to me). – pijio Jan 18 '23 at 11:09
  • As you may have noticed, I want CTRL+INSERT to be called after pressing hotkey, and after calling CTRL+INSERT, I want to get what was copied and write to a file, but this does not happen, the method simply copies what was in the buffer before calling CTRL+INSERT and put it in file – pijio Jan 18 '23 at 11:10
  • Keyboard input goes to the foreground thread. Make sure that is the thread you expect it to be. That said, the effects of faking input is affected by factors that are generally outside your control, and solutions based on this will be brittle. If you need to read the selected text of another program, use [UI Automation](https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32) instead. No more mucking with the clipboard either. – IInspectable Jan 18 '23 at 11:35
  • CTRL+INSERT Keystrokes were sent but it doesn't function the same as what you wanted. See [the question](https://stackoverflow.com/questions/75061028/windows-low-level-keyboard-hook-allow-win-h-not-other-win-hotkeys) for a similar scene. Use *UI Automation* instead as @IInspectable said. – YangXiaoPo-MSFT Feb 01 '23 at 08:04

0 Answers0