Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with longpress.ahk #254

Open
boromyr opened this issue Jul 14, 2024 · 4 comments
Open

Error with longpress.ahk #254

boromyr opened this issue Jul 14, 2024 · 4 comments

Comments

@boromyr
Copy link

boromyr commented Jul 14, 2024

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

@Banaanae
Copy link
Collaborator

            % stored_value() ; line 79

I can't find a fix with our current Label -> Func conversion, but using this will fix it #236 (comment)

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

StringMid conversion seems to be incorrect when L param is present, ill work on that now

@boromyr
Copy link
Author

boromyr commented Jul 15, 2024

Do you mean it this way?

            if (IsLabel(stored_value)) {
                Goto("stored_value")
            } else {

Furthermore, the script converted with the latest commit has this error:

Error: Parameter #5 of InStr requires a Number, but received an empty string.

044: Continue
046: occurrence := (InStr(a_loopField, "=", , 1) = 1) ? ("L2") : ("")
▶ 047: pos := InStr(a_loopField, "=",, (0)+1, occurrence) - 1
048: ini_key := SubStr(a_loopField, 1, pos)
049: ini_value := SubStr(a_loopField, pos + 2)

@Banaanae
Copy link
Collaborator

Do you mean it this way?

Not really, GoSub means it runs to the specified label, then goes back to where the GoSub is (think subroutines)

The problem with this code is currently the label in the GoSub gets converted to a function (which is better practise than labels, but doesn't work with falling in). We aren't exactly sure how to handle this, whether to push practicality or functionality (despite not using functions haha). You can track this at #236

Furthermore, the script converted with the latest commit has this error:

StringMid is not very easy to convert :) I look at it soon

@Banaanae
Copy link
Collaborator

▶ 047: pos := InStr(a_loopField, "=",, (0)+1, occurrence) - 1

Just started looking into this, but there might be a reason why it doesn't work
Comments from the code:

         ; else then a variable was passed (containing the "L#|R#" string),
         ;      or literal text converted to expr, something like:   "L" . A_Index
         ; output something anyway even though it won't work, so that they can see something to fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants