Skip to content

Commit

Permalink
Implemented option for copying the current clipboard text if no text …
Browse files Browse the repository at this point in the history
…input is present
  • Loading branch information
CoreyHayward committed Jan 2, 2024
1 parent bc7a961 commit 443c3d1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>Community.PowerToys.Run.Plugin.InputTyper</RootNamespace>
<AssemblyName>Community.PowerToys.Run.Plugin.InputTyper</AssemblyName>
<Version>0.1.1</Version>
<Version>0.2.0</Version>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Platforms>AnyCPU;x64;ARM64</Platforms>
Expand Down
33 changes: 33 additions & 0 deletions Community.PowerToys.Run.Plugin.InputTyper/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public List<Result> Query(Query query)
results.Add(new Result
{
Title = $"Type: {text}",
SubTitle = "Types the text into the selected input.",
IcoPath = _icon_path,
Action = c =>
{
Expand All @@ -59,6 +60,20 @@ public List<Result> Query(Query query)
},
});
}
else
{
results.Add(new Result
{
Title = "Type Clipboard",
SubTitle = "Types the current clipboard into the selected input.",
IcoPath = _icon_path,
Action = c =>
{
Task.Run(() => RunAsSTAThread(() => _typer.TypeClipboard(_beginTypeDelay)));
return true;
}
}) ;
}

return results;
}
Expand Down Expand Up @@ -95,5 +110,23 @@ public void UpdateSettings(PowerLauncherPluginSettings settings)
var typeDelay = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "BeginTypeDelay");
_beginTypeDelay = (int)(typeDelay?.NumberValue ?? 200);
}

/// <summary>
/// Start an Action within an STA Thread
/// </summary>
/// <param name="action">The action to execute in the STA thread</param>
static void RunAsSTAThread(Action action)
{
AutoResetEvent @event = new AutoResetEvent(false);
Thread thread = new Thread(
() =>
{
action();
@event.Set();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
@event.WaitOne();
}
}
}
13 changes: 13 additions & 0 deletions Community.PowerToys.Run.Plugin.InputTyper/Typer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Corey Hayward. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Clipboard = System.Windows.Clipboard;

namespace Community.PowerToys.Run.Plugin.InputTyper
{
internal sealed class Typer
Expand Down Expand Up @@ -29,5 +31,16 @@ public void Type(string str, int delay = 2000)
Thread.Sleep(INTERKEYDELAY);
}
}

internal void TypeClipboard(int beginTypeDelay)
{
if (!Clipboard.ContainsText())
{
return;
}

var text = Clipboard.GetText();
Type(text, beginTypeDelay);
}
}
}
2 changes: 1 addition & 1 deletion Community.PowerToys.Run.Plugin.InputTyper/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"IsGlobal": false,
"Name": "InputTyper",
"Author": "Corey Hayward",
"Version": "0.1.1",
"Version": "0.2.0",
"Language": "csharp",
"Website": "https://github.com/CoreyHayward/PowerToys-Run-InputTyper",
"IcoPathDark": "Images\\InputTyper.dark.png",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Simple [PowerToys Run](https://learn.microsoft.com/windows/powertoys/run) plugin
## Usage
- Select/Place cursor where text should be typed
- Open PowerToys Run
- Input: "@@ \<text\>"
- Input: "@@ \<text\>" or "@@" for clipboard
- Select the result (ENTER)
- \<text\> is typed into the selected location

0 comments on commit 443c3d1

Please sign in to comment.