gpt4 book ai didi

c# - 阻止 KeyDown 事件识别 SendKeys

转载 作者:行者123 更新时间:2023-11-30 22:08:45 25 4
gpt4 key购买 nike

如果用户按下“D1 - D0”键,我想制作一个程序来模拟文本输入,如果用户按下功能键,我想模拟一些数字..

这是我试过的代码。

if (e.KeyData == Keys.D1){
SendKeys.Send("simulate this text entry");
}
if (e.KeyData == Keys.F12){
SendKeys.Send("123");
}

但问题是当我按下 F12 按钮时,KeyDown 事件将第一个“1”识别为“D1”键并发送“123”和“模拟此文本输入”

我怎样才能避免这个问题?

最佳答案

当您发送 key 时,您必须设置一个标志,以便您知道忽略正在发送的字符。此外,您需要知道何时重新打开标志。这可以通过将变量设置为发送值的长度,然后用每个检测到的键递增计数器来完成。这并非万无一失,因为用户可能会快速连续敲击按键(或按住某个键),而您不知道按键是用户交互的结果还是您的模拟结果。

基本思路是:

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private bool IgnoreKeys = false;
private int target = 0;
private int counter = 0;

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (!IgnoreKeys)
{
if (e.KeyData == Keys.D1)
{
Send("simulate this text entry");
}
if (e.KeyData == Keys.F12)
{
Send("123");
}
}
else
{
counter++;
if (counter == target)
{
IgnoreKeys = false;
}
}
}

private void Send(string entry)
{
IgnoreKeys = true;
counter = 0;
target = entry.Length;
SendKeys.Send(entry);
}

}

关于c# - 阻止 KeyDown 事件识别 SendKeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22069669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com