AHK-v2-script-converter icon indicating copy to clipboard operation
AHK-v2-script-converter copied to clipboard

Error with longpress.ahk

Open boromyr opened this issue 7 months ago • 4 comments

V1:

/*
[script info]
version     = 1.2
description = long press a key to Send, an alternate symbol or String
author      = davebrny
source      = https://github.com/davebrny/long-press
*/

#NoEnv
#NoTrayIcon
#Persistent
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
#MaxThreads 255
#MaxMem 4095
#MaxThreadsBuffer On
#SingleInstance force
ListLines Off
Process, Priority,, Realtime
SetBatchLines, -1
SetKeyDelay, 1, 1
SetMouseDelay, 1
SetDefaultMouseSpeed, 0
SetWinDelay, 1
SetControlDelay, 1
SendMode Event
SetTitleMatchMode, 2

Menu, tray, add,

IniRead, press_duration, % a_scriptDir "\LongPress.Settings.ini" , settings, press_duration
IniRead, spam_start, % a_scriptDir "\LongPress.Settings.ini" , settings, spam_start
; IniRead, active_list, % a_scriptDir "\LongPress.Settings.ini" , settings, active_list

FileRead, file_contents, % a_scriptDir "\LongPress.Settings.ini"
StringGetPos, pos, file_contents, % "[long press]" ;# get ini section manually
StringMid, ini_section, file_contents, pos + 13

loop, parse, ini_section, `n, `r ;# get ini key and value
{
    if (a_loopField = "")
        continue

    occurrence := (inStr(a_loopField, "=", , 1) = 1) ? ("L2") : ("") ; (if key name is the equals symbol, then L2)
    StringGetPos, pos, a_loopField, =, % occurrence
    StringMid, ini_key, a_loopField, pos, , L
    StringMid, ini_value, a_loopField, pos + 2
    ini_key := trim(ini_key)
    ini_value := trim(regExReplace(ini_value, "^;.*$|\s+;.*$")) ; strip comments

    if (ini_value = "")
        continue

    scancode := format("SC{:x}", getKeySC(ini_key)) ; convert key to scancode
    LP_%scancode% := ini_value ; save alt key in LP_SC2B
    hotkey, ~%ini_key%, long_press
    Sleep, 20
}

file_contents := ""
ini_section := ""

return
; end of auto-execute ------------------------------------------------
long_press:
    stringTrimLeft, key, a_thisHotkey, 1 ; Rimuove il simbolo ~
    varDidReplace := false
    while ((GetKeyState(key, "P")) and (A_TimeSincePriorHotkey > 100)) {
        keyWait, % key
        if (A_TimeSinceThisHotkey > press_duration && A_TimeSinceThisHotkey < spam_start && !varDidReplace) {
            varDidReplace := true
            scancode := "LP_" . format("SC{:x}", getKeySC(key))
            stored_value := %scancode%
            if (IsLabel(stored_value)) {
                GoSub, % stored_value
            } else {
                if (key = "VKBA") {
                    SendInput, % "{backspace}" . stored_value . "{}}{Left}"
                } else {
                    SendInput, % "{backspace}" . stored_value
                }
            }
        }
        Sleep, 20
    }
return


delete_previous_word:
    Send, ^{backspace}
return

delete_next_word:
    Send, ^{Del}
return

insert_line_before:
    Send, {backspace} ; delete long press input
    Send, {home}{enter}{up}
return

V2 (Converted):

/*
[script info]
version     = 1.2
description = long press a key to Send, an alternate symbol or String
author      = davebrny
source      = https://github.com/davebrny/long-press
*/

; REMOVED: #NoEnv
#NoTrayIcon
Persistent
A_MaxHotkeysPerInterval := 99000000
A_HotkeyInterval := 99000000
KeyHistory(0)
#MaxThreads 255
; REMOVED: #MaxMem 4095
#MaxThreadsBuffer true
#SingleInstance force
ListLines(false)
ErrorLevel := ProcessSetPriority("Realtime")
; REMOVED: SetBatchLines, -1
SetKeyDelay(1, 1)
SetMouseDelay(1)
SetDefaultMouseSpeed(0)
SetWinDelay(1)
SetControlDelay(1)
SendMode("Event")
SetTitleMatchMode(2)

tray:= A_TrayMenu
tray.add("")

press_duration := IniRead(a_scriptDir "\LongPress.Settings.ini", "settings", "press_duration")
spam_start := IniRead(a_scriptDir "\LongPress.Settings.ini", "settings", "spam_start")
; IniRead, active_list, % a_scriptDir "\LongPress.Settings.ini" , settings, active_list

file_contents := Fileread(a_scriptDir "\LongPress.Settings.ini")
pos := InStr(file_contents, "[long press]") - 1 ;# get ini section manually
ini_section := SubStr(file_contents, pos + 13)

Loop Parse, ini_section, "`n", "`r" ;# get ini key and value
{
    if (a_loopField = "")
        continue

    occurrence := (InStr(a_loopField, "=", , 1) = 1) ? ("L2") : ("") ; (if key name is the equals symbol, then L2)
    pos := InStr(a_loopField, "=",, (0)+1, occurrence) - 1
    ini_key := SubStr(SubStr(a_loopField, 1, pos), -)
    ini_value := SubStr(a_loopField, pos + 2)
    ini_key := trim(ini_key)
    ini_value := trim(RegExReplace(ini_value, "^;.*$|\s+;.*$")) ; strip comments

    if (ini_value = "")
        continue

    scancode := format("SC{:x}", getKeySC(ini_key)) ; convert key to scancode
    LP_%scancode% := ini_value ; save alt key in LP_SC2B
    Hotkey("~" ini_key, long_press)
    Sleep(20)
}

file_contents := ""
ini_section := ""

return
; end of auto-execute ------------------------------------------------
long_press(ThisHotkey)
{ ; V1toV2: Added bracket
global ; V1toV2: Made function global
    key := SubStr(a_thisHotkey, (1)+1) ; Rimuove il simbolo ~
    varDidReplace := false
    while ((GetKeyState(key, "P")) and (A_TimeSincePriorHotkey > 100)) {
        ErrorLevel := !KeyWait(key)
        if (A_TimeSinceThisHotkey > press_duration && A_TimeSinceThisHotkey < spam_start && !varDidReplace) {
            varDidReplace := true
            scancode := "LP_" . format("SC{:x}", getKeySC(key))
            stored_value := %scancode%
            if (IsLabel(stored_value)) {
                % stored_value()
            } else {
                if (key = "VKBA") {
                    SendInput("{backspace}" . stored_value . "{}}{Left}")
                } else {
                    SendInput("{backspace}" . stored_value)
                }
            }
        }
        Sleep(20)
    }
return
} ; V1toV2: Added Bracket before label


delete_previous_word:
    Send("^{backspace}")
return

delete_next_word:
    Send("^{Del}")
return

insert_line_before:
    Send("{backspace}") ; delete long press input
    Send("{home}{enter}{up}")
return

V2 (Errors):

    ini_key := SubStr(SubStr(a_loopField, 1, pos), -) ; line 48

                % stored_value() ; line 79

Converting the script produces these two errors. To test the script use: LongPress.Settings.ini.txt

boromyr avatar Jul 14 '24 13:07 boromyr