Simon
Simon

Reputation: 467

Send clipboard text or key stroke to current active window

what I like to do: I have a program that should run in background and capture keyup and keydowns. On a specific keydown it should paste some text into the current active window.

The capturing of the keys is working but how can I paste text into the current active window? (mouse position)

The code that I have so far you can see here:

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
            }
        }

    }
}

Upvotes: 1

Views: 1959

Answers (1)

Giorgi
Giorgi

Reputation: 30873

Use the SendKeys Class to send Ctrl+V to the current window like this:

SendKeys.Send("{^}V");

Upvotes: 1

Related Questions