-1

I'm trying to do a Form using VS2017 and C# , it will be for personal use.

I want that when I press "F2" key in other application (like Word, Excel, Notepad) a text that was previously determined in a TextBox into Visual Studio automatically appears.

For example, If the form has a TextBox1 with a text that says "Hello" I want that I press F2 in Word and the text "Hello" appears.

By the moment I have this, but I think is wrong

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.F2)
        {
        SendKeys.Send(textBox1.Text);
        }
    }
  }
}

If you can help me with this, I'll be glad! :)

Thank you!

1 Answers1

0

When you are creating an event handler in your form such as Form1_KeyDown you're listening for events happening on that form and nowhere else. If you want to be informed about a key happening outside of your application you won't be able to do it by using the form handlers at all and will have to do it via other APIs.

The specific feature you want isn't, to the best of my knowledge, exposed directly in .net, this means you will need to interop with the windows API to do what you want

This question and it's answer should show you how to do it : Capture a keyboard keypress in the background

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78