AutoHotKey脚本按回车/小键盘回车无法获取复选框值求助
Problem Cause
The issue stems from the default behavior of a single-line Edit control: when you press Enter while focused on a single-line Edit, Windows automatically triggers the window's default button (your OK button). Since you also defined an Enter hotkey, these two events fire almost simultaneously, interfering with Gui, Submit's ability to correctly read the checkbox state—resulting in empty checkbox variables.
Solution
The simplest fix is to add the -Enter option to your Edit control to disable its default Enter behavior. This lets your hotkeys fire normally and correctly read all control states:
#Persistent #SingleInstance Off Gui, New, -MinimizeBox, % "Test" Gui, Add, Edit, vUserInput w300 -Enter ; Add -Enter to disable default Enter behavior Gui, Add, Checkbox, vMyCheckbox1, % "Checkbox one" ; Fixed typo: Ceckbox → Checkbox Gui, Add, Checkbox, vMyCheckbox2, % "Checkbox two" Gui, Add, Button, gOkButtonPress w50 h25 x80, % "OK" Gui, Show, Center return Enter:: Gosub, OkButtonPress return ; Add return to properly end the hotkey code block NumpadEnter:: Gosub, OkButtonPress return ; Add return here as well OkButtonPress: Gui, Submit MsgBox, Meow! %MyCheckbox2% ExitApp return
Additional Improvements
- Fixed the typo in the first checkbox label (
Ceckbox→Checkbox) - Added
returnstatements to theEnterandNumpadEnterhotkeys to follow AutoHotKey's code block conventions and avoid potential execution flow issues
Now when you run the script, clicking the OK button or pressing Enter/NumpadEnter will correctly display the second checkbox's state (0 or 1) in the message box.
内容的提问来源于stack exchange,提问作者user14430182

