Chaoses-Ib/IbLogiSoftExt

I find a strange bug, the first code works and can catch long-press or double-click, etc., however, the second code only catch single-click, (I turn off all ahk scripts to test this one, and vkC5 can be any other key):

pytkr opened this issue · 8 comments

pytkr commented

I find a strange bug, the first code works and can catch long-press or double-click, etc., however, the second code only catch single-click, (I turn off all ahk scripts to test this one, and vkC5 can be any other key):

A. OK:


Morse(timeout = 400) {
   tout := timeout/1000
   key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
   
   Loop {
      t := A_TickCount
      KeyWait %key%
      Pattern .= A_TickCount-t > timeout
      KeyWait %key%,DT%tout%
      If (ErrorLevel)
         Return Pattern
   }
}



vkC5::
p := Morse(300)
If (p = "0")
{
  ToolTip, Short press
}
Else If (p = "00")
{
  ToolTip, Double short presses
}
Else If (p = "000")
{
  ToolTip, Triple short presses
}
Else If (p = "0000")
{
  ToolTip, Quadruple short presses
}
Else If (p = "01")
{
  ToolTip, Short+Long press
}
Else If (p = "1")
{
  ToolTip, Long press
}
Else
{
  ToolTip, Press pattern %p%
}
Sleep 500
ToolTip  ; Turn off the tip.
Return

^#r::
Reload  ; Assign Ctrl-Alt-R as a hotkey to restart the script.
Sleep 1000 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 4,, The script could not be reloaded. Would you like to open it for editing?
IfMsgBox, Yes, Edit

Return


Tab::
tooltip, OK
return 

B. Only add #IfWinActive ahk_exe notepad.exe and #IfWinActive, then failed:


Morse(timeout = 400) {
   tout := timeout/1000
   key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
   
   Loop {
      t := A_TickCount
      KeyWait %key%
      Pattern .= A_TickCount-t > timeout
      KeyWait %key%,DT%tout%
      If (ErrorLevel)
         Return Pattern
   }
}



vkC5::
p := Morse(300)
If (p = "0")
{
  ToolTip, Short press
}
Else If (p = "00")
{
  ToolTip, Double short presses
}
Else If (p = "000")
{
  ToolTip, Triple short presses
}
Else If (p = "0000")
{
  ToolTip, Quadruple short presses
}
Else If (p = "01")
{
  ToolTip, Short+Long press
}
Else If (p = "1")
{
  ToolTip, Long press
}
Else
{
  ToolTip, Press pattern %p%
}
Sleep 500
ToolTip  ; Turn off the tip.
Return

^#r::
Reload  ; Assign Ctrl-Alt-R as a hotkey to restart the script.
Sleep 1000 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 4,, The script could not be reloaded. Would you like to open it for editing?
IfMsgBox, Yes, Edit

Return

#IfWinActive ahk_exe notepad.exe
Tab::
tooltip, OK
return 
#IfWinActive

Originally posted by @pytkr in #1 (comment)

Sorry for not seeing your comment in #1. It seems that the notification mail was sent when I was absent.
According to my test, it is caused by keyboard hook. #IfWinActive will make AHK to install keyboard hook. And when keyboard hook turned on, KeyWait will ignore non-physically releasing and pressing. If you use #InstallKeybdHook instead, you will get the same result.
I'm now finding a way to solve this.

Luckily, I found that AHK has an internal flag to consider the key to be physical. And using it solves this bug on my computer.
Please test it: winmm.zip

By the way, I also found the code won't work when you open the script's main window. But it's the same when you use others keys (such as F12) rather than G-keys. Perhaps it's a feature or bug of AHK.

pytkr commented

Luckily, I found that AHK has an internal flag to consider the key to be physical. And using it solves this bug on my computer.
Please test it: winmm.zip

Thanks for your reply, new winmm.dll works, and I can catch long-press, double click, etc., however, combined hotkey cannot work, the hotkeys are not triggered:
A. ok:
vkC5, ^vkC5, !vkC5, +vkC5, #vkC5,
+^#!vkC5

B. failed:
Ctrl & vkC5, LCtrl & vkC5, RCtrl & vkC5,
Alt & vkC5, LAlt & vkC5, RAlt & vkC5,
Shift & vkC5, LShift & vkC5, RShift & vkC5,
LWin & vkC5,
~vkC5,
$vkC5,
*vkC5,
CapsLock & vkC5,
*^vkC5,
*!vkC5,
*+vkC5,
*#vkC5
~CapsLock & vkC5,

Thanks for your test. I noticed the combination hotkeys don't work yesterday, but I didn't find the reason. Then I decided to use Logitech's driver to send keys. This method can not only solve the problem of AHK, but also can be used to enhance AHK's game operation ability (as Logitech's driver is both low-level and official).

I'm now working on it with my friend. It may be basically finished today or tomorrow.

Oh...I've just read AHK's source code again, and found that in fact this bug can be fixed by changing your code.
Just add 'L' after every KeyWait, so that it won't check the key's physical state, but logical. In this way, you can still use v0.3 and be able to detect double-presses and define custom combination hotkeys. And here's the code:

Ctrl & vkC6::ToolTip, Ctrl+G6

vkC6 & vkC9::ToolTip, G6+G9
vkC9 & vkC6::ToolTip, G9+G6
; Another way:
/*
#if GetKeyState("vkC9") ; No "P"
vkC6::ToolTip, G9+G6
#if
*/

vkC7::
p := Morse(300)
If (p = "0")
  ToolTip, Short press
Else If (p = "00")
  ToolTip, Double short presses
Else If (p = "000")
  ToolTip, Triple short presses
Else If (p = "0000")
  ToolTip, Quadruple short presses
Else If (p = "01")
  ToolTip, Short+Long press
Else If (p = "1")
  ToolTip, Long press
Else
  ToolTip, Press pattern %p%
Sleep 500
ToolTip  ; Turn off the tip.
Return

;#InstallKeybdHook
#IfWinActive ahk_exe notepad.exe
vkC7::
tooltip, OK
return
#IfWinActive

^#r::
Reload  ; Assign Ctrl-Alt-R as a hotkey to restart the script.
Sleep 1000 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 4,, The script could not be reloaded. Would you like to open it for editing?
IfMsgBox, Yes, Edit
Return

Morse(timeout = 400) {
   tout := timeout/1000
   key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
   
   Loop {
      t := A_TickCount
      KeyWait %key%,L ; Must "L"
      Pattern .= A_TickCount - t > timeout
      KeyWait %key%,DLT%tout%
      If (ErrorLevel)
         Return Pattern
   }
}
pytkr commented

I just notice the L shows in KeyWait %key%,DLT%tout% and now it perfectly works, thank you!!