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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MikeKey
Posts: 6
Joined: 01 Jun 2020, 12:17

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

Post by MikeKey » 01 Jun 2020, 12:33

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!

Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

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

Post by Nightwolf85 » 01 Jun 2020, 13:10

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?

User avatar
dmg
Posts: 287
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

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

Post by dmg » 01 Jun 2020, 14:17

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.
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

MikeKey
Posts: 6
Joined: 01 Jun 2020, 12:17

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

Post by MikeKey » 02 Jun 2020, 06:41

Thank you, I tried the flag method and it is working perfectly.

Post Reply

Return to “Ask for Help (v1)”