Script makes pc lag Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

Script makes pc lag

21 Oct 2021, 16:21

Hi,

Can someone please tell me why when my script is running it makes my pc lag even though I'm not calling any hotkey from it. When it is not running : the pc goes back to its normal speed. I tried testing several times (with my script running vs closed) and its always the same result. I tried with both .exe and .ahk extention (compiled/uncompiled): same result.

Thank you!

Code: Select all

SetTitleMatchMode, 2
#SingleInstance Force
#Persistent
TrayTip, AutoHotKey, Started, 1
;SoundBeep, 300, 150
Return

^#!r:: ; Save and Reload script
Send, ^s ; To save a changed script
Sleep, 300 ; give it time to save the script
Reload
Return

global CurrentClipBoard, NAM, Qc, nom, prenom, genre

~t::
	{
    SetTitleMatchMode, 1
	If WinActive("1-Patient -")
		{
		Send {AppsKey}
		Sleep, 700
		Send {up}{up}{up}
		Sleep, 100
		Send {up}{up}{up}
		Sleep, 100
		Send {Enter}
		}
	SetTitleMatchMode, 2
	return
	}

^LButton:: ;ctrl+Left Mouse Click
	{
	hotkey,% a_thisHotkey,off
	Send {RButton}
	Sleep, 50
    Send {TAB}
    Sleep, 50
    Send {F4}
	Sleep, 50
	Send {Home} ;white
	Sleep, 50
	Send {Enter}
	hotkey,% a_thisHotkey,on
	Return
	}

^RButton::
	{
	hotkey,% a_thisHotkey,off
	Send {RButton}
	Sleep, 50
	Send {TAB}
    Sleep, 50
    Send {F4}
	Sleep, 50
	Send {Home}{Down}
	Sleep, 50
	Send {Down} ;Green
	Sleep, 50
	Send {Enter}
	hotkey,% a_thisHotkey,on
	Return
	}

!LButton:: ;alt+Left Mouse Click
	{
	hotkey,% a_thisHotkey,off
	Send {RButton}
	Sleep, 50
	Send {TAB}
    Sleep, 50
    Send {F4}
	Sleep, 50
	Send {End}
	Sleep, 50
	Send {Up}
	Sleep, 50
	Send {Up}
    Sleep, 50
	Send {Up} ;Red
	Sleep, 50
	Send {Enter}
	hotkey,% a_thisHotkey,on
	Return
	}

^#c::
	{
	If WinActive("1-Patient - ")
		{
		CurrentClipBoard = %ClipBoard%
		Send {Ctrl Down}l{Ctrl Up}
		Send {TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}
		Sleep, 100
		Send {Ctrl Down}{Shift Down}{right}{right}{Ctrl Up}{Shift Up}
		Sleep, 100
		Send {Ctrl Down}c{Ctrl Up}
		Sleep, 100
		Qc = %ClipBoard%
		Send {TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}
		Sleep, 100
		Send {Ctrl Down}{Shift Down}{right}{right}{Ctrl Up}{Shift Up}
		Sleep, 100
		Send {Ctrl Down}c{Ctrl Up}
		NAM = %ClipBoard%
		ClipBoard = %CurrentClipBoard%
		return
		}
	}

