Skip to content

Commit bd99d45

Browse files
committed
Drop SharpClipboard, use own clipboard monitoring implementation
Closes #102
1 parent fe628d1 commit bd99d45

5 files changed

Lines changed: 275 additions & 188 deletions

File tree

PasteIntoFile/Dialog.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,21 @@
88
using System.Windows.Forms;
99
using PasteIntoFile.Properties;
1010
using WK.Libraries.BetterFolderBrowserNS;
11-
using WK.Libraries.SharpClipboardNS;
1211

1312
namespace PasteIntoFile {
1413
public sealed partial class Dialog : MasterForm {
1514
private ClipboardContents clipData = new ClipboardContents();
1615
private int saveCount = 0;
1716
private bool _formLoaded = false;
1817

19-
private SharpClipboard _clipMonitor;
18+
private SystemEventMonitor eventMonitor = new SystemEventMonitor();
2019
private bool _disableUiEvents = false;
2120
private bool _topMostPreviousState = false;
2221
private const string DYNAMIC_EXTENSION = "*"; // special value to determine extension dynamically
2322

24-
public SharpClipboard clipMonitor {
25-
get {
26-
if (_clipMonitor == null) _clipMonitor = new SharpClipboard();
27-
return _clipMonitor;
28-
}
29-
}
3023
protected override void OnFormClosed(FormClosedEventArgs e) {
3124
// leave the clipboard monitoring chain in a clean way, otherwise the chain will break when the program exits
32-
clipMonitor?.StopMonitoring();
25+
eventMonitor?.StopClipboardMonitoring();
3326
base.OnFormClosed(e);
3427
}
3528

@@ -118,8 +111,8 @@ public Dialog(
118111
BringToFrontForced();
119112

120113
// register clipboard monitor
121-
clipMonitor.ClipboardChanged += ClipboardChanged;
122-
FormClosing += (s, e) => clipMonitor.ClipboardChanged -= ClipboardChanged;
114+
eventMonitor.StartClipboardMonitoring();
115+
eventMonitor.ClipboardChanged += ClipboardChanged;
123116

124117

125118
} else {
@@ -286,7 +279,7 @@ private bool readClipboard() {
286279

287280

288281

289-
private void ClipboardChanged(Object sender, SharpClipboard.ClipboardChangedEventArgs e) {
282+
private void ClipboardChanged(Object sender, EventArgs e) {
290283
// Only process update if live update enabled, or in batch mode
291284
if (!chkEnableLiveClipboardUpdate.Checked && !chkContinuousMode.Checked) return;
292285

@@ -465,9 +458,10 @@ string save(bool overwriteIfExists = false, bool? clearClipboardOverwrite = fals
465458
}
466459

467460
if (clearClipboardOverwrite ?? Settings.Default.clrClipboard) {
468-
clipMonitor.MonitorClipboard = false; // to prevent callback during batch mode
469-
Clipboard.Clear();
470-
clipMonitor.MonitorClipboard = true;
461+
// Prevent callback during batch mode
462+
eventMonitor.CallWithoutClipboardMonitoring(() => {
463+
Clipboard.Clear();
464+
});
471465
}
472466

473467
rememberExtension(content, comExt.Text);

PasteIntoFile/KeyboardHook.cs

Lines changed: 0 additions & 133 deletions
This file was deleted.

PasteIntoFile/Main.cs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using CommandLine.Text;
1515
using Microsoft.Toolkit.Uwp.Notifications;
1616
using PasteIntoFile.Properties;
17-
using WK.Libraries.SharpClipboardNS;
1817
#if PORTABLE
1918
using Bluegrams.Application;
2019
#endif
@@ -273,56 +272,55 @@ static int RunTray(ArgsTray args = null) {
273272
}
274273

275274

275+
var monitor = new SystemEventMonitor();
276+
276277
// Register hotkeys
277-
KeyboardHook paste = new KeyboardHook();
278-
paste.KeyPressed += (s, e) => {
279-
var arg = new ArgsPaste();
280-
arg.Directory = ExplorerUtil.GetActiveExplorerPath();
281-
RunPaste(arg);
282-
};
283-
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.V);
284-
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift, Keys.V);
285-
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Control, Keys.V);
286-
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift | ModifierKeys.Control, Keys.V);
287-
288-
KeyboardHook copy = new KeyboardHook();
289-
copy.KeyPressed += (s, e) => {
290-
var files = ExplorerUtil.GetActiveExplorerSelectedFiles();
291-
if (files.Count == 1) {
292-
var arg = new ArgsCopy();
293-
arg.FilePath = files.Item(0).Path;
294-
RunCopy(arg);
295-
} else {
296-
MessageBox.Show(Resources.str_copy_failed_not_single_file, Resources.app_title, MessageBoxButtons.OK, MessageBoxIcon.Error);
278+
monitor.KeyPressed += (s, e) => {
279+
if (e.Key == Keys.V) {
280+
// Paste hotkey
281+
var arg = new ArgsPaste();
282+
arg.Directory = ExplorerUtil.GetActiveExplorerPath();
283+
RunPaste(arg);
284+
} else if (e.Key == Keys.C) {
285+
// Copy hotkey
286+
var files = ExplorerUtil.GetActiveExplorerSelectedFiles();
287+
if (files.Count == 1) {
288+
var arg = new ArgsCopy();
289+
arg.FilePath = files.Item(0).Path;
290+
RunCopy(arg);
291+
} else {
292+
MessageBox.Show(Resources.str_copy_failed_not_single_file, Resources.app_title, MessageBoxButtons.OK, MessageBoxIcon.Error);
293+
}
297294
}
298295
};
299-
copy.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.C);
296+
// Paste hotkeys (with different modifier combinations)
297+
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.V);
298+
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift, Keys.V);
299+
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Control, Keys.V);
300+
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift | ModifierKeys.Control, Keys.V);
301+
// Copy hotkey
302+
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.C);
303+
300304

301305
// Register clipboard observer for patching
302-
SharpClipboard clipMonitor = null;
303306
if (Settings.Default.trayPatchingEnabled) {
304307
bool skipFirst = true;
305-
void PatchClipboard(object s, SharpClipboard.ClipboardChangedEventArgs e) {
308+
monitor.ClipboardChanged += (s, e) => {
306309
if (skipFirst) { skipFirst = false; return; }
307310
Settings.Default.Reload(); // load modifications made from other instance
308311
if (!Settings.Default.trayPatchingEnabled) return; // allow to temporarily disable
309312
if (Settings.Default.continuousMode) return; // don't interfere with batch mode
313+
310314
if (PatchedClipboardContents() is IDataObject data) {
311315
// TODO: This is experimental (might impact performance, might break proprietary formats used internally by other programs, not 100% stable)
312-
// Temporarily pausing monitoring seams unstable with the SharpClipboard library, so close and re-create the monitor instead
313-
314-
// Stop monitoring and leave clipboard chain cleanly
315-
clipMonitor.MonitorClipboard = false;
316-
clipMonitor.StopMonitoring();
317-
// Re-write clipboard contents
318-
Clipboard.SetDataObject(data, false);
319-
// Create a new monitor to handle future updates
320-
clipMonitor = new SharpClipboard();
321-
clipMonitor.ClipboardChanged += PatchClipboard;
316+
monitor.CallWithoutClipboardMonitoring(() => {
317+
// Re-write clipboard contents with patched version
318+
Clipboard.SetDataObject(data, false);
319+
});
322320
}
323-
}
324-
clipMonitor = new SharpClipboard();
325-
clipMonitor.ClipboardChanged += PatchClipboard;
321+
};
322+
323+
monitor.StartClipboardMonitoring();
326324
}
327325

328326
// Tray icon
@@ -345,7 +343,7 @@ void PatchClipboard(object s, SharpClipboard.ClipboardChangedEventArgs e) {
345343
Application.Run();
346344

347345
// leave the clipboard monitoring chain in a clean way, otherwise the chain will break when the program exits
348-
clipMonitor?.StopMonitoring();
346+
monitor.StopClipboardMonitoring();
349347

350348
icon.Visible = false;
351349

PasteIntoFile/PasteIntoFile.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<PackageReference Include="LINQtoCSV" Version="1.5.0" />
5353
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
5454
<PackageReference Include="PDFsharp" Version="1.50.5147" />
55-
<PackageReference Include="SharpClipboard" Version="3.5.2" />
5655
</ItemGroup>
5756
<ItemGroup Condition="'$(Flavor)'=='Portable'">
5857
<PackageReference Include="PortableSettingsProvider" Version="0.2.4" />
@@ -76,7 +75,7 @@
7675
<ItemGroup>
7776
<Compile Include="ClipboardContents.cs" />
7877
<Compile Include="ExplorerUtil.cs" />
79-
<Compile Include="KeyboardHook.cs" />
78+
<Compile Include="SystemEventMonitor.cs" />
8079
<Compile Include="MasterForm.cs" />
8180
<Compile Include="RegistryUtil.cs" />
8281
<Compile Include="SeparableComboBox.cs">

0 commit comments

Comments
 (0)