星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. namespace ComeCapture.Controls
  5. {
  6. public class KeyboardTextBox : Control
  7. {
  8. static KeyboardTextBox()
  9. {
  10. DefaultStyleKeyProperty.OverrideMetadata(typeof(KeyboardTextBox), new FrameworkPropertyMetadata(typeof(KeyboardTextBox)));
  11. }
  12. public KeyboardTextBox()
  13. {
  14. AddHandler(PreviewKeyDownEvent, new KeyEventHandler(Keyboard_KeyDown));
  15. DataContext = AppModel.Current;
  16. }
  17. #region 键盘事件:只允许输入数字以及字母
  18. private void Keyboard_KeyDown(object sender, KeyEventArgs e)
  19. {
  20. int key = (int)e.Key;
  21. TextBox textbox = e.OriginalSource as TextBox;
  22. //A-Z
  23. if (key >= 44 && key <= 69)
  24. {
  25. textbox.Text = e.Key.ToString();
  26. textbox.Select(1, 0);
  27. }
  28. else if (key >= 34 && key <= 43) //0 - 9,
  29. {
  30. textbox.Text = e.Key.ToString().Substring(1);
  31. textbox.Select(1, 0);
  32. }
  33. e.Handled = true;
  34. }
  35. #endregion 键盘事件:只允许输入数字以及字母
  36. }
  37. }