Search code examples
counterautohotkeykeystroke

Autohotkey Application Keycounter


I am trying to create an application based Key counter. Below is my script

#UseHook
KeyCount=0
#If WinActive("Ahk_Class XLMAIN") Or WinActive("Ahk_Class Notepad")
Loop
{
Input, Key, L1 I V, , 
AscKey:=Asc(Key)
If (AscKey > 31 && AscKey < 127)
KeyCount:=KeyCount+1
}
#If
^+o::
msgbox %KeyCount%
return

As the WinActive commands says it should count keystrokes if active window is either Excel or notepad. But this script counts all keystrokes. Am I missing something?


Solution

  • You don't use the #if its only for hotkeys and hotstrings but you can use a normal if statment like this

    #UseHook
    KeyCount=0
    
    
    Loop
    {
        Input, Key, L1 I V
        If (WinActive("Ahk_Class XLMAIN") Or WinActive("Ahk_Class Notepad"))
        {
            AscKey:=Asc(Key)
            If (AscKey > 31 && AscKey < 127)
            KeyCount++
        }
    }
    
    
    ^+o::
    msgbox %KeyCount%
    return
    

    Hope it helps