1414using CommandLine . Text ;
1515using Microsoft . Toolkit . Uwp . Notifications ;
1616using PasteIntoFile . Properties ;
17- using WK . Libraries . SharpClipboardNS ;
1817#if PORTABLE
1918using 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
0 commit comments