不使用剪贴板获取所选文本 [英] Get Selected Text Without Using the Clipboard
问题描述
我正在尝试在 AutoHotKey 中创建一个非常基本的文本包装器,以便在编程时使用.我让它使用剪贴板来复制所选文本,修改它,然后粘贴它,但我试图避免使用剪贴板,因为它不能与我的剪贴板管理器很好地结合使用.有谁知道如何做到这一点?
I am trying to create a pretty basic text wrapper in AutoHotKey for use when programming. I got it to work using the clipboard to copy the selected text, modify it, then paste it, but I am trying to refrain from using the clipboard since it does not work well in conjunction with my Clipboard Manager. Does anyone know how to do this?
!r:: ;Alt+R+%Char% = Wrap Text with Input Characters
ClipSave := ClipboardAll
Send ^c
Input, Char, L1
if ("" . Char = "{")
{
clipboard = {%clipboard%}
}
else if ("" . Char = "[")
{
clipboard = [%clipboard%]
}
else if ("" . Char = "(")
{
clipboard = (%clipboard%)
}
else
{
clipboard = %Char%%clipboard%%Char%
}
StringReplace, clipboard, clipboard,%A_SPACE%",", All
Send ^v
Clipboard := ClipSave
ClipSave =
return
注意:我已经看到ControlGet, text, Selected
并尝试实现它,但它没有工作(没有错误,只是没有操作).如果有人对此有解决方案,那将解决我的问题.
Note: I have seen ControlGet, text, Selected
and attempted to implement it, but it did not work (no error, just no action). If anyone has a solution to this, that would fix my issue.
推荐答案
感谢 AutoHotkey 论坛上的 Solar 提出以下解决方案
这种方法有点不可靠,因为它只适用于特定的控件类型.但是,它可能是您正在寻找的解决方案,因为它根本不使用剪贴板.
This method is a bit unreliable, as it will only work for specific control types. However, it may be the solution you are looking for, as it does not use the clipboard at all.
WinActive("A") ; sets last found window
ControlGetFocus, ctrl
if (RegExMatch(ctrl, "A)Editd+")) ; attempt copying without clipboard
ControlGet, text, Selected,, %ctrl%
}
这是一个建议的解决方案,它尝试使用 ControlSend 复制文本,但如果需要,则退回到使用剪贴板作为备份.
Here's a proposed solution which attempts to copy text with ControlSend, but falls back to using the clipboard as a backup if needed.
WinActive("A") ; sets last found window
ControlGetFocus, ctrl
if (RegExMatch(ctrl, "A)Editd+")) ; attempt copying without clipboard
ControlGet, text, Selected,, %ctrl%
else { ; fallback solution
clipboardOld := Clipboard ; backup clipboard
Send, ^c ; copy selected text to clipboard
if (Clipboard != clipboardOld) {
text := Clipboard ; store selected text
Clipboard := clipboardOld ; restore clipboard contents
}
}
MsgBox % text
这篇关于不使用剪贴板获取所选文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!