Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ SendKeys has no dependencies besides the .NET Framework 2.0 and can be deployed
###### Same but wait 3 seconds upfront:
`SendKeys.exe -pid:4711 "format C:{Enter}" -wait:3000`

###### Same but read a file and send the contents of that file to the new pid:
`SendKeys.exe -pid:4711 -wait:3000 -file:"C:\test.txt"`


## Noteworthy

As always, you'll need to add quotes to the argument string if it contains spaces (like shown in the examples). Otherwise, Windows will split it up as multiple arguments.
Expand Down
60 changes: 55 additions & 5 deletions SendKeys/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace SendKeys
{
Expand All @@ -26,16 +27,21 @@ static void Main(string[] args)
{
int pid = -1;
int wait = 0;
int file = 0;
string line;
bool filepresent = false;
string filetoread = "";
string keysToSend = "";

var validArguments = args?.Length == 2 || args?.Length == 3;
var validArguments = args?.Length == 2 || args?.Length == 3 || args?.Length == 4;

if (validArguments)
{
for (int i = 0; i < args.Length; i++)
{
int pidIndex = args[i].IndexOf("pid:", StringComparison.OrdinalIgnoreCase);
int waitIndex = args[i].IndexOf("wait:", StringComparison.OrdinalIgnoreCase);
int fileIndex = args[i].IndexOf("file:", StringComparison.OrdinalIgnoreCase);
if (pidIndex > -1)
{
var pidString = args[i].Substring(pidIndex + "pid:".Length);
Expand All @@ -46,6 +52,13 @@ static void Main(string[] args)
var waitString = args[i].Substring(waitIndex + "wait:".Length);
int.TryParse(waitString, out wait);
}
else if (fileIndex > -1)
{
var fileString = args[i].Substring(waitIndex + "file:".Length + 2);
int.TryParse(fileString, out file);
filepresent = true;
filetoread = fileString;
}
else
{
keysToSend = args[i].Replace("'", "\"");
Expand All @@ -57,12 +70,13 @@ static void Main(string[] args)
{
WriteError("Invalid arguments. Please define a process id and the string value to send as keys." +
"\n Example: SendKeys.exe -pid:4711 \"Keys to send{Enter}\"" +
"\n Optional: Add -wait:100 to add a delay of 100 milliseconds, for example.");
"\n Optional: Add -wait:100 to add a delay of 100 milliseconds, for example." +
"\n Optional: Add -file:'test.txt' to read the contents of a file.");

return;
}

Process process = null ;
Process process = null;
try
{
process = Process.GetProcessById(pid);
Expand All @@ -81,9 +95,38 @@ static void Main(string[] args)
{
if (wait > 0)
Thread.Sleep(wait);
if (filepresent)
{
SetForegroundWindow(process.MainWindowHandle);
try
{
StreamReader sr = new StreamReader(filetoread);
int linenumber = 0;
line = sr.ReadLine();
while (sr.Peek() > -1)
{
SetForegroundWindow(process.MainWindowHandle);
string keytosendline = line.Replace("+", "{+}");
System.Windows.Forms.SendKeys.SendWait(keytosendline + '\n');
WriteInfo("On line: " + linenumber.ToString());

linenumber = linenumber + 1;
line = sr.ReadLine();
}
sr.Close();
}
catch (Exception e)
{
WriteError("Exception: " + e.Message);

}

SetForegroundWindow(process.MainWindowHandle);
System.Windows.Forms.SendKeys.SendWait(keysToSend);
}
else
{
SetForegroundWindow(process.MainWindowHandle);
System.Windows.Forms.SendKeys.SendWait(keysToSend);
}
}
}

Expand All @@ -93,5 +136,12 @@ private static void WriteError(string message)
Console.WriteLine(message);
FreeConsole();
}

public static void WriteInfo(string message)
{
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine(message);
FreeConsole();
}
}
}
112 changes: 52 additions & 60 deletions SendKeys/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 15 additions & 19 deletions SendKeys/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion SendKeys/SendKeys.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<OutputType>WinExe</OutputType>
<RootNamespace>SendKeys</RootNamespace>
<AssemblyName>SendKeys</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -21,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -30,6 +32,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject>SendKeys.Program</StartupObject>
Expand All @@ -56,7 +59,9 @@
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
3 changes: 3 additions & 0 deletions SendKeys/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>