简体   繁体   English

在粘贴时从剪贴板更改 output

[英]Change output from Clipboard on Paste

I frequently have to copy file paths from Windows 10 programs or dialogue boxes into my R code.我经常需要将 Windows 10 程序或对话框中的文件路径复制到我的R代码中。 Since R escapes backslashes, I always have to change the backslashes to forward slashes or double backslashes, so I am trying to program this problem away so that anytime I copy text and then use some special combination of shortcut keys to paste, it replaces my backslashes with double backslashes.由于R转义反斜杠,我总是必须将反斜杠更改为正斜杠或双反斜杠,所以我试图解决这个问题,以便每当我复制文本然后使用一些特殊的快捷键组合粘贴时,它会替换我的反斜杠带有双反斜杠。 So for example, if I copied "C:\windows\system32\drivers\etc" and then pressed some combination of keys like Ctrl+Alt+P (or even using the right click context menu) I'd like to change the pasted output to "C:\\windows\\system32\\drivers\\etc" instead of what was copied.因此,例如,如果我复制了“C:\windows\system32\drivers\etc”,然后按下了一些组合键,如 Ctrl+Alt+P(甚至使用右键单击上下文菜单),我想更改粘贴的output 到“C:\\windows\\system32\\drivers\\etc”,而不是复制的内容。 I thought I might be able to do this in PowerShell with something like this:我想我也许可以在 PowerShell 中这样做:

$copiedtext = Get-Clipboard;
$copiedtext = -join(-join('"', $copiedtext -replace "\\", '\\'), '"');
Set-Clipboard $copiedtext;
Write-Output $copiedtext | clip

But that only outputs my clipboard to the console (and doesn't work if I assigne a shortcut to my PowerShell program and assign shotcut keys to it).但这只会将我的剪贴板输出到控制台(如果我为我的 PowerShell 程序分配快捷方式并为其分配快捷键则不起作用)。 Can anyone recommend how to modify this to achieve what I want or recommend some other language that would allow Windows to always recognize some shortcut key combination of characters as my special paste operation?任何人都可以推荐如何修改它以实现我想要的或推荐一些其他语言,以允许 Windows 始终将某些快捷键组合识别为我的特殊粘贴操作? I'd like to do this without having to start up the program when I want to use it too, if possible.如果可能的话,我想在我想使用它时不必启动该程序来执行此操作。

Note: While the following approach works, it is:注意:虽然以下方法有效,但它是:

  • slow减缓
  • not fully reliable不完全可靠

Ultimately, you're better off looking for a solution based on a specialized tool such as AutHotkey .最终,您最好寻找基于诸如AutHotkey 之类的专用工具的解决方案。


  • Create a script, say $HOME\pasteEscaped.ps1 , with the following content:创建一个脚本,比如$HOME\pasteEscaped.ps1 ,其内容如下:
# Get text from the clipboard and escape '\' chars. as '\\'
$escapedText = (Get-Clipboard -Format Text) -replace '\\', '\\' 

# Helper COM object for sending keystrokes.
$sh = New-Object -ComObject WScript.Shell

# Switch back to the previously active application...
$sh.SendKeys("%{tab}")
Start-Sleep -Milliseconds 100
# ... and send the escaped text. additionally escaped for .SendKeys()
$sh.SendKeys(($escapedText -replace '[{}()+^%]', '{$&}'))
  • Create a shortcut file, eg on your desktop, with the following properties:使用以下属性创建一个快捷方式文件,例如在您的桌面上:

    • Target:目标:

       powershell.exe -executionpolicy bypass -noprofile -file %USERPROFILE%\pasteEscaped.ps1
    • Run: Minimized运行: Minimized

    • Shortcut key: (a shortcut key of your choice)快捷键:(您选择的快捷键)


Thereafter, when you press the shortcut key, the text is retrieved from the clipboard, the \ instances in it are doubled, and the resulting text is pasted into the foreground window, using simulated keyboard input.此后,当您按下快捷键时,将从剪贴板中检索文本,其中的\实例加倍,并使用模拟键盘输入将生成的文本粘贴到前景 window 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM