FullKeyboardForm.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. public partial class FullKeyboardForm : Form
  12. {
  13. public string Caption { set => lblCaption.Text = value; }
  14. public bool ValueChanged { get; set; }
  15. public string Value { get; set; }
  16. public char PasswordChar { set => txtPreview.PasswordChar = txtInput.PasswordChar = value; }
  17. public FullKeyboardForm()
  18. {
  19. InitializeComponent();
  20. }
  21. public FullKeyboardForm(string currentString):this()
  22. {
  23. txtPreview.Text = currentString;
  24. Value=currentString;
  25. }
  26. private void FullKeyboardForm_Load(object sender, EventArgs e)
  27. {
  28. doubleCharButtons = Controls
  29. .Cast<Control>()
  30. .Where(c => c.Name.Contains("DoubleChar"))
  31. .Select(c => (Button)c)
  32. .ToList();
  33. }
  34. private void FullKeyboardForm_Shown(object sender, EventArgs e)
  35. {
  36. txtInput.Focus();
  37. txtInput.SelectAll();
  38. }
  39. public bool IsPassword
  40. {
  41. set
  42. {
  43. if (value)
  44. txtInput.PasswordChar = '*';
  45. }
  46. }
  47. bool isCaps = false;
  48. bool isShift = false;
  49. bool isCtrl = false;
  50. bool isAlt = false;
  51. private void btnCharKey_Click(object sender, EventArgs e)
  52. {
  53. if (sender is Button button)
  54. {
  55. string stringToAdd;
  56. if (!isCaps)
  57. {
  58. stringToAdd = button.Text.ToLower().Replace("&&", "&");
  59. }
  60. else
  61. {
  62. stringToAdd = button.Text.ToUpper().Replace("&&", "&");
  63. }
  64. InsertOneChar(stringToAdd);
  65. }
  66. }
  67. int selectionStartSaved = 0;
  68. void InsertOneChar(string s)
  69. {
  70. if (selectionStartSaved != txtInput.SelectionStart)
  71. selectionStartSaved = txtInput.SelectionStart;
  72. if (txtInput.SelectionLength > 0)
  73. {
  74. txtInput.Text = txtInput.Text.Remove(selectionStartSaved, txtInput.SelectionLength);
  75. }
  76. txtInput.Text = txtInput.Text.Insert(selectionStartSaved, s);
  77. selectionStartSaved = selectionStartSaved + s.Length;
  78. txtInput.Focus();
  79. txtInput.SelectionStart = selectionStartSaved;
  80. txtInput.SelectionLength = 0;
  81. }
  82. private void btnShift_Click(object sender, EventArgs e)
  83. {
  84. isShift = !isShift;
  85. foreach (var item in doubleCharButtons)
  86. {
  87. if (isShift)
  88. {
  89. btnShift1.Text = btnShift2.Text = "Shift";
  90. item.Text = item.Tag.ToString().Split(' ')[0];
  91. }
  92. else
  93. {
  94. btnShift1.Text = btnShift2.Text = "shift";
  95. item.Text = item.Tag.ToString().Split(' ')[1];
  96. }
  97. }
  98. }
  99. List<Button> doubleCharButtons;
  100. private void btnCapsLock_Click(object sender, EventArgs e)
  101. {
  102. isCaps = !isCaps;
  103. if (isCaps)
  104. {
  105. btnCapsLock.Text = "Caps Lock";
  106. }
  107. else
  108. {
  109. btnCapsLock.Text = "caps lock";
  110. }
  111. }
  112. private void btnTab_Click(object sender, EventArgs e)
  113. {
  114. InsertOneChar('\t'.ToString());
  115. }
  116. private void btnBack_Click(object sender, EventArgs e)
  117. {
  118. if (txtInput.Text.Length > 0 && (txtInput.SelectionStart > 0 || txtInput.SelectionLength>0))
  119. {
  120. if (selectionStartSaved != txtInput.SelectionStart)
  121. selectionStartSaved = txtInput.SelectionStart;
  122. if (txtInput.SelectionLength == 0)
  123. {
  124. txtInput.Text = txtInput.Text.Remove(txtInput.SelectionStart - 1, 1);
  125. selectionStartSaved = selectionStartSaved - 1;
  126. txtInput.Focus();
  127. txtInput.SelectionStart = selectionStartSaved;
  128. txtInput.SelectionLength = 0;
  129. }
  130. else
  131. {
  132. txtInput.Text = txtInput.Text.Remove(txtInput.SelectionStart, txtInput.SelectionLength);
  133. txtInput.Focus();
  134. txtInput.SelectionStart = selectionStartSaved;
  135. txtInput.SelectionLength = 0;
  136. }
  137. }
  138. }
  139. private void btnLeft_Click(object sender, EventArgs e)
  140. {
  141. txtInput.Focus();
  142. if (txtInput.SelectionStart > 0)
  143. {
  144. txtInput.SelectionStart -= 1;
  145. }
  146. }
  147. private void btnRight_Click(object sender, EventArgs e)
  148. {
  149. txtInput.Focus();
  150. if (txtInput.SelectionStart < txtInput.Text.Length)
  151. {
  152. txtInput.SelectionStart += 1;
  153. }
  154. }
  155. private void btnCtrl_Click(object sender, EventArgs e)
  156. {
  157. isCtrl = !isCtrl;
  158. if (isCtrl)
  159. {
  160. btnCtrl1.Text = btnCtrl2.Text= "Ctrl";
  161. }
  162. else
  163. {
  164. btnCtrl1.Text = btnCtrl2.Text = "ctrl";
  165. }
  166. }
  167. private void btnAlt_Click(object sender, EventArgs e)
  168. {
  169. isAlt = !isAlt;
  170. if (isAlt)
  171. {
  172. btnAlt1.Text = btnAlt2.Text = "Alt";
  173. }
  174. else
  175. {
  176. btnAlt1.Text = btnAlt2.Text = "alt";
  177. }
  178. }
  179. private void btnEnter_Click(object sender, EventArgs e)
  180. {
  181. if(isCtrl || isAlt)
  182. {
  183. InsertOneChar(Environment.NewLine);
  184. }
  185. else
  186. {
  187. if(Value != txtInput.Text)
  188. {
  189. Value= txtInput.Text;
  190. ValueChanged= true;
  191. }
  192. DialogResult = DialogResult.OK;
  193. Close();
  194. }
  195. }
  196. private void btnWin1_Click(object sender, EventArgs e)
  197. {
  198. // 模拟按下并释放 Windows 键
  199. SimulateInput.SimulateKey(SimulateInput.VK_LWIN);
  200. }
  201. private void btnUp_Click(object sender, EventArgs e)
  202. {
  203. txtInput.Focus();
  204. SimulateInput.SimulateKey(SimulateInput.VK_UP);
  205. }
  206. private void btnDown_Click(object sender, EventArgs e)
  207. {
  208. txtInput.Focus();
  209. SimulateInput.SimulateKey(SimulateInput.VK_DOWN);
  210. }
  211. private void btnExit_Click(object sender, EventArgs e)
  212. {
  213. DialogResult = DialogResult.Cancel;
  214. Close();
  215. }
  216. }