c#中類比鍵盤)

來源:互聯網
上載者:User
【全文】
      前一段時間瘋狂的迷上了魔獸世界,為之瘋狂,但是9c的5區,讓多少玩家鬱悶啊,守護之劍每天排隊800+,還不停地卡機,掉線,本人上班族,每天晚上6點下班回家排隊,上線也都8點多了,MC啊,黑E啊,AQL啊等活動早開始了,十分的鬱悶!

     為了按時玩遊戲,本著將魔獸進行到底的原則,決定開發一個魔獸的自動登陸器,以解決燃眉之急,哈哈!

     首先定義一個類ViaStruct,用來儲存遊戲的路徑,等待時間,以及使用者名稱,密碼!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace KeyBoardInput
{
    [Serializable]
    public class ViaStruct
    {
        private string timer = "";
        private string filepath = "";
        private string username = "";
        private string pwd = "";
        private string timer2 = "";
        public bool SuccessFlag = false;
        public ViaStruct()
        {
            SuccessFlag=ReadFormKBI();
        }

        #region 屬性
        public string Timer
        {
            get { return timer; }
            set { timer = value; }
        }
        public string FilePath
        {
            get { return filepath; }
            set { filepath = value; }
        }
        public string UserName
        {
            get { return username; }
            set { username = value; }
        }
        public string Pwd
        {
            get { return pwd; }
            set { pwd = value; }
        }
        public string Timer2
        {
            get { return timer2; }
            set { timer2 = value; }
        }
        #endregion

        private bool ReadFormKBI()
        {
            StreamReader sr = new StreamReader("info.txt");
            if ((this.filepath = sr.ReadLine()) == null || this.filepath == "")
            {
                return false;
            }
            this.timer = sr.ReadLine();
            this.timer2 = sr.ReadLine();
            this.username = sr.ReadLine();
            this.pwd = sr.ReadLine();
            sr.Close();
            sr.Dispose();
            GC.Collect();
            return true;
        }

        public void WriteToKBI()
        {
            Thread th = new Thread(new ThreadStart(NewMethod));
            th.Start();
        }

        private void NewMethod()
        {
            try
            {
               
                StreamWriter sw = new StreamWriter("info.txt");
                sw.WriteLine(this.filepath);
                sw.Flush();
                sw.WriteLine(this.timer);
                sw.Flush();
                sw.WriteLine(this.timer2);
                sw.Flush();
                sw.WriteLine(this.username);
                sw.Flush();
                sw.WriteLine(this.pwd);
                sw.Flush();
             
                sw.Close();

                MessageBox.Show("寫入成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

 

然後編寫Form1視窗

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace KeyBoardInput
{
   

    public partial class Form1 : Form
    {
        ViaStruct via;
        Thread th;
        Thread ti;
        public Form1()
        {
            InitializeComponent();
            via = new ViaStruct();
        }

      

        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = via.UserName;
            this.textBox2.Text = via.Pwd;
            this.textBox3.Text = via.FilePath;
            this.textBox4.Text = via.Timer2;
            this.comboBox1.Text = via.Timer;
            th = new Thread(new ThreadStart(NewMethod));
            th.Start();
           
          
        }

        private void NewMethod()
        {
            if (via.SuccessFlag)
            {
                try
                {
                    Thread.Sleep(Convert.ToInt32((Convert.ToDecimal(via.Timer) * 60 * 1000)));//開起程式後等待
                                                                                                                                                          //via.Timer時間內啟動程式
                    Process.Start(via.FilePath);
                    Thread.Sleep(Convert.ToInt32((Convert.ToDecimal(via.Timer2) * 60 * 1000)));//啟動程式後等待                                                                                                                                      //via.Timer2時間輸入使用者名稱密碼
                }
                catch (Exception ex)
                {
          
                  if(ex.GetType().ToString()!="System.Threading.ThreadAbortException")
                    MessageBox.Show(ex.Message+"1");
                }               
            }
            if (via.SuccessFlag)
            {
                MySendKeys();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.RestoreDirectory = true;
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox3.Text = this.openFileDialog1.FileName;
                      
            }
           // MessageBox.Show(Environment.CurrentDirectory);
           
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void Form1_MinimumSizeChanged(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void MySendKeys()//輸入使用者名稱密碼
        {
           
            foreach (char ArrayValue in via.UserName.ToCharArray())
            {
                SendKeys.SendWait(ArrayValue.ToString());
                Thread.Sleep(10);
            }
            SendKeys.SendWait("{Tab}");
            foreach (char ArrayValue in via.Pwd.ToCharArray())
            {
                SendKeys.SendWait(ArrayValue.ToString());
                Thread.Sleep(10);
            }
            SendKeys.SendWait("{Enter}");
          
        }

        private void button2_Click(object sender, EventArgs e)//給Via對象賦值
        {
            via.Timer = this.comboBox1.Text;
            via.Timer2 = this.textBox4.Text;
            via.FilePath = this.textBox3.Text;        
            via.UserName = this.textBox1.Text;
            via.Pwd = this.textBox2.Text;
            via.WriteToKBI();          
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)//關閉視窗後推出線程
        {
            if (th.ThreadState != System.Threading.ThreadState.Aborted)
                th.Abort();
            if (ti!=null&&ti.ThreadState !=System.Threading.ThreadState.Aborted)
                ti.Abort();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            th.Abort();
            ti = new Thread(new ThreadStart(PrograssImmediately));
            ti.Start();
        }

        private void PrograssImmediately()
        {
            if (via.SuccessFlag)
            {
                try
                {
                    Process.Start(via.FilePath);
                    Thread.Sleep(Convert.ToInt32((Convert.ToDecimal(via.Timer2) * 60 * 1000)));
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.GetType().ToString());
                    if (ex.GetType().ToString() != "System.Threading.ThreadAbortException")
                        MessageBox.Show(ex.Message + "1");
                }
            }
            if (via.SuccessFlag)
            {
                MySendKeys();
            }
        }  

    }
}

把改程式的捷徑拷入啟動裡,讓其成為開機自啟動程式

程式在VS2005 WinXp系統下測試成功!

 

局限性:

1.需要主板BIOS支援自動開機;

2.開機以後電腦就連在互連網上;

3.需要.net framework2.0的支援

來源:http://blog.csdn.net/firestone2003/archive/2006/06/24/829316.aspx#464431

用這個   System.Windows.Forms.SendKeys.Send(); 就是類比鍵盤
下面是一些特殊符號的鍵盤值
System.Windows.Forms.SendKeys.Send("+="); //值+
System.Windows.Forms.SendKeys.Send("+9"); //值(
System.Windows.Forms.SendKeys.Send("+8"); //值*
System.Windows.Forms.SendKeys.Send("+7"); //值&
System.Windows.Forms.SendKeys.Send("+6"); //值^
System.Windows.Forms.SendKeys.Send("+5"); //值%
System.Windows.Forms.SendKeys.Send("+4"); //值$
System.Windows.Forms.SendKeys.Send("+3"); //值#
System.Windows.Forms.SendKeys.Send("+2"); //值@
System.Windows.Forms.SendKeys.Send("+1"); //值!
System.Windows.Forms.SendKeys.Send("+0"); //值)
System.Windows.Forms.SendKeys.Send("+`"); //值~
下面是常用的值

代表的鍵 指定值 KeyLabelName

LEFTARROW

RIGHTARROW

UPARROW

DNARROW

HOME

HOME

END

END

PAGE UP

PGUP

PAGE DOWN

PGDN

DEL

DEL

BACKSPACE

BACKSPACE

SPACEBAR

SPACEBAR

INS

INS

TAB

TAB

SHIFT+TAB

BACKTAB

Left Brace

LBRACE

Right Brace

RBRACE

ENTER

ENTER

F1 to F12

F1, F2, F3 ...

CTRL+F1 to CTRL+F12

CTRL+F1, CTRL+F2 ...

SHIFT+F1 to SHIFT+F12

SHIFT+F1, SHIFT+F2 ...

ALT+F1 to ALT+F12

ALT+F1, ALT+F2, ALT+F3 ...

ALT+0 to ALT+9

ALT+0, ALT+1, ALT+2 ...

ALT+A to ALT+Z

ALT+A, ALT+B, ALT+C ...

CTRL+LEFT ARROW

CTRL+LEFTARROW

CTRL+RIGHT ARROW

CTRL+RIGHTARROW

CTRL+HOME

CTRL+HOME

CTRL+END

CTRL+END

CTRL+PAGE UP

CTRL+PGUP

CTRL+PAGE DOWN

CTRL+PGDN

CTRL+A TO CTRL+Z

CTRL+A, CTRL+B, CTRL+C ...

CTRL+0

CTRL+0

RIGHT MOUSE BUTTON

RIGHTMOUSE

LEFT MOUSE BUTTON

LEFTMOUSE

MOUSE BUTTON

MOUSE

ESC

ESC

來源:http://bbs.bc-cn.net/dispbbs.asp?boardid=117&id=140452

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.