修饰键映射问题求教 Topic is solved

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

Moderators: tmplinshi, arcticir

emacs25
Posts: 6
Joined: 09 Sep 2020, 08:18

修饰键映射问题求教

Post by emacs25 » 09 Sep 2020, 08:38

刚接触autohoktey没多久,想实现如下功能
单击Lalt输出空格,按住不放为LControl,参考网络上的代码如下:

Code: Select all

Lalt::
Send {LControl Down}
KeyWait, Lalt
Send {LControl Up}
If ( A_PriorKey = "Lalt")
{
Send {SPC}
}
return
运行结果不成功,请教各位大神,代码存在什么问题,要如何修正呢,感谢!
Last edited by joedf on 09 Sep 2020, 08:46, edited 1 time in total.
Reason: added [code] tags
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 修饰键映射问题求教  Topic is solved

Post by tmplinshi » 11 Sep 2020, 03:29

Code: Select all

; 单击 LAlt 输出空格,按住不放为 LControl
LAlt::
	KeyWait, LAlt, T0.3 ; 等待 LAlt 松开, 等待时间 0.3 秒
	if ErrorLevel { ; 超时
		LAlt::LControl
	} else {
		Send {Space}
	}
Return
emacs25
Posts: 6
Joined: 09 Sep 2020, 08:18

Re: 修饰键映射问题求教

Post by emacs25 » 11 Sep 2020, 05:58

感谢大神的解答,已经测试成功

另外求教我上面的代码哪里有问题呢
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 修饰键映射问题求教

Post by tmplinshi » 11 Sep 2020, 06:52

首先, 空格的名称是 {Space}, 而不是 {SPC}
然后, 可以用 A_TimeSinceThisHotkey 来判断长按短按

Code: Select all

^r::MsgBox  Ctrl + r
^!r::MsgBox Alt + Ctrl + r

Lalt::
   Send {LControl Down}
   KeyWait, Lalt
   Send {LControl Up}
   If (A_TimeSinceThisHotkey < 300)
   {
      Send {Space}
   }
return
以上代码可以区分长按短按, 但是当你长按住alt, 再按 r, 会发现实际上触发的是 Alt + Ctrl + r, 而不是预想中的 Ctrl + r.
另外代码逻辑与你的描述也不一致. 你的描述是 "单击变空格, 长按变 ctrl", 但是代码不管长按短按都会按 ctrl
emacs25
Posts: 6
Joined: 09 Sep 2020, 08:18

Re: 修饰键映射问题求教

Post by emacs25 » 11 Sep 2020, 08:18

懂了,谢谢大神的仔细解释,再次感谢
emacs25
Posts: 6
Joined: 09 Sep 2020, 08:18

Re: 修饰键映射问题求教

Post by emacs25 » 13 Sep 2020, 04:37

tmplinshi wrote:
11 Sep 2020, 03:29

Code: Select all

; 单击 LAlt 输出空格,按住不放为 LControl
LAlt::
	KeyWait, LAlt, T0.3 ; 等待 LAlt 松开, 等待时间 0.3 秒
	if ErrorLevel { ; 超时
		LAlt::LControl
	} else {
		Send {Space}
	}
Return
再请教一个问题,根据如上代码,我在使用过程当中发现一个奇怪现象

当长按键的时候,表现没有任何问题,但是当短按键的时候,经常性的出现无响应

我以为是键盘的灵敏度出了问题,换了四个键盘,还是同样的情况

然后我把代码编译成独立Exe,使用过程还是同样的表现

想请问各位,这种情况是正常现象还是有问题,如果有问题的话,该如何去优化
Post Reply

Return to “请求帮助”