Security in combination of Hotkey with AhkThread()

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
manehscripts
Posts: 80
Joined: 03 May 2019, 16:10

Security in combination of Hotkey with AhkThread()

11 Jun 2019, 13:24

Hello guys!

When creating multiple AhkThread(), the combination of some keys that are pressed by an AhkThread() will be combined with other keys that are pressed in the other AhkThread(), or if you press manually while an AhkThread() is being read (example below). The question is, is there any safe way to mount the script to avoid this combination that can cause multiple errors?

Code: Select all

Gui, +AlwaysOnTop
Gui, Font, s30
Gui, Add, Button, w200 h100 vStart gStart, START!
Gui, Show
return

toggle := 0
Start:
    toggle := !toggle
    if (toggle) {
		GuiControl,, Start, STOP!
		VarCrit := CriticalObject({})
        script:="
        ("
			VarCrit := CriticalObject(A_Args[1])
            Loop {
				Send, {F4}
				ToolTip, The loop is pressing F4. If you press Alt it will combine and close.
            }
        )"
        Dll:=AhkThread(script,&VarCrit)
    } else {
        Dll.ahkTerminate.1
		GuiControl,, Start, START!
    }
return

ESC::ExitApp
GuiClose:
ExitApp
I believe some of these options below are useful for this, but I do not know how to use it in each of the AhkThread(): (Can someone help me?)

Code: Select all

#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#MaxThreads 20
Process, Priority, , A
#KeyHistory 0
exemplo9.gif
exemplo9.gif (261.02 KiB) Viewed 649 times
kyuuuri
Posts: 201
Joined: 09 Jan 2016, 19:20

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 13:40

Keys such as Alt, Ctrl, Shift are global keys. Doesn't matter where you press them, it will affect all keys sent. Is not a problem with ahk_h nor threads.
manehscripts
Posts: 80
Joined: 03 May 2019, 16:10

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 13:44

kyuuuri wrote:
11 Jun 2019, 13:40
Keys such as Alt, Ctrl, Shift are global keys. Doesn't matter where you press them, it will affect all keys sent. Is not a problem with ahk_h nor threads.
Yes, I understand that the problem is not ahk_H. I need to know if there is any safe way to avoid this. :|
So in that case, if I set the Hotkeys to (for example) A, B, C, D, 3, 4, 6 ... it will not cause conflict?
kyuuuri
Posts: 201
Joined: 09 Jan 2016, 19:20

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 14:43

manehscripts wrote:
11 Jun 2019, 13:44
kyuuuri wrote:
11 Jun 2019, 13:40
Keys such as Alt, Ctrl, Shift are global keys. Doesn't matter where you press them, it will affect all keys sent. Is not a problem with ahk_h nor threads.
Yes, I understand that the problem is not ahk_H. I need to know if there is any safe way to avoid this. :|
So in that case, if I set the Hotkeys to (for example) A, B, C, D, 3, 4, 6 ... it will not cause conflict?
If by conflict you mean pressing both keys at the same time then I'm not sure. I'm no expert in keyboards but if I'm not wrong holding more than 1 key at the same time will send the key that has the highest priority in the layout. But again, I'm not sure.
It won't make your scripts crash, get stuck, etc. Maybe it will simply not work with both keys at the same time.
Long time ago I had 3 ahkthreads with different Hotkeys I used at the same time, like holding them at the same time and it didn't have any conflicts BUT some mouse movement and keys release were weird.

If by conflict you mean keys releasing Shift, Alt, Ctrl, etc. before sending then that's exactly how ahk works unless you ignore those key states or use {blind}
Also you can do 1 if with 3 GetKeyStates that evaluate to true ONLY when none of those keys are down, this way you avoid combinations.
But remember, first try then ask.
swagfag
Posts: 2601
Joined: 11 Jan 2017, 17:59

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 15:05

first of all, which part of what u have posted so far has anything to do with security in the slightest?
second, explain clearly what ur problem is. what are u trying to accomplish, what is happening that u think should be happening, what are the results ure after.