#c::
	{
	If WinActive("Patient - ")
		{
		Send {Home}
		Sleep, 100
		CurrentClipBoard = %ClipBoard%
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}
		Sleep, 100
		Send {Ctrl Down}c{Ctrl Up}
		NAM = %ClipBoard%
		Send {Ctrl Down}l{Ctrl Up} ;remonte à nom
		Sleep, 100
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}
		Sleep, 100
		Send {Ctrl Down}c{Ctrl Up} ;copie nom
		Sleep, 100
		nom = %ClipBoard%
		Send {TAB}
		Sleep, 250
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}
		Send {Ctrl Down}c{Ctrl Up} ;copie prenom
		Sleep, 100
		prenom = %ClipBoard%
		Send {TAB}
		Sleep, 250
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}
		Sleep, 100
		Send {Ctrl Down}c{Ctrl Up} ;copie genre
		Sleep, 100
		genre = %ClipBoard%
		Send {TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}
		Sleep, 100
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}
		Sleep, 100
		Send {Ctrl Down}c{Ctrl Up}
		Sleep, 100
		Send {TAB}
		Qc = %ClipBoard%
		Sleep, 100
		ClipBoard = %CurrentClipBoard%
		}
	else If WinActive("1-(Pat Search)")
		{
		CurrentClipBoard = %ClipBoard%
		Send {Enter}
		Sleep, 1300
		Send {Ctrl Down}l{Ctrl Up}
		Send {TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}
		Sleep, 100
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}{AppsKey}
		Sleep, 200
		Send {Down}{Down}{Down}
		Qc = %ClipBoard%
		Send {TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}
		Sleep, 100
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}{AppsKey}
		Sleep, 200
		Send {Down}{Down}{Down}
		NAM = %ClipBoard%
		ClipBoard = %CurrentClipBoard%
		}
	else If WinActive("ABC ")
		{
		Sleep, 200
		Send {BS}
		Sleep, 100
		Send %Qc%{space}
		Sleep, 700
		Send {Down}
		Sleep, 100
		Send {Enter}
		}
	else If WinActive("recherche - ")
		{
SetTitleMatchMode, 1
		Send %NAM%
		x = Rand(300, 350) ; - Random integer between X and Y
		Sleep, %x%
		Send {enter}
		x = Rand(300, 350) ; - Random integer between X and Y
		Sleep, %x%
		Send {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}
		Send {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}
		x = Rand(100, 150) ; - Random integer between X and Y
		Sleep, %x%
		Send {Tab}
		x = Rand(100, 150) ; - Random integer between X and Y
		Sleep, %x%
		;Send {Tab}
		x = Rand(100, 150) ; - Random integer between X and Y
		Sleep, %x%
		Send {Tab}
		KeyWait, Tab, D T10
		Send {enter}
SetTitleMatchMode, 2
		}
	else If WinActive("Save As")
		{
		Send %NAM%_
		Send, %A_YYYY%-%A_MM%-%A_DD%_
		FormatTime, Time, ,hh:mmtt
		SendInput, %Time%
		Sleep, 100
		Send {enter}
		Sleep, 100

		}
	return
	}


~Tab::
{
WinGetActiveTitle, currectWindow
if (InStr(currectWindow, "https://(ADRESS HIDDEN FOR CONFIDENTIALITY REASON)" , false,1))
{
	if (NAM)
	{
	ControlClick, x280 y79, https://(ADRESS HIDDEN FOR CONFIDENTIALITY REASON)
	Sleep, 2000
	Send %NAM%_
	Send, %A_YYYY%-%A_MM%-%A_DD%_
	FormatTime, Time, ,hhmmsstt
	SendInput, %Time%
	Sleep, 100
	Send {enter}
	x = Rand(2000, 2500) ; - Random integer between X and Y
	Sleep, %x%
	;WinActivate ahk_class IEFrame
	;Sleep, 100
	if WinExist("https://(ADRESS HIDDEN FOR CONFIDENTIALITY REASON)")
		WinClose ; Use the window found by WinExist.
	WinActivate ahk_class IEFrame
	Sleep, 100

	;Send {Alt Down}{F4}{Alt Up}
	Sleep, 300
	Send {Shift Down}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Shift Up}{enter}
	Sleep, 100
	Send {Shift Down}{Tab}{Tab}{Shift Up}{enter}
	Sleep, 100
	Send {Right}
	}
}
return
}

~d::
	{
    SetTitleMatchMode, 1
	If WinActive("1-Modify ")
		{
		Send {Ctrl Down}e{Ctrl Up}
		}
	SetTitleMatchMode, 2
	return
	}

~^c:: ;ctrl+c
    {
    ;SetTitleMatchMode, 1
    If WinActive("1-Patient -")
		{
		Send {Home}
		Sleep, 100
		Send {Ctrl Down}{Shift Down}{End}{Ctrl Up}{Shift Up}
		Sleep, 100
		Send {Ctrl Down}c{Ctrl Up}
		}
    ;SetTitleMatchMode, 2
    Return
    }


