1

我喜欢做什么: 我有一个程序应该在后台运行并捕获 keyup 和 keydowns。在特定的按键下,它应该将一些文本粘贴到当前活动窗口中。

键的捕获正在工作,但如何将文本粘贴到当前活动窗口中?(鼠标位置)

我到目前为止的代码你可以在这里看到:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Utilities;

namespace Developper_Dashboard
{
    public partial class Form1 : Form
    {
        globalKeyboardHook gkh = new globalKeyboardHook();

        private bool IsADown = false;
        private bool IsBDown = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Opacity = 0;

            gkh.HookedKeys.Add(Keys.LControlKey);
            gkh.HookedKeys.Add(Keys.LShiftKey);
            gkh.HookedKeys.Add(Keys.Q);
            gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
            gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
        }

        void gkh_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Control)
            {
                IsADown = false;
            }
            if (e.KeyCode == Keys.LShiftKey)
            {
                IsBDown = false;
            }
            if (!IsADown | !IsBDown)
            {
                this.Opacity = 0;
            }
            //e.Handled = true;
        }

        void gkh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.LControlKey)
            {
                IsADown = true;
            }
            if (e.KeyCode == Keys.LShiftKey)
            {
                IsBDown = true;
            }
            if (IsADown && IsBDown)
            {
                this.Opacity = 1;
            }
            if (IsADown && IsBDown && e.KeyCode == Keys.Q)
            {
                //Send Clipboard to current active window
            }
        }

    }
}
4

1 回答 1

1

使用SendKeys 类将 Ctrl+V 发送到当前窗口,如下所示:

SendKeys.Send("{^}V");
于 2013-06-23T11:03:15.940 回答