Copy and Modify selected text in different application

前端 未结 4 1926
陌清茗
陌清茗 2020-12-05 09:11

I have a windows application running at the backend. I have functions in this applications mapped to hot keys. Like if I put a message box into this function and give hot ke

4条回答
  •  盖世英雄少女心
    2020-12-05 09:39

    Your question has two answers

    How can my app set a global hotkey

    You have to call an API funcion called RegisterHotKey

    BOOL RegisterHotKey(
        HWND hWnd,         // window to receive hot-key notification
        int id,            // identifier of hot key
        UINT fsModifiers,  // key-modifier flags
        UINT vk            // virtual-key code
    );
    

    More info here: http://www.codeproject.com/KB/system/nishhotkeys01.aspx

    How to get the selected text from the foreground window

    Easiest way is to send crl-C to the window and then capture the clipboard content.

    [DllImport("User32.dll")] 
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static public extern IntPtr GetForegroundWindow();
    
    [DllImport("user32.dll")]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
    
    
    .....
    
    private void SendCtrlC(IntPtr hWnd)
        {
        uint KEYEVENTF_KEYUP = 2;
        byte VK_CONTROL = 0x11;
        SetForegroundWindow(hWnd);
        keybd_event(VK_CONTROL,0,0,0);
        keybd_event (0x43, 0, 0, 0 ); //Send the C key (43 is "C")
        keybd_event (0x43, 0, KEYEVENTF_KEYUP, 0);
        keybd_event (VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);// 'Left Control Up
    
    }
    

    Disclaimer: Code by Marcus Peters from here: http://bytes.com/forum/post1029553-5.html
    Posted here for your convenience.

提交回复
热议问题