尝试使用热键启动SendKeys.Keys

时间:2015-06-08 17:35:11

标签: c# autologin

我为自己的目的制作了一个小的自动登录程序。我使用按钮来执行' SendKeys.Keys(...)'。所以我基本上必须按程序按下按钮,它会登录给我。

但我认为使用热键作为按钮点击可以快得多。例如,我按下F8,它将完成与单击按钮时完全相同的事情。

    public string Path = "Login Credentials Email.txt";
    public string Path2 = "Login Credentials Username.txt";

    public Form1()
    {

        InitializeComponent();

        RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        reg.SetValue("AutoLogin", Application.ExecutablePath.ToString());

        if (!File.Exists(Path))
        {
            File.Create(Path);
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (!textBox1.Text.Contains("@"))
        {
            MessageBox.Show("This is not a valid mail address.");
            return;
        }

        try
        {
            StreamWriter sw = new StreamWriter(Path);
            sw.Write(textBox1.Text + Environment.NewLine);
            Thread.Sleep(2000);
            sw.Write(textBox2.Text);
            sw.Close();

            MessageBox.Show("Succesfully saved. You may press Start to start." + Environment.NewLine + "Once clicked the start button, the program will write the credentials in 1,5 seconds.");
        }

        catch (Exception)
        {
            MessageBox.Show("Failed to save credentials.");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {

        Thread.Sleep(1500);
        string[] lines = File.ReadAllLines(Path);
        string Email = File.ReadLines(Path).Skip(0).Take(1).First();
        string Password = File.ReadLines(Path).Skip(1).Take(1).First(); ;


        SendKeys.Send(Email);
        Thread.Sleep(500);
        SendKeys.Send("{TAB}");
        Thread.Sleep(500);
        SendKeys.Send(Password);
        Thread.Sleep(500);
        SendKeys.Send("{ENTER}");

    }


    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            StreamWriter sw = new StreamWriter(Path2);
            sw.Write(textBox3.Text + Environment.NewLine);
            Thread.Sleep(2000);
            sw.Write(textBox4.Text);
            sw.Close();

            MessageBox.Show("Succesfully saved. You may press Start to start." + Environment.NewLine + "Once clicked the start button, the program will write the credentials in 1,5 seconds.");
        }

        catch (Exception)
        {
            MessageBox.Show("Failed to save credentials.");
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Thread.Sleep(1500);
        string[] lines = File.ReadAllLines(Path);
        string Username = File.ReadLines(Path2).Skip(0).Take(1).First();
        string Password = File.ReadLines(Path2).Skip(1).Take(1).First(); ;


        SendKeys.Send(Username);
        Thread.Sleep(500);
        SendKeys.Send("{TAB}");
        Thread.Sleep(500);
        SendKeys.Send(Password);
        Thread.Sleep(500);
        SendKeys.Send("{ENTER}");
    }

}
}

0 个答案:

没有答案