what i gather from this thread is uve spun an ahk_h thread that spams F4. u claim that as soon as u press Alt "it(it, meaning what exactly?) will combine(combine how?) and close(close what, the loop, the thread, the gui, everything?)". i can only assume u mean the spamming of F4, along with ur physical holding down of Alt, produces Alt + F4 and since ur gui is the currently focused window, it gets altf4'ed, triggering GuiClose and shutting everything down.

It does no such thing.

Send {F4} auto-releases all held down modifiers to produce a single F4 keystroke. post an example that actually demonstrates the problems u face.
manehscripts
Posts: 80
Joined: 03 May 2019, 16:10

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 15:58

swagfag wrote:
11 Jun 2019, 15:05
first of all, which part of what u have posted so far has anything to do with security in the slightest?
second, explain clearly what ur problem is. what are u trying to accomplish, what is happening that u think should be happening, what are the results ure after.

what i gather from this thread is uve spun an ahk_h thread that spams F4. u claim that as soon as u press Alt "it(it, meaning what exactly?) will combine(combine how?) and close(close what, the loop, the thread, the gui, everything?)". i can only assume u mean the spamming of F4, along with ur physical holding down of Alt, produces Alt + F4 and since ur gui is the currently focused window, it gets altf4'ed, triggering GuiClose and shutting everything down.

It does no such thing.

Send {F4} auto-releases all held down modifiers to produce a single F4 keystroke. post an example that actually demonstrates the problems u face.
lol, I've posted a gif exactly so people understand that it's matching an out-of-thread process. I understand that this has to work, but I needed to understand how to "lock" other keys like Alt, Shit, Ctrl while the line of code at that moment is running.

I've put together a simple example to try to understand, but in my code it's not exactly like that.

In my code it works as follows:
The program has several processes in tabs with different AhkThread().
Each tab has an option for the person to choose the type of Hotkey that will be used automatically in a certain situation. Suppose he chose Ctrl + F12 for tab 1.

Code: Select all

;TAB 1
HotKeysCombination1 := Ctrl
HotKeys1 := F12

if (HotKeysCombination1==Alt) {
	Send, !{%HotKeys1%}
} else if (HotKeysCombination1==Ctrl) {
	Send, ^{%HotKeys1%}
} else if (HotKeysCombination1==Shift) {
	Send, +{%HotKeys1%}
}
What is happened is that sometimes it presents a combination conflict causing slow execution of the command if other combinations are being executed at the same time. It usually happens when the user prefixes some keys (eg, Alt or Shift) manually while an AhkThread() is running and trying to press Ctrl (like the example above).
manehscripts
Posts: 80
Joined: 03 May 2019, 16:10

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 17:18

Sorry for duplicate post, but I understood how SEND logic works. Each time it is triggered, it "unlocks" any other Alt, Ctrl or Shift key that is being pressed by the user or by another AhkThread(). The problem that may be happening to me is that the moment another Hotkey needs to be triggered, another may be running and ends up causing the supposed conflict. I will continue to do some detailed testing, because now I understand the logic of how that combination works.
Last edited by manehscripts on 11 Jun 2019, 18:41, edited 1 time in total.
swagfag
Posts: 2601
Joined: 11 Jan 2017, 17:59

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 18:13

i saw ur gif. i also ran ur code. it didnt exhibit any of the issues u claimed to have been experiencing for the very reasons i already outlined in my previous post.

if u present one example, but the problem manifests itself in a completely different one, i dont know what to tell u. it would be quite obviously somewhat difficult to diagnose code that no one but u is privy to

u will probably have to synchronize the threads so that only one thread at a time ever attempts sending something. have the main script orchestrate that, or use a shared global variable, something like that
manehscripts
Posts: 80
Joined: 03 May 2019, 16:10

Re: Security in combination of Hotkey with AhkThread()

11 Jun 2019, 23:27

HotKeyIt wrote:
11 Jun 2019, 20:01
Therefore you can use CriticalSection
Hello bro!
I've noticed Ctrl, Alt and Shift are canceled (if it's being pressed), when some other process executes the SEND command. In my test, I pressed Ctrl while AhkThread() was running with a check, when it needed to execute the command, it left a shortcut that I configured F5 (for example), and Ctrl (which I kept pressing) was released as if I remove taken my finger, but I did not.
The funny thing is that in the code below, while F4 is being pressed by the CriticalObject(), the Alt is not being canceled because it is doing a Hotkey combination and closing the program by the Alt + F4 junction (Alt because I am pressed, and F4 of the CriticalObject()).

