1

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

    }
}
Simon
  • 377
  • 2
  • 11
  • 30

1 Answers1

1

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

SendKeys.Send("{^}V");
Giorgi
  • 30,270
  • 13
  • 89
  • 125
  • And which is a good way to get the current window? Because just using SendKeys.Send is not working for me... (remember my other program is running in the background and I need to get the current active window before I think?) **Another Point**: The SendKeys Class is fired to often and cant be used :( – Simon Jun 23 '13 at 11:09
  • @Simon I had the same problem, but SendKeys worked for me. It does send to the active window. Something you need to make sure of is that you don't have the debugger as the top window, otherwise the debugger (e.g. Visual Studio) will be the active window. – acarlon Oct 16 '13 at 08:48