Page 1 of 1

Don't execute script if a specified key was pressed before the hotkey

Posted: 01 Jun 2020, 12:33
by MikeKey
Hello,

I have the following script

Code: Select all

#If WinActive("ahk_exe notepad.exe")
    g::
    +g::
    Send, é
    Sleep, 400 
    Send, q
    Sleep, 500 
    Send, í
    return
#If
It works fine, it executes when I press G or shit + G, but I need to add one limitation for it:

If I press the ENTER key, and then I press the G, nothing should happen untill I press either the ENTER again or ESC.
So to make it more simple, here is how it should work:

Press G - script executes
Press Enter - Press G - Nothing happens
Press Enter - Press G - Nothing happens, Press G again - Nothing happens
Press Enter - Press G - Nothing happens - Press Enter - Press G - Script executes
Press Enter - Press G - Nothing happens - Press Escape - Press G - Script executes

Basically, pressing the Enter key turns the G hotkey "off" until the ENTER is pressed again (or ESCAPE) and it urns the G hotkey back "on"

In case you are curious why I need it to work like that, it's for a game, I don't want the hotkey to do anything when I'm typing inside of the game chat (which is activated by an enter key, and then the typed message is sent by pressing the enter again, or pressing the esc to close the chat).

thank you!

Re: Don't execute script if a specified key was pressed before the hotkey

Posted: 01 Jun 2020, 13:10
by Nightwolf85
I have a solution that uses the Hotkey Command.

Code: Select all

Hotkey, IF, WinActive("ahk_exe notepad.exe")
Hotkey,g,doThing, on
Hotkey,+g,doThing, on
Hotkey, IF
Return

#If WinActive("ahk_exe notepad.exe")
	Enter::
	Esc::
		Hotkey, IF, WinActive("ahk_exe notepad.exe")
			Hotkey,g, Toggle
			Hotkey,+g, Toggle
		Hotkey, IF
	Return
#If


doThing:
    Send, é
    Sleep, 400 
    Send, q
    Sleep, 500 
    Send, í
Return
How does that work for you?

Re: Don't execute script if a specified key was pressed before the hotkey  Topic is solved

Posted: 01 Jun 2020, 14:17
by dmg
Try this:

Code: Select all

~enter::flag := !flag

#If WinActive("ahk_exe notepad.exe") and !(flag)
    g::
    +g::
    Send, é
    Sleep, 400 
    Send, q
    Sleep, 500 
    Send, í
    return
#If
It sets the enter key as a hotkey that sets the state of the flag variable to be the logical opposite of its current state. Allowing it to act as a toggle switch for your #IF statement.

Re: Don't execute script if a specified key was pressed before the hotkey

Posted: 02 Jun 2020, 06:41
by MikeKey
Thank you, I tried the flag method and it is working perfectly.