仅长按单键执行功能,不影响短按示例

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

Post Reply
nuance
Posts: 7
Joined: 23 Jan 2015, 21:01

仅长按单键执行功能,不影响短按示例

Post by nuance » 23 Jan 2015, 21:54

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
$T::
SetTimer, mainc, 10       ;10毫秒执行一次
return

mainc:
counts ++
GetKeyState, tstt, T, P      ;获取按键T的状态
if ( tstt = "U" )   ;当松开按键的时候
{
	SetTimer, mainc, off   ;取消定时器
	if (counts < 11)   ;如果counts在10以内,说明从按下到松开的时间为10*10=100毫秒,判断为短按。
	{
		GetKeyState, capstt, CapsLock, T       ;获取大小写锁定键状态
		if ( capstt = "D" )   ;如果锁定
		{
			Send T        ;发送T
		}
		else
		{
			Send t        ;发送t
		}
	}
	else            ;如果counts大于10,说明是长按,则执行下面的主程序
	{
		MsgBox, 即将戳开百度
		gosub, mainp    ;转到mainp
	}
	counts = 0        ;重置计数,清空变量,用于下次使用
	tstt := 
	capstt := 
	return
}
return

mainp:
Run, http://www.baidu.com
return
tmplinshi
Posts: 1600
Joined: 01 Oct 2013, 14:57

Re: 仅长按单键执行功能,不影响短按示例

Post by tmplinshi » 24 Jan 2015, 09:42

这样简单点

Code: Select all

$t::
	KeyWait, t
	If (A_TimeSinceThisHotkey > 300)
		SetTimer, mainp, -1
	Else
		SendInput, % GetKeyState("CapsLock", "T") ? "T" : "t"
Return

mainp:
	Run, http://www.baidu.com
Return
nuance
Posts: 7
Joined: 23 Jan 2015, 21:01

Re: 仅长按单键执行功能,不影响短按示例

Post by nuance » 24 Jan 2015, 22:27

tmplinshi wrote:这样简单点

Code: Select all

$t::
	KeyWait, t
	If (A_TimeSinceThisHotkey > 300)
		SetTimer, mainp, -1
	Else
		SendInput, % GetKeyState("CapsLock", "T") ? "T" : "t"
Return

mainp:
	Run, http://www.baidu.com
Return
哈,太棒了,原来还有keywait这个功能 :bravo:
我居然都没发现,到处找方法怎么实现。
keywait更省CPU啊。
cixiplc
Posts: 8
Joined: 18 Oct 2014, 15:52

Re: 仅长按单键执行功能,不影响短按示例

Post by cixiplc » 03 Apr 2015, 05:30

keywait 以后还是有点区别,因为原来是按键一触即发 keywait就得等释放按键后才运行
Tom2016
Posts: 1
Joined: 23 Apr 2016, 03:52

Re: 仅长按单键执行功能,不影响短按示例

Post by Tom2016 » 23 Apr 2016, 04:06

It is not working correctly, i found it will has issue when you press w and a, the "aw" will come out

$w::
KeyWait, w
If (A_TimeSinceThisHotkey > 500)
SetTimer, mainpw, -1
Else
SendInput, % GetKeyState("CapsLock", "W") ? "W" : "w"
Return
mainpw:
msgbox, w
Return
pigwei
Posts: 10
Joined: 13 Oct 2019, 02:36

Re: 仅长按单键执行功能,不影响短按示例

Post by pigwei » 13 Oct 2019, 22:04

@tmplinshi
老师 这段代码会影响打字的,其实创意很好,我很想实现,有什么办法吗
MAFFF
Posts: 1
Joined: 11 Mar 2020, 23:47

Re: 仅长按单键执行功能,不影响短按示例

Post by MAFFF » 11 Mar 2020, 23:50

cixiplc wrote:
03 Apr 2015, 05:30
keywait 以后还是有点区别,因为原来是按键一触即发 keywait就得等释放按键后才运行
稍稍改变即可做到一触即发,思路是很好的
Peter_Wang
Posts: 7
Joined: 01 Jun 2020, 12:22

Re: 仅长按单键执行功能,不影响短按示例

Post by Peter_Wang » 22 Jun 2020, 11:20

Tom2016 wrote:
23 Apr 2016, 04:06
It is not working correctly, i found it will has issue when you press w and a, the "aw" will come out
先给出一个working script摁住“p”的同时摁ijko可以移动caret,摁ed可以滚动屏幕。
这个脚本就不会有上述的问题了,原因是,脚本识别的机制是摁住po首先打出po两个字如果在200毫秒内p没有放手(keywait实现)就删掉po改成向右移动,其他键由于是中文打字不会有就直接认为是要触发热键了 :trollface:

Code: Select all

global PriorKeys:= []
ih := InputHook("I1VL10000")
ih.KeyOpt("{All}", "NV")
ih.OnKeyDown := Func("priorKeyHandler")
ih.Start()
priorKeyHandler(ih, vk, sc) {
	global PriorKeys
	vk := Format("vk{:x}", vk)
	sc := Format("sc{:x}", sc)
	name := GetKeyName(vk sc)
	PriorKeys.InsertAt(1,(name != "") ? name : "undefined")
    if(PriorKeys.Length()>700)
   {
 	PriorKeys.RemoveAt(300, 400)
   }
}
ih.OnEnd := Func("onEndHandler")
onEndHandler(ih) {
		ih.Start()
}

#Inputlevel 2
*p::
PriorKeys[1]:=""
v2:=1
;if (ErrorLevel=1&& A_ThisHotkey="*$p")
    ;{
    ;v2?v2:=0,v2:=1
 ;   SendInput,V
  ;  }else
    
if (A_ThisHotkey="*p")
{
    if GetKeyState("Shift")or GetKeyState("CapsLock", "T")
    Sendinput,P
    else if GetKeyState("q") or GetKeyState("[") 
        Sendinput, % (A_Priorkey="q"or A_Priorkey="[")?"{bs}P":"P"
    
    else
    {
        sendlevel 1
        Sendinput p
    
}}
KeyWait,p,T 0.2
if (Errorlevel=1)
{
    
    if (PriorKeys[1]="o")
      sendinput,{bs 2}{right}
    else if (PriorKeys[1]="i")
      sendinput,{bs 2}{up}


v1:=1
keywait,p
}
v1:=0
v2:=0
Return

#if  v1
*i::
if A_Priorkey=p
    sendinput,{bs}
SendInput,{Up 1}
return

*o::
if (A_Priorkey="p")
    sendinput,{bs}
SendInput,{right 1} 
return
#if  v2
*8::Esc
*e::
if A_Priorkey=p
    sendinput,{bs}
SendInput,{WheelUp 1}
return

*d::
if A_Priorkey=p
    sendinput,{bs}
SendInput,{WheelDown 1} 
return

*s::
if A_Priorkey=p
    sendinput,{bs}
SendInput,{ShiftDown}{Left}{ShiftUp}
return

*f::
if A_Priorkey=p
    sendinput,{bs}
SendInput,{ShiftDown}{Right}{ShiftUp}
return

*j::
if (A_Priorkey="p")
    sendinput,{bs}
SendInput,{left 1}
return
*k::
if A_Priorkey=p
    sendinput,{bs}
SendInput,{Down 1} 
return
#inputlevel 0
Post Reply

Return to “脚本函数”