| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- public partial class FullKeyboardForm : Form
- {
- public string Caption { set => lblCaption.Text = value; }
- public bool ValueChanged { get; set; }
- public string Value { get; set; }
- public char PasswordChar { set => txtPreview.PasswordChar = txtInput.PasswordChar = value; }
- public FullKeyboardForm()
- {
- InitializeComponent();
- }
- public FullKeyboardForm(string currentString):this()
- {
- txtPreview.Text = currentString;
- Value=currentString;
- }
- private void FullKeyboardForm_Load(object sender, EventArgs e)
- {
- doubleCharButtons = Controls
- .Cast<Control>()
- .Where(c => c.Name.Contains("DoubleChar"))
- .Select(c => (Button)c)
- .ToList();
- }
- private void FullKeyboardForm_Shown(object sender, EventArgs e)
- {
- txtInput.Focus();
- txtInput.SelectAll();
- }
- public bool IsPassword
- {
- set
- {
- if (value)
- txtInput.PasswordChar = '*';
- }
- }
- bool isCaps = false;
- bool isShift = false;
- bool isCtrl = false;
- bool isAlt = false;
- private void btnCharKey_Click(object sender, EventArgs e)
- {
- if (sender is Button button)
- {
- string stringToAdd;
- if (!isCaps)
- {
- stringToAdd = button.Text.ToLower().Replace("&&", "&");
- }
- else
- {
- stringToAdd = button.Text.ToUpper().Replace("&&", "&");
- }
- InsertOneChar(stringToAdd);
- }
- }
- int selectionStartSaved = 0;
- void InsertOneChar(string s)
- {
- if (selectionStartSaved != txtInput.SelectionStart)
- selectionStartSaved = txtInput.SelectionStart;
- if (txtInput.SelectionLength > 0)
- {
- txtInput.Text = txtInput.Text.Remove(selectionStartSaved, txtInput.SelectionLength);
- }
- txtInput.Text = txtInput.Text.Insert(selectionStartSaved, s);
- selectionStartSaved = selectionStartSaved + s.Length;
- txtInput.Focus();
- txtInput.SelectionStart = selectionStartSaved;
- txtInput.SelectionLength = 0;
- }
- private void btnShift_Click(object sender, EventArgs e)
- {
- isShift = !isShift;
- foreach (var item in doubleCharButtons)
- {
- if (isShift)
- {
- btnShift1.Text = btnShift2.Text = "Shift";
- item.Text = item.Tag.ToString().Split(' ')[0];
- }
- else
- {
- btnShift1.Text = btnShift2.Text = "shift";
- item.Text = item.Tag.ToString().Split(' ')[1];
- }
- }
-
- }
- List<Button> doubleCharButtons;
- private void btnCapsLock_Click(object sender, EventArgs e)
- {
- isCaps = !isCaps;
- if (isCaps)
- {
- btnCapsLock.Text = "Caps Lock";
- }
- else
- {
- btnCapsLock.Text = "caps lock";
- }
- }
- private void btnTab_Click(object sender, EventArgs e)
- {
- InsertOneChar('\t'.ToString());
- }
- private void btnBack_Click(object sender, EventArgs e)
- {
- if (txtInput.Text.Length > 0 && (txtInput.SelectionStart > 0 || txtInput.SelectionLength>0))
- {
- if (selectionStartSaved != txtInput.SelectionStart)
- selectionStartSaved = txtInput.SelectionStart;
- if (txtInput.SelectionLength == 0)
- {
- txtInput.Text = txtInput.Text.Remove(txtInput.SelectionStart - 1, 1);
- selectionStartSaved = selectionStartSaved - 1;
- txtInput.Focus();
- txtInput.SelectionStart = selectionStartSaved;
- txtInput.SelectionLength = 0;
- }
- else
- {
- txtInput.Text = txtInput.Text.Remove(txtInput.SelectionStart, txtInput.SelectionLength);
- txtInput.Focus();
- txtInput.SelectionStart = selectionStartSaved;
- txtInput.SelectionLength = 0;
- }
- }
- }
- private void btnLeft_Click(object sender, EventArgs e)
- {
- txtInput.Focus();
- if (txtInput.SelectionStart > 0)
- {
- txtInput.SelectionStart -= 1;
- }
- }
- private void btnRight_Click(object sender, EventArgs e)
- {
- txtInput.Focus();
- if (txtInput.SelectionStart < txtInput.Text.Length)
- {
- txtInput.SelectionStart += 1;
- }
- }
- private void btnCtrl_Click(object sender, EventArgs e)
- {
- isCtrl = !isCtrl;
- if (isCtrl)
- {
- btnCtrl1.Text = btnCtrl2.Text= "Ctrl";
- }
- else
- {
- btnCtrl1.Text = btnCtrl2.Text = "ctrl";
- }
- }
- private void btnAlt_Click(object sender, EventArgs e)
- {
- isAlt = !isAlt;
- if (isAlt)
- {
- btnAlt1.Text = btnAlt2.Text = "Alt";
- }
- else
- {
- btnAlt1.Text = btnAlt2.Text = "alt";
- }
- }
- private void btnEnter_Click(object sender, EventArgs e)
- {
- if(isCtrl || isAlt)
- {
- InsertOneChar(Environment.NewLine);
- }
- else
- {
-
- if(Value != txtInput.Text)
- {
- Value= txtInput.Text;
- ValueChanged= true;
- }
- DialogResult = DialogResult.OK;
- Close();
- }
- }
-
- private void btnWin1_Click(object sender, EventArgs e)
- {
- // 模拟按下并释放 Windows 键
- SimulateInput.SimulateKey(SimulateInput.VK_LWIN);
- }
- private void btnUp_Click(object sender, EventArgs e)
- {
- txtInput.Focus();
- SimulateInput.SimulateKey(SimulateInput.VK_UP);
- }
- private void btnDown_Click(object sender, EventArgs e)
- {
- txtInput.Focus();
- SimulateInput.SimulateKey(SimulateInput.VK_DOWN);
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- DialogResult = DialogResult.Cancel;
- Close();
- }
- }
|