Herramienta de fondo para copiar y pegar

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?


SOLUCIÓN:

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: Un simple gancho de teclado global de bajo nivel en C #

preguntado el 08 de noviembre de 11 a las 10:11

Do you want to simulate CTRL+V in your application or in every opened application? -

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

2 Respuestas

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;

respondido 08 nov., 11:16

Una opción sería utilizar 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: ¿La mejor manera de abordar el procesamiento global de teclas de acceso rápido en c #? or this CodeProject article: Un simple gancho de teclado global de bajo nivel en C #

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

contestado el 23 de mayo de 17 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.