请教如何实现Ctrl+C与Ctrl+C+C功能不同 Topic is solved

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

AAHKUser
Posts: 21
Joined: 19 Jun 2020, 04:31

请教如何实现Ctrl+C与Ctrl+C+C功能不同

Post by AAHKUser » 26 Jun 2020, 01:18

我目前参考1Hour实现了按Ctrl+C统计复制的字数的功能,但我现在想实现Ctrl+C复制(单击C),只有按Ctrl+C+C才复制并显示字数(快速双击C)

目前只查到似乎可以用KeyWait或者TimeSet实现,但我写出来时灵时不灵,请教大家该如何修改,谢谢。

Code: Select all

$^c::
if (CtrlC_presses > 0) ; SetTimer 已经启动, 所以我们记录键击.
{
    CtrlC_presses += 1
    return
}
; 否则, 这是新开始系列中的首次按下. 把次数设为 1 并启动
; 计时器:
CtrlC_presses := 1
SetTimer, KeyCtrlC, -400 ; 在 400 毫秒内等待更多的键击.
return

KeyCtrlC:
if (CtrlC_presses = 1) ; 此键按下了一次.
{
    Send ^c
}
else if (CtrlC_presses = 2) ; 此键按下了两次.
{
  clip=%Clipboard%                
  numofwords=0  
  StringSplit,words,clip    
  ;words :=StrSplit(Clip)    ;
    loop  %words0%         ; words0,
    {
      If words%A_Index%<>                    
        numofwords+=1
    }
  ToolTip Word Count: %numofwords%
  Sleep 2000
  RemoveToolTip:
  ToolTip
  numofwords=0
  Return
}

; 不论触发了上面的哪个动作, 都对 count 进行重置
; 为下一个系列的按下做准备:
CtrlC_presses := 0
return
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 请教如何实现Ctrl+C与Ctrl+C+C功能不同  Topic is solved

Post by tmplinshi » 26 Jun 2020, 02:30

试试这样:

Code: Select all

~^c::
	if IsDoubleClick()
		ToolTip(StrLen(Clipboard),,,, 2000)
Return

IsDoubleClick() {
	static doubleClickTime := DllCall("GetDoubleClickTime")
	KeyWait, % LTrim(A_ThisHotkey, "~")
	return (A_ThisHotKey = A_PriorHotKey) && (A_TimeSincePriorHotkey <= doubleClickTime)
}

ToolTip(Text := "", X := "", Y := "", WhichToolTip := 1, Timeout := "") {
	ToolTip, % Text, X, Y, WhichToolTip

	If (Timeout) {
		RemoveToolTip := Func("ToolTip").Bind(,,, WhichToolTip)
		SetTimer, % RemoveToolTip, % -Timeout
	}
}
AAHKUser
Posts: 21
Joined: 19 Jun 2020, 04:31

Re: 请教如何实现Ctrl+C与Ctrl+C+C功能不同

Post by AAHKUser » 28 Jun 2020, 08:17

@tmplinshi
非常感谢您的分享!功能很完美,设计的好简洁,我有些地方还看不太懂,正在对这资料看:)
AAHKUser
Posts: 21
Joined: 19 Jun 2020, 04:31

Re: 请教如何实现Ctrl+C与Ctrl+C+C功能不同

Post by AAHKUser » 28 Jun 2020, 16:43

@tmplinshi
发现现在空格也会算为字数,我做了一点点点修改,加了一行

Code: Select all

~^c::     
Clip:=StrReplace(Clipboard, A_Space, "")    ;去掉空格
	if IsDoubleClick()
		ToolTip(StrLen(Clip)" 个字",,,, 2000)
Return

IsDoubleClick() {
	static doubleClickTime := DllCall("GetDoubleClickTime")
	KeyWait, % LTrim(A_ThisHotkey, "~")
	return (A_ThisHotKey = A_PriorHotKey) && (A_TimeSincePriorHotkey <= doubleClickTime)
}

ToolTip(Text := "", X := "", Y := "", WhichToolTip := 1, Timeout := "") {
	ToolTip, % Text, X, Y, WhichToolTip

	If (Timeout) {
		RemoveToolTip := Func("ToolTip").Bind(,,, WhichToolTip)
		SetTimer, % RemoveToolTip, % -Timeout
	}
}
Post Reply

Return to “请求帮助”