^+Enter:: ;ctrl+Shift+Enter
^+NumpadEnter::
    {
    SetTitleMatchMode, 1
    If WinActive("1-Patient -")
		{
		Send {Shift Down}{F3}{Shift Up}
		}
    SetTitleMatchMode, 2
    Return
    }

^Enter:: ;ctrl+Enter
^NumpadEnter::
	{
	If WinActive("ABC ")
		{
		Send {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}
		Sleep, 100
		Send {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}
		Sleep, 100
		Send {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}
		Sleep, 100
		Send {Enter}
		Sleep, 700
		Send {Shift Down}{Tab}{Shift Up}
		}
	else If WinActive("Patient")
		{
		Send d
		Sleep, 300
		Send {End}
		Sleep, 300
		Send m
		}
	return
	}

+Enter::
	{
	If WinActive("ABC ")
		{
		Send {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}
		Sleep, 100
		}
	return
	}

#z::
	if(%nomPatient%)
		{
		if (WinExist("Courriel - Outlook"))
			{
			WinActivate, Courriel - Outlook
			Sleep, 500
			Send n
			Sleep, 500
			Send compass
			Sleep, 500
			Send {Tab}
			Sleep, 300
			Send {Tab}{Tab}
			Sleep, 300
			Send {Tab}{Tab}{Tab}
			Sleep, 100
			Send %QC%
			Sleep, 100
			Send {ctrl Down}{left}{left}{ctrl Up}
			Sleep, 100
			Send {del}{del}{del}2
			Sleep, 100
			Send {End} - Suivi 
			Sleep, 100
			Send {Tab}
			Sleep, 100
			Send Bonjour
			}
		Else
			MsgBox, "Veuillez- SVP ouvrir"
	return
	}
User avatar
mikeyww
Posts: 26880
Joined: 09 Sep 2014, 18:38

Re: Script makes pc lag

21 Oct 2021, 16:59

Sounds like an antivirus issue (possibly). I tried your script and noticed no impact on CPU load, though the script has some bugs.
RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: Script makes pc lag

22 Oct 2021, 05:36

Not sure if it has anything to do with lagging problem, but one thing that jumped out at me was the mis-placement of some Return statements. In the hotkeys "^#c" and "#z", the Return statements are contained within If() blocks which means that if the condition evaluates to false, the return for that key is never seen. As I understand the docs, Return is only implied when the hotkey and its respective (single line of) code is all on one line.

Russ
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

Re: Script makes pc lag

22 Oct 2021, 06:34

I will correct those 2 returns.

I will check if I can deactivate the antivirus and see if that is the cause. Feel free to tell me where are bugs (and correction if possible), I would appreciate very much.
User avatar
mikeyww
Posts: 26880
Joined: 09 Sep 2014, 18:38

Re: Script makes pc lag

22 Oct 2021, 06:39

I suggest removing extraneous braces. As noted by RussF, in AHK v1.x, hotkeys use different syntax than functions. Instead of the bounding braces, they just use Return at the end. See this forum or the AHK documentation for examples.

Unlabeled code that follows Return is unreachable and will never execute.
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

Re: Script makes pc lag

22 Oct 2021, 09:44

I will review my code.

I checked, and its not due to the antivirus (does the same even when antivirus shut down).

If there are other causes to be explored: I'm all ears. In the meantime, I thought of doing a second hotkey app that would deactivate ("suspend and pause") the current script and only reactivate it while it calls one of its functions and then deactivate it again when the function is completed. What do you think? and how would I do such a thing?

Thank you.
User avatar
mikeyww
Posts: 26880
Joined: 09 Sep 2014, 18:38

Re: Script makes pc lag  Topic is solved

22 Oct 2021, 09:50

Easy to debug your script: instead of starting with 368 lines, start with a single short and simple hotkey routine. If it works, add one more routine, and re-test carefully. Repeat. This will enable you to pinpoint the problem quickly.
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

Re: Script makes pc lag

25 Oct 2021, 08:52

I think I found a possible part of the issue. A HotKey was triggered by another program.
I will test and get back to confirm.

Why does the ctrl+mouse click hotkey sometimes makes the script crash? It makes the mouse slow down then it crashes the script.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Nerafius, scriptor2016 and 104 guests