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