production-taskbar-client / bin / win-key-server.ahk
win-key-server.ahk
Raw
/************************************************************************
 *         Windows keyboard hook server for taskbar, autohotkey v2
 ************************************************************************/

/************************ Compiler directives ***************************/
VERSION := "2.0.0"
;@Ahk2Exe-Let version = %A_PriorLine~U)^(.+"){1}(.+)".*$~$2%
NAME := "winkeyserver"
;@Ahk2Exe-Let name = %A_PriorLine~U)^(.+"){1}(.+)".*$~$2%
;@Ahk2Exe-SetName %U_name%
;@Ahk2Exe-SetVersion %U_version%
;@Ahk2Exe-SetMainIcon .\winkeyicon.ico
;@Ahk2Exe-SetDescription Autohotkey keybdhook for taskbar
;@Ahk2Exe-SetCopyright Mykyta.Nehrych@sebn.com 2023
;@Ahk2Exe-SetCompanyName SEBN.UA CHE
;@Ahk2Exe-Base ..\v2\AutoHotkey32.exe

/************************** Runtime directives ***************************/
#Requires AutoHotkey v2.0
#SingleInstance Force
Persistent(true)
#UseHook
A_MaxHotkeysPerInterval := 9999    ; default 70
A_HotkeyInterval := 2000    ; default 2000
InstallKeybdHook(true, true)
if A_IsCompiled {
    #NoTrayIcon
    #Warn All, Off
} else {
    #Warn All, StdOut
}
ListLines false
SendMode "Input"
SetWorkingDir A_ScriptDir

/*************************** Startup checkup *****************************/
if (A_Args.Length < 1) {
    MsgBox("Standalone usage restricted. Taskbar manage this itself.", , "64 T10")
    ExitApp
}

/**************************************************************************/
logfile := NAME . ".log"
for (a in A_Args) {
    arr := StrSplit(a, "=")
    if (arr[1] = "logdir")
        if DirExist(arr[2])
            logfile := arr[2] . "\" . logfile

}
LogError(exception, mode) {
    if (FileExist(logfile)) {
        if (FileGetSize(logfile, "M") >= 1) {
            FileDelete(logfile)
        }
    }

    datetime := FormatTime(, "dd.MM.yyyy HH:mm:ss")
    text := "[" . datetime . "] Error on line " exception.Line ": " exception.Message " in " exception.What ". Stack:`n" exception.Stack "`n"
    FileAppend(text, logfile, "UTF-8")
    return -1
}

handler(ThisHotkey) {
    data := "{`"char`": `"" . SubStr(ThisHotkey, -1) . "`", `"timeSincePressed`": `"" . (A_TimeSincePriorHotkey ?? "-1") . "`"}`n"
    FileAppend(data, "*", "UTF-8")    ; send to attached stdout

}

ScannerHook(option) {
    Loop 26
        Hotkey "*$~" . Chr(A_Index + 64), handler, option
    Loop 10
        Hotkey "*$~" . A_Index - 1, handler, option
}

OnError(LogError, -1)
ScannerHook("on")