using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace keyboardDemo { public partial class Form1 : Form { /// <summary> /// 涂聚文 /// 软键盘http://code.msdn.microsoft.com/CSSoftKeyboard-0a86f914 /// </summary> public Form1() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { Form2 f = new Form2(); f.Press += MiniKeyboardHandler; f.ShowDialog(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MiniKeyboardHandler(object sender, MiniKeyboardArgs e) { if (this.textBox1.Text == "←backpspace") { if (this.textBox1.Text.Length != 0) { } } else { textBox1.Text += e.KeyCode; string s = this.textBox1.Text; if (s.Contains("←backpspace"))//去除一个字符串 { s = s.Replace("←backpspace", ""); if (s.Length > 0) { s = s.Substring(0, s.Length - 1); this.textBox1.Text = s; } else { this.textBox1.Text = s; } } } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace keyboardDemo { public partial class Form2 : Form { bool isShift = true; /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void MiniKeyboardHandler(object sender, MiniKeyboardArgs e); /// <summary> /// /// </summary> public event MiniKeyboardHandler Press; /// <summary> /// /// </summary> private void BindEvent() { foreach (Control ctl in this.Controls) { if (ctl is Button) ctl.Click += MinikeyboardPress; } } /// <summary> /// /// </summary> public Form2() { InitializeComponent(); BindEvent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form2_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MinikeyboardPress(object sender, EventArgs e) { //大小写 if (((Button)sender).Text == "Shift") { if (isShift == true) { isShift = false; string s = this.button1.Text; s = s.ToUpper(); this.button1.Text = s; } else { isShift = true; string s = this.button1.Text; s = s.ToLower(); this.button1.Text = s; } } else { OnMiniKeyboardHandle(new MiniKeyboardArgs(((Button)sender).Text)); } } /// <summary> /// /// </summary> /// <param name="e"></param> private void OnMiniKeyboardHandle(MiniKeyboardArgs e) { MiniKeyboardHandler temp = Press; if (temp != null) temp(this, e); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click(object sender, EventArgs e) { //if (isShift == true) //{ // isShift = false; // this.button1.Text.ToUpper(); //} //else //{ // isShift = true; //} } } /// <summary> /// /// </summary> public class MiniKeyboardArgs : EventArgs { public string KeyCode { get; private set; } public MiniKeyboardArgs(string code) { if (code == "←backpspace") { if (code.Length != 0) { KeyCode = code; } else { KeyCode = code; //KeyCode = "";Caps Lock } } else if (code == "Shift") { KeyCode = ""; } else if (code == "Caps Lock") { KeyCode = ""; } else { KeyCode = code; } } } }