0

Is there a simple way to create a tool which works in the background and simulates the CTRL+V key?

For example: Whenever I press the F1 button, windows pastes the copied text in an active textbox.

PS: sorry for STRG :( its the german version of CTRL


Ok Part II:

This way works for me:

private void Form1_Load(object sender, EventArgs e)
    {
        this.KeyPreview = true;
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(KeyEvent);
    }

    private void KeyEvent(object sender, KeyEventArgs e) //Keyup Event 
    {
        if (e.KeyCode == Keys.F4)
        {
            SendKeys.Send("^(V)");
        }
    }

But it only works within my application.I also wan't my program to do this in other applications. For example: when i press [WindowsKey] + [R], the "proceed window" opens, if i klick f4 then my program should simulate an [CTRL] + [V] and paste the copied text. Any help?


SOLUTION:

I finally got it, the way it works for me:

using Utilities;

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

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        gkh.HookedKeys.Add(Keys.F4);
        gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
    }

    void gkh_KeyDown(object sender, KeyEventArgs e)
    {
        SendKeys.Send("^(V)");
        e.Handled = true;
    }

    private void label1_Click(object sender, EventArgs e)
    {

    } 


  }
}

This link is neccessary: A Simple C# Global Low Level Keyboard Hook

  • Do you want to simulate CTRL+V in your application or in every opened application? – Otiel Nov 08 '11 at 11:11
  • For future readers: A SendKeys test I did on computers with Windows 7 crashed on 2/4 machines without any prompt or debug infos. Use SendInput, SendMessage, PostMessage, the InputSimulator library, anything but SendKeys. – Léon Pelletier May 12 '12 at 06:20

2 Answers2

0

I have created once something similar. I was attaching a OnDataReceivedHandler to read a input from serial port (reading a EAN code and write it to focused textbox). Your goal is to get event, when user press F1. Then you just call:

SendKeys.Send(data);

SendKeys si a part of Forms, so dont forget for using System.Windows.Forms;

sasjaq
  • 761
  • 1
  • 11
  • 31
0

One option would be to use InputSimulator. It wraps SendInput under the hood but abstracts away all the PInvoke calls and other complexity. It's a drop in DLL that (for your situation) should only a single line of code.

InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C); 

The next step (depending on what you are looking for - global to windows versus active to the form only) would be to register a "hotkey" for F1.

I recommend checking out some of the answers for this question for a global hot-key: Best way to tackle global hotkey processing in c#? or this CodeProject article: A Simple C# Global Low Level Keyboard Hook

If you are looking for a hotkey that is active on the current form you can use:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.F1)
    {
       // Send Ctrl + V
    }
}
Community
  • 1
  • 1
robowahoo
  • 1,259
  • 9
  • 10