KeyinListener.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using AwInitilizer.Model;
  2. using PhihongEv.Lib;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Input;
  10. using System.Windows.Threading;
  11. namespace AwInitilizer
  12. {
  13. public class KeyinListener
  14. {
  15. public event EventHandler<SystemID> OnSystemIDReseived;
  16. private string stackedInput = string.Empty;
  17. private bool shiftPressed = false;
  18. private DispatcherTimer CleanInputTimer;
  19. private Window sourceWindow;
  20. public KeyinListener(Window sourceWindow)
  21. {
  22. this.sourceWindow = sourceWindow;
  23. sourceWindow.Closing += SourceWindow_Closing;
  24. sourceWindow.PreviewKeyDown += MainWindow_PreviewKeyDown;
  25. sourceWindow.PreviewKeyUp += MainWindow_PreviewKeyUp;
  26. CleanInputTimer = new DispatcherTimer();
  27. CleanInputTimer.Interval = TimeSpan.FromSeconds(1);
  28. CleanInputTimer.Tick += CleanInputTimer_Tick;
  29. }
  30. private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
  31. {
  32. if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
  33. {
  34. shiftPressed = true;
  35. return;
  36. }
  37. else if (e.Key == Key.Capital)
  38. {
  39. shiftPressed = !shiftPressed;
  40. return;
  41. }
  42. else if (e.Key == Key.Enter)
  43. {
  44. e.Handled = true;
  45. return;
  46. }
  47. else
  48. {
  49. if (e.Key >= Key.D0 && e.Key <= Key.D9)
  50. {
  51. // Number keys pressed so need to so special processing
  52. // also check if shift pressed
  53. var input = e.Key.ToString();
  54. input = input.Remove(0, 1);
  55. stackedInput += input;
  56. }
  57. else if (e.Key == Key.OemMinus)
  58. {
  59. stackedInput += "-";
  60. }
  61. else if (e.Key == Key.Return)
  62. {
  63. return;
  64. }
  65. else
  66. {
  67. var input = e.Key.ToString();
  68. if (shiftPressed)
  69. input = input.ToUpper();
  70. stackedInput += input;
  71. }
  72. }
  73. CleanInputTimer.Stop();
  74. CleanInputTimer.Start();
  75. }
  76. private void MainWindow_PreviewKeyUp(object sender, KeyEventArgs e)
  77. {
  78. if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
  79. {
  80. shiftPressed = false;
  81. }
  82. }
  83. private void CleanInputTimer_Tick(object sender, EventArgs e)
  84. {
  85. CleanInputTimer.Stop();
  86. if (SystemID.TryLooseParse(stackedInput, out SystemID systemID))
  87. {
  88. OnSystemIDReseived?.Invoke(this, systemID);
  89. //SystemIDScanReseived(systemID);
  90. }
  91. stackedInput = string.Empty;
  92. }
  93. private void SourceWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  94. {
  95. try
  96. {
  97. sourceWindow.Closing -= SourceWindow_Closing;
  98. sourceWindow.PreviewKeyDown -= MainWindow_PreviewKeyDown;
  99. sourceWindow.PreviewKeyUp -= MainWindow_PreviewKeyUp;
  100. CleanInputTimer.Tick -= CleanInputTimer_Tick;
  101. CleanInputTimer.Stop();
  102. }
  103. catch
  104. {
  105. }
  106. }
  107. }
  108. }