Key-press for a hotkey of a key-release Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
padanges
Posts: 24
Joined: 15 Jul 2019, 06:59

Key-press for a hotkey of a key-release

04 Aug 2020, 05:20

Hi there,
I have a simple code, where I want to create a hotkey on a key-press.
The problem is that it works (gives confimation with MsgBox) only once - and I can't figure out why it's so. Maybe someone could share his thoughts?

Code: Select all

#SingleInstance Force
Gui, Add, Text,, Release F1 button for message
Gui, Show, w200 h150
Return

F1::
	Hotkey F1, __Message, On
	Send {Blind}{F1 down}
Return

__Message:
	KeyWait F1
	Hotkey F1, __Message, Off
	MsgBox The button has been released
Return

ESC::ExitApp
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Key-press for a hotkey of a key-release  Topic is solved

04 Aug 2020, 09:51

If you're asking why it doesn't show the message again on subsequent keypresses, it's very clearly because you turned off the hotkey:

Code: Select all

	Hotkey F1, __Message, Off

Are you asking why it doesn't show the message more than once on the first key press? What makes you think that it should do that? You press the hotkey, the original definition changes the hotkey to execute the code at the label instead, then invokes the hotkey, which then turns the hotkey off and shows the message.
padanges
Posts: 24
Joined: 15 Jul 2019, 06:59

Re: Key-press for a hotkey of a key-release

05 Aug 2020, 01:22

Thanks boiler for the questions - you got me thinking in a right direction: I have just realized that Hotkey function turns off not only Hotkey function created hotkeys but hotkey labels as well! It seems I have missed that in documentation. So it's obvious why we would see only one MsgBox after multiple key presses, as this code illustrates:

Code: Select all

#SingleInstance Force
Gui, Add, Text,, Press F1 button for message
Gui, Show, w200 h150
Return

F1::
	MsgBox How many times will you see me?
	Hotkey F1, , Off
Return

ESC::ExitApp
Furthermore it seems that by creating a hotkey by a function call (Hotkey) within a hotkey label (F1::) we simply re-label the hotkey - as demonstrated here:

Code: Select all

#SingleInstance Force
Gui, Add, Text,, Release F1 button for message
Gui, Show, w200 h150
Return

F1::
	Hotkey F1, _RelabeledF1hotkey, On
	MsgBox Message from the 1st hotkey label
Return

_RelabeledF1hotkey:
	MsgBox Message from the 2nd hotkey label
Return

ESC::ExitApp
So to answer the initial question - a "key-press for a hotkey of a key-release" simply has to be seen as a hotkey relabeling, without turning off the hotkey itself. That's when we would see multiple button presses to invoke multiple messages. Here's the fix:

Code: Select all

#SingleInstance Force
Gui, Add, Text,, Release F1 button for message
Gui, Show, w200 h150
Return

F1::
	Hotkey F1, __Message, On
Return

__Message:
	KeyWait F1
	;; Hotkey F1, __Message, Off
	MsgBox The button has been released
Return

ESC::ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Fredrik F, Joey5, maxkill and 388 guests