Program.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. namespace DreamCheekyUSB {
  6. class Program {
  7. static string strCMD = "";
  8. static string strCMDARGs = "";
  9. static string strMacro = "";
  10. static int count = 0;
  11. static int Main(string[] args) {
  12. int actions = 0;
  13. DreamCheekyBTN btn = null;
  14. try {
  15. if (args.ContainsInsensitive("debug")) {
  16. Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  17. }
  18. string devicearg = args.StartsWith("device=").FirstOrDefault();
  19. if (string.IsNullOrEmpty(devicearg)) {
  20. Trace.WriteLine("\r\nConnecting to DreamCheekyBTN using default values...");
  21. try {
  22. btn = new DreamCheekyBTN();
  23. } catch (Exception ignored) {
  24. Trace.WriteLine(ignored.Message);
  25. Trace.WriteLine("No Iron Man USB Stress Buttons found.\r\nSearching for Big Red Buttons...");
  26. btn = new DreamCheekyBigRedBTN(DreamCheekyBigRedBTN.DEFAULT_VENDOR_ID, DreamCheekyBigRedBTN.DEFAULT_PRODUCT_ID);
  27. }
  28. } else {
  29. Trace.WriteLine("\r\nConnecting to DreamCheekyBTN using specified device {0}...", devicearg);
  30. string[] deviceSplit = devicearg.Substring(7).Split(',');
  31. if (deviceSplit.Length == 1) {
  32. //One argument = device path
  33. var searchString = string.Format("pid_{0}", DreamCheekyBigRedBTN.PID);
  34. if (deviceSplit[0].Contains(searchString)) {
  35. Trace.WriteLine("Device is a Big Red Button...");
  36. btn = new DreamCheekyBigRedBTN(deviceSplit[0]);
  37. } else {
  38. Trace.WriteLine("Device is a Iron Man USB Stress Button...");
  39. btn = new DreamCheekyBTN(deviceSplit[0]);
  40. }
  41. } else {
  42. //Two or Three arguments = VID,PID,Count=0
  43. int devicecount = 0;
  44. if (deviceSplit.Length > 2) {
  45. devicecount = int.Parse(deviceSplit[2]);
  46. }
  47. int VID = int.Parse(deviceSplit[0].Substring(2), System.Globalization.NumberStyles.HexNumber);
  48. int PID = int.Parse(deviceSplit[1].Substring(2), System.Globalization.NumberStyles.HexNumber);
  49. if (PID == DreamCheekyBTN.DEFAULT_PRODUCT_ID) {
  50. btn = new DreamCheekyBTN(VID, PID, devicecount);
  51. } else {
  52. btn = new DreamCheekyBigRedBTN(VID, PID, devicecount);
  53. }
  54. }
  55. }
  56. string cmdarg = args.StartsWith("CMD=").FirstOrDefault();
  57. if (!string.IsNullOrEmpty(cmdarg)) {
  58. actions++;
  59. strCMD = cmdarg.Substring(4);
  60. Console.WriteLine("Setting command to: " + strCMD);
  61. }
  62. string argarg = args.StartsWith("ARG=").FirstOrDefault();
  63. if (!string.IsNullOrEmpty(argarg)) {
  64. strCMDARGs = argarg.Substring(4);
  65. Console.WriteLine("Setting command arguments to: " + strCMDARGs);
  66. }
  67. string macroarg = args.StartsWith("MACRO=").FirstOrDefault();
  68. if (!string.IsNullOrEmpty(macroarg)) {
  69. actions++;
  70. string[] macosplit = macroarg.Split('=');
  71. strMacro = macosplit[1];
  72. Console.WriteLine("Setting Macro to: " + strMacro);
  73. }
  74. if (actions > 0) {
  75. btn.RegisterCallback(DoAction);
  76. Console.WriteLine("Listening for button press events. Press any key to escape...");
  77. Console.ReadKey(true);
  78. }
  79. } catch (Exception ex) {
  80. Trace.TraceError("\r\n\r\nError: " + ex.Message + "\r\n\r\n");
  81. }
  82. //Pause on exit or display usage syntax
  83. if (actions > 0) {
  84. Trace.WriteLine("Finished\r\n");
  85. } else { //No actions specified, show help
  86. Console.WriteLine(" DreamCheekyBTN.exe [device=...] [options]");
  87. Console.WriteLine("\r\nExamples:");
  88. Console.WriteLine(" DreamCheekyBTN.exe debug MACRO=ASDF~ (ASDF then Enter)");
  89. Console.WriteLine(" DreamCheekyBTN.exe MACRO=%+{F1} (ALT+SHIFT+F1)");
  90. Console.WriteLine(" DreamCheekyBTN.exe CMD=c:\\temp\\test.bat");
  91. Console.WriteLine(@" DreamCheekyBTN.exe CMD=powershell ARG=""-noexit -executionpolicy unrestricted -File c:\test.ps1""");
  92. Console.WriteLine("\r\n\r\nDevice Path:");
  93. Console.WriteLine(" Optional, Defaults to first USB device with VID=0x1D34 and PID=0x0008");
  94. Console.WriteLine(" Example (VID,PID,Index): device=\"0x1D34,0x0008,0\"");
  95. Console.WriteLine(" Example (Path): device=" + @"""\\?\hid#vid_1d34&pid_0008#6&1067c3dc&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}""");
  96. Console.WriteLine("\r\nOptions:");
  97. Console.WriteLine(" debug = Print Console statements to Console.Out");
  98. Console.WriteLine("\r\nCMD: will run specified command when button is pressed");
  99. Console.WriteLine("ARG: can be used to specified command arguments");
  100. Console.WriteLine(" Example (open calculator): CMD=calc");
  101. Console.WriteLine(" Example (run Powershell commands): ");
  102. Console.WriteLine(" CMD=\"%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe\"");
  103. Console.WriteLine(@" ARG=""-Command \""& {write-host 'BEEP!'; [console]::beep(440,1000);}\""""");
  104. Console.WriteLine(" NOTE: use ^& instead of & if running from command prompt as & is special character");
  105. Console.WriteLine("\r\nMACRO: will send specified key sequense to active window via C# Sendkeys");
  106. Console.WriteLine("NOTE: +=Shift, ^=CTRL, %=ALT, ~=Return, use () to group characters.");
  107. Console.WriteLine(" Example: MACRO=\"%^g\" (ALT + CTRL + g)");
  108. Console.WriteLine(" Example: MACRO=\"%(asdf)\" (ALT + asdf)");
  109. Console.WriteLine("\r\n");
  110. }
  111. if (args.ContainsInsensitive("pause")) {
  112. Console.WriteLine("\r\nPress enter to exit...");
  113. Console.ReadLine();
  114. }
  115. if (btn != null) {
  116. btn.Dispose();
  117. }
  118. return 0;
  119. }
  120. static void DoAction() {
  121. count++;
  122. Console.WriteLine(String.Format("\r\n{0}: Detected button press event. Count={1}", DateTime.Now.ToLongTimeString(), count));
  123. if (!string.IsNullOrEmpty(strCMD)) {
  124. try {
  125. Process.Start(strCMD, strCMDARGs);
  126. } catch (Exception ex) {
  127. Trace.TraceError("Error: " + ex.Message);
  128. }
  129. }
  130. if (!string.IsNullOrEmpty(strMacro)) {
  131. try {
  132. Console.WriteLine("Sending keys: " + strMacro);
  133. System.Windows.Forms.SendKeys.SendWait(strMacro);
  134. } catch (Exception ex) {
  135. Trace.TraceError("Error: " + ex.Message);
  136. }
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Extenstions for working with string arrays
  142. /// </summary>
  143. public static class StringArrayExtenstions {
  144. public static bool ContainsInsensitive(this string[] args, string name) {
  145. return args.Contains(name, StringComparer.CurrentCultureIgnoreCase);
  146. }
  147. public static IEnumerable<string> StartsWith(this string[] args, string value, StringComparison options = StringComparison.CurrentCultureIgnoreCase) {
  148. var q = from a in args
  149. where a.StartsWith(value, options)
  150. select a;
  151. return q;
  152. }
  153. }
  154. }