我想模拟键盘硬件输入,例如当前输入法状态是“紫光拼音输入法”,我想模拟在键盘上按下一个a键,再模拟按下空格键,然后就可以在文本框中输入一个“啊”汉字

解决方案 »

  1.   


    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }    /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;    /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }    #region Windows Form Designer generated code    /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(86, 129);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(51, 63);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(198, 21);
            this.textBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();    }    #endregion    private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;    private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            this.textBox1.ImeMode = ImeMode.OnHalf;        SendKeys.SendWait("a ");
        }
    }
      

  2.   

    谢谢水月流影,我是想用API实现模拟键盘硬件输入,下面代码,第一次单击button1按钮时可以输入一个字母a,但是没有空格,第二次单击button1时,变成输入了一个大写的字母A了,为什么变大写了呢?难道(keybd_event(20, MapVirtualKey(20, 0), 1, 0);)不是模拟输入一个空格键吗?空格键的Virtual-Key码是20啊,这是怎么回事,请高手指教!!![DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);[DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern byte MapVirtualKey(Byte bVk, int c);
    private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Focus();
                
                keybd_event(0x41, MapVirtualKey(0x41,0), 0, 0);
                keybd_event(20, MapVirtualKey(20, 0), 1, 0);
                         }