And I tested the CriticalSection() but it seems that it did not work, or I adapting the wrong way (the link example does not work, I had to try to interpret it and did so).

Code: Select all

Gui, +AlwaysOnTop
Gui, Font, s30
Gui, Add, Button, w200 h100 vStart gStart, START!
Gui, Show
return

toggle := 0
Start:
    toggle := !toggle
    if (toggle) {
		GuiControl,, Start, STOP!
		VarCrit := CriticalObject({})
        script:="
        ("
            SetBatchLines, -1
            SendMode Input
			
			VarCrit := CriticalObject(A_Args[1])
			CriSec := CriticalSection()
            Loop {
				EnterCriticalSection(CriSec)
				var++
				Send, {F4}
				ToolTip, The loop is pressing F4. If you press Alt it will combine and close. %var%
				LeaveCriticalSection(CriSec)
            }
        )"
        Dll:=AhkThread(script,&VarCrit)
    } else {
        Dll.ahkTerminate.1
		GuiControl,, Start, START!
    }
return

ESC::ExitApp
GuiClose:
ExitApp
kyuuuri
Posts: 201
Joined: 09 Jan 2016, 19:20

Re: Security in combination of Hotkey with AhkThread()

12 Jun 2019, 15:05

So your problem now is:
If you have 3 loops sending keys at the same time and you press alt it gets ignored by some send commands because it releases alt to send the keys?
Then use {blind} option it will ignore Alt/Control/Shift/Win state. So if you hold alt it will send alt+key.
If this is not the problem then I suggest you redo the post by explaining EXACTLY what you have, what it does and what you need. For example:

Right now I have 3 loops sending keys.
1 Of those loops uses alt modifier.
1 Of those loops uses ctrl modifier.
The last one doesn't use any modifier.

When I open all 3 it doesn't work as expected, sometimes keys are not modified, sometimes they are.

What I need:
Loop 1 send alt+key
Loop 2 send ctrl+key
Loop 3 send key only

No need to use images, videos, gifs, as it is not a visual issue.
swagfag
Posts: 2601
Joined: 11 Jan 2017, 17:59

Re: Security in combination of Hotkey with AhkThread()

12 Jun 2019, 16:31

no, i think i got it. what hes doing is he has several threads ALL of which simultaneously spam keys(in SendInput mode, but who knows really?? hooks/other scripts might be disabling it and it reverts back to SendEvent) and they do so at blazing fast speeds since hes using this prototype for his threads:

Code: Select all

SetBatchLines -1
SendMode Input

Loop
	Send a ; the key
basically SetBatchLines -1 + SendInput + Loop with no Sleep = wtf
crank it up a couple of notches: + ???x Threads = wtf ** WTF

u dont need ahk_h to break this, same thing happens on v1 1.1.30.3 too:
  1. run script, notepad opens, r is spammed
  2. physically hold down Shift(my keybd has auto-repeat, urs might be different) or spam it like crazy manually
  3. observe the instances where notepad(!! its not the script because the keyhistory checks out, see for urself with #KeyHistory 500 / #InstallKeyboardHook) couldnt keep up with the hook and let Shift slip through, producing an R. The longer u spam, the more slip ups u start noticing, as the program becomes overloaded
  • F1 when uve had enough and want to inspect the results
  • Esc to close & exit

Code: Select all

#NoEnv
SendMode Input
SetBatchLines -1

Run notepad.exe
WinWaitActive ahk_exe notepad.exe

Loop
	Send r
Until breakLoop

*F1::breakLoop := true
*Esc::
	WinClose ahk_exe notepad.exe
	ExitApp
demo

CriticalSection wont help in this case. u simply have to spam the keys at a slower rate like 30ms or depending on the application receiving them, u might get away with simply holding them down {F4 Down} instead of spamming

@HotKeyIt check the docs PR when u can

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 3 guests