Skip to content

Commit

Permalink
Improve ClipboardTextExtractor to wait until the user released all keys
Browse files Browse the repository at this point in the history
  • Loading branch information
haefele committed Oct 16, 2023
1 parent 7b28502 commit c8159db
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ChatPrisma/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
{
o.Key = "Y";
o.KeyModifiers = "Ctrl+Shift+Alt";
o.HotkeyDelayInMilliseconds = 200;
o.HotkeyDelayInMilliseconds = 500;
})
.BindConfiguration("Hotkey")
.ValidateDataAnnotations()
Expand Down
48 changes: 43 additions & 5 deletions src/ChatPrisma/Services/TextExtractor/ClipboardTextExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System.Windows.Forms;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows.Input;
using ChatPrisma.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace ChatPrisma.Services.TextExtractor;

public class ClipboardTextExtractor(IOptionsMonitor<HotkeyOptions> hotkeyOptions) : ITextExtractor
public class ClipboardTextExtractor(IOptionsMonitor<HotkeyOptions> hotkeyOptions, ILogger<ClipboardTextExtractor> logger) : ITextExtractor
{
public async Task<string?> GetCurrentTextAsync()
{
// Give the user a bit of time to release the keyboard keys,
// otherwise CTRL+C will not work
await Task.Delay(TimeSpan.FromMilliseconds(hotkeyOptions.CurrentValue.HotkeyDelayInMilliseconds));
// We gotta wait until no keys are pressed anymore, otherwise CTRL+C will not work
await this.WaitUntilNoKeyPressed();

// Need to clear the clipboard, or otherwise we might get some previously copied text
Clipboard.Clear();
Expand All @@ -23,4 +25,40 @@ public class ClipboardTextExtractor(IOptionsMonitor<HotkeyOptions> hotkeyOptions

return selectedText;
}

private async Task WaitUntilNoKeyPressed()
{
var task = Task.Delay(TimeSpan.FromMilliseconds(hotkeyOptions.CurrentValue.HotkeyDelayInMilliseconds));
var watch = Stopwatch.StartNew();

// Either wait until the task is completed or the user releases all keys
while (task.IsCompleted is false)
{
if (AnyKeyPressed() is false)
{
logger.LogInformation("Early exit from WaitUntilNoKeyPressed because no key is pressed anymore (after {Time} ms)", watch.Elapsed.TotalMilliseconds);
return;
}

await Task.Delay(10);
}

logger.LogInformation("Sadly the user did not release all keys in time (after {Time} ms)", watch.Elapsed.TotalMilliseconds);
}

private static readonly Key[] s_allKeys = Enum.GetValues<Key>();
private static bool AnyKeyPressed()
{
foreach (var key in s_allKeys)
{
// Skip the None key
if (key == Key.None)
continue;

if (Keyboard.IsKeyDown(key))
return true;
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/ChatPrisma/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"Hotkey": {
"Key": "Y",
"KeyModifiers": "Ctrl+Shift+Alt",
"HotkeyDelayInMilliseconds": 200,
"HotkeyDelayInMilliseconds": 500,
},
"NLog": {
"throwConfigExceptions": true,
Expand Down

0 comments on commit c8159db

Please sign in to comment.