【问题标题】:Use F2 to write a text previously determined in a TextBox使用 F2 在 TextBox 中写入先前确定的文本
【发布时间】:2018-10-26 00:18:12
【问题描述】:

我正在尝试使用 VS2017 和 C# 做一个表单,它将供个人使用。

我希望当我在其他应用程序(如 Word、Excel、记事本)中按“F2”键时,会自动显示之前在 TextBox 中确定到 Visual Studio 中的文本。

例如,如果表单有一个 TextBox1,其中包含“Hello”的文本,我希望在 Word 中按 F2 并显示文本“Hello”。

目前我有这个,但我认为是错误的

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

如果你能帮我解决这个问题,我会很高兴的! :)

谢谢!

【问题讨论】:

    标签: c# windows winforms visual-studio-2017


    【解决方案1】:

    当您在表单中创建事件处理程序(例如 Form1_KeyDown)时,您正在侦听该表单上发生的事件,而不是其他任何地方。如果您想知道在您的应用程序之外发生的密钥,您将无法通过使用表单处理程序来完成,而必须通过其他 API 来完成。

    据我所知,您想要的特定功能不是直接在 .net 中公开的,这意味着您需要与 Windows API 互操作才能完成您想要的操作

    这个问题和它的答案应该告诉你怎么做:Capture a keyboard keypress in the background

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多