KeyinListener.cs 3.5 KB

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