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
24 changes: 9 additions & 15 deletions PasteIntoFile/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,21 @@
using System.Windows.Forms;
using PasteIntoFile.Properties;
using WK.Libraries.BetterFolderBrowserNS;
using WK.Libraries.SharpClipboardNS;

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

private SharpClipboard _clipMonitor;
private SystemEventMonitor eventMonitor = new SystemEventMonitor();
private bool _disableUiEvents = false;
private bool _topMostPreviousState = false;
private const string DYNAMIC_EXTENSION = "*"; // special value to determine extension dynamically

public SharpClipboard clipMonitor {
get {
if (_clipMonitor == null) _clipMonitor = new SharpClipboard();
return _clipMonitor;
}
}
protected override void OnFormClosed(FormClosedEventArgs e) {
// leave the clipboard monitoring chain in a clean way, otherwise the chain will break when the program exits
clipMonitor?.StopMonitoring();
eventMonitor?.StopClipboardMonitoring();
base.OnFormClosed(e);
}

Expand Down Expand Up @@ -118,8 +111,8 @@ public Dialog(
BringToFrontForced();

// register clipboard monitor
clipMonitor.ClipboardChanged += ClipboardChanged;
FormClosing += (s, e) => clipMonitor.ClipboardChanged -= ClipboardChanged;
eventMonitor.StartClipboardMonitoring();
eventMonitor.ClipboardChanged += ClipboardChanged;


} else {
Expand Down Expand Up @@ -286,7 +279,7 @@ private bool readClipboard() {



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

Expand Down Expand Up @@ -465,9 +458,10 @@ string save(bool overwriteIfExists = false, bool? clearClipboardOverwrite = fals
}

if (clearClipboardOverwrite ?? Settings.Default.clrClipboard) {
clipMonitor.MonitorClipboard = false; // to prevent callback during batch mode
Clipboard.Clear();
clipMonitor.MonitorClipboard = true;
// Prevent callback during batch mode
eventMonitor.CallWithoutClipboardMonitoring(() => {
Clipboard.Clear();
});
}

rememberExtension(content, comExt.Text);
Expand Down
133 changes: 0 additions & 133 deletions PasteIntoFile/KeyboardHook.cs

This file was deleted.

74 changes: 36 additions & 38 deletions PasteIntoFile/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using CommandLine.Text;
using Microsoft.Toolkit.Uwp.Notifications;
using PasteIntoFile.Properties;
using WK.Libraries.SharpClipboardNS;
#if PORTABLE
using Bluegrams.Application;
#endif
Expand Down Expand Up @@ -273,56 +272,55 @@ static int RunTray(ArgsTray args = null) {
}


var monitor = new SystemEventMonitor();

// Register hotkeys
KeyboardHook paste = new KeyboardHook();
paste.KeyPressed += (s, e) => {
var arg = new ArgsPaste();
arg.Directory = ExplorerUtil.GetActiveExplorerPath();
RunPaste(arg);
};
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.V);
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift, Keys.V);
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Control, Keys.V);
paste.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift | ModifierKeys.Control, Keys.V);

KeyboardHook copy = new KeyboardHook();
copy.KeyPressed += (s, e) => {
var files = ExplorerUtil.GetActiveExplorerSelectedFiles();
if (files.Count == 1) {
var arg = new ArgsCopy();
arg.FilePath = files.Item(0).Path;
RunCopy(arg);
} else {
MessageBox.Show(Resources.str_copy_failed_not_single_file, Resources.app_title, MessageBoxButtons.OK, MessageBoxIcon.Error);
monitor.KeyPressed += (s, e) => {
if (e.Key == Keys.V) {
// Paste hotkey
var arg = new ArgsPaste();
arg.Directory = ExplorerUtil.GetActiveExplorerPath();
RunPaste(arg);
} else if (e.Key == Keys.C) {
// Copy hotkey
var files = ExplorerUtil.GetActiveExplorerSelectedFiles();
if (files.Count == 1) {
var arg = new ArgsCopy();
arg.FilePath = files.Item(0).Path;
RunCopy(arg);
} else {
MessageBox.Show(Resources.str_copy_failed_not_single_file, Resources.app_title, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
};
copy.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.C);
// Paste hotkeys (with different modifier combinations)
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.V);
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift, Keys.V);
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Control, Keys.V);
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt | ModifierKeys.Shift | ModifierKeys.Control, Keys.V);
// Copy hotkey
monitor.RegisterHotKey(ModifierKeys.Win | ModifierKeys.Alt, Keys.C);


// Register clipboard observer for patching
SharpClipboard clipMonitor = null;
if (Settings.Default.trayPatchingEnabled) {
bool skipFirst = true;
void PatchClipboard(object s, SharpClipboard.ClipboardChangedEventArgs e) {
monitor.ClipboardChanged += (s, e) => {
if (skipFirst) { skipFirst = false; return; }
Settings.Default.Reload(); // load modifications made from other instance
if (!Settings.Default.trayPatchingEnabled) return; // allow to temporarily disable
if (Settings.Default.continuousMode) return; // don't interfere with batch mode

if (PatchedClipboardContents() is IDataObject data) {
// TODO: This is experimental (might impact performance, might break proprietary formats used internally by other programs, not 100% stable)
// Temporarily pausing monitoring seams unstable with the SharpClipboard library, so close and re-create the monitor instead

// Stop monitoring and leave clipboard chain cleanly
clipMonitor.MonitorClipboard = false;
clipMonitor.StopMonitoring();
// Re-write clipboard contents
Clipboard.SetDataObject(data, false);
// Create a new monitor to handle future updates
clipMonitor = new SharpClipboard();
clipMonitor.ClipboardChanged += PatchClipboard;
monitor.CallWithoutClipboardMonitoring(() => {
// Re-write clipboard contents with patched version
Clipboard.SetDataObject(data, false);
});
}
}
clipMonitor = new SharpClipboard();
clipMonitor.ClipboardChanged += PatchClipboard;
};

monitor.StartClipboardMonitoring();
}

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

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

icon.Visible = false;

Expand Down
3 changes: 1 addition & 2 deletions PasteIntoFile/PasteIntoFile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
<PackageReference Include="LINQtoCSV" Version="1.5.0" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
<PackageReference Include="PDFsharp" Version="1.50.5147" />
<PackageReference Include="SharpClipboard" Version="3.5.2" />
</ItemGroup>
<ItemGroup Condition="'$(Flavor)'=='Portable'">
<PackageReference Include="PortableSettingsProvider" Version="0.2.4" />
Expand All @@ -76,7 +75,7 @@
<ItemGroup>
<Compile Include="ClipboardContents.cs" />
<Compile Include="ExplorerUtil.cs" />
<Compile Include="KeyboardHook.cs" />
<Compile Include="SystemEventMonitor.cs" />
<Compile Include="MasterForm.cs" />
<Compile Include="RegistryUtil.cs" />
<Compile Include="SeparableComboBox.cs">
Expand Down
Loading
Loading