自动打字与多行文本框

本文关键字:文本 | 更新日期: 2023-09-27 18:14:27

我试图创建一个自动类型,将键入消息每10秒。它应该总共发送28条消息。在MultiLine文本框中,每条消息都在不同的行上。

它的布局如下:

  • Multiline Textbox = Textbox
  • Start Button = StartBtn
  • 停止按钮= StopBtn
  • 一个间隔10500(10.5秒)的定时器= TimerAuth

我结合了一些我在网上找到的代码。这可能看起来很愚蠢。但我还是个初学者:)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AuthKeyTyper
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void label2_Click(object sender, EventArgs e)
    {
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    private void StartBtn_Click(object sender, EventArgs e)
    {
        TimerAuth.Enabled = true;
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    }
    private void StopBtn_Click(object sender, EventArgs e)
    {
        TimerAuth.Enabled = false;
    }
    private void TimerAuth_Tick(object sender, EventArgs e)
    {
        TextReader read = new System.IO.StringReader(TextBox.Text);
        int rows = 27;
        string[] text1 = new string[rows];
        for (int r = 0; r < rows; r++)
        {
            text1[r] = read.ReadLine();
        }
        SendKeys.Send("::auth " + text1[0] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[1] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[2] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[3] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[4] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[5] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[6] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[7] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[8] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[9] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[10] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[11] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[12] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[13] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[14] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[15] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[16] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[17] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[18] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[19] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[20] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[21] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[22] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[23] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[24] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[25] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[26] + "{enter}");
        TimerAuth.Enabled = false;
        TimerAuth.Enabled = true;
        SendKeys.Send("::auth " + text1[27] + "{enter}");
        TimerAuth.Enabled = false;
        MessageBox.Show("Done");
    }
}

}

自动打字与多行文本框

首先。谢谢所有的反对票…我说我是一个初学者,我对学习c#很感兴趣。经过一番挖掘和阅读,我发现了await函数。

所以我去掉了计时器,把整个功能放在start button_click事件中,并使用async。

所以这就是sendkey事件的形式:

SendKeys.Send(...)
await Task.Delay(10000)
SendKeys.Send(...)
await Task.Delay(10000)

享受吧!