-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
173 lines (154 loc) · 5.31 KB
/
Program.cs
File metadata and controls
173 lines (154 loc) · 5.31 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CommandLine;
namespace sendkeys
{
class Program
{
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
static void Main(string[] args)
{
CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed(RunOptions)
.WithNotParsed(HandleParseError);
#if DEBUG
Console.ReadLine();
#endif
}
private static void HandleParseError(IEnumerable<Error> errs)
{
}
private static void RunOptions(Options options)
{
if(options.KeyHelp)
{
KeyHelp();
return;
}
#if DEBUG
Console.WriteLine("Targeted window: {0}", options.WindowName);
Console.WriteLine("Key to be sent: {0}", options.Key);
#endif
if (options.WindowName == "" || options.Key == "")
return;
try
{
Process[] proc = Process.GetProcesses();
foreach (Process p in proc)
{
if (p.MainWindowTitle==options.WindowName)
{
IntPtr h = p.MainWindowHandle;
SetForegroundWindow(h);
SendKeys.SendWait(options.Key);
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
/// <summary>
/// Extended help for keys.
/// </summary>
private static void KeyHelp()
{
// Source: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys.send?view=netcore-3.1
string keyHelp;
keyHelp =
@"Each key is represented by one or more characters. To specify
a single keyboard character, use the character itself.
For example, to represent the letter A, pass in the string 'A'
to the method. To represent more than one character, append
each additional character to the one preceding it.
To represent the letters A, B, and C, specify the parameter as 'ABC'.
The plus sign (+), caret (^), percent sign (%), tilde (~), and
parentheses () have special meanings. To specify one of these characters,
enclose it within braces ({}). For example, to specify the plus sign, use {+}.
To specify brace characters, use {{} and {}}. Brackets ([ ]) have no
special meaning to SendKeys, but you must enclose them in braces.
In other applications, brackets do have a special meaning that
might be significant when dynamic data exchange (DDE) occurs.
To specify characters that aren't displayed when you press a key,
such as ENTER or TAB, and keys that represent actions rather than
characters, use the codes in the following table.
Key Code
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER}or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC} (reserved for future use)
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
Keypad add {ADD}
Keypad subtract {SUBTRACT}
Keypad multiply {MULTIPLY}
Keypad divide {DIVIDE}
To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys,
precede the key code with one or more of the following codes.
Key Code
SHIFT +
CTRL ^
ALT %
To specify that any combination of SHIFT, CTRL, and ALT should be held down
while several other keys are pressed, enclose the code for those keys in
parentheses. For example, to specify to hold down SHIFT while E and C are
pressed, use +(EC). To specify to hold down SHIFT while E is pressed, followed
by C without SHIFT, use +EC.
To specify repeating keys, use the form {key number}. You must put a space
between key and number.For example, {LEFT 42} means press the LEFT ARROW
key 42 times; {h 10} means press H 10 times.";
Console.WriteLine(keyHelp);
}
public class Options
{
[Option('w',
Required = false,
HelpText = "Window name where the key will be sent.")]
public string WindowName { get; set; }
[Option('k',
Required = false,
HelpText = "The key to be sent.")]
public string Key { get; set; }
[Option("keyhelp",
Required = false,
HelpText = "Display extented help")]
public bool KeyHelp { get; set; }
}
}
}