Code: Select all
; https://autohotkey.com/board/topic/26054-how-to-detect-loss-of-focus-in-edit-control-solved/
WM_COMMAND=0x111
EN_SETFOCUS=0x100
EN_KILLFOCUS=0x200
SetFormat, Integer, Hex
Gui, Add, Edit, vEdit1 hWndhEdit1, Edit1
Gui, Add, Edit, vEdit2 hWndhEdit2, Edit2
Gui, Add, Text, xm, Window handle:
Gui, Add, Text, x100 yp vWindowHandle w100,
Gui, Add, Text, xm, Edit1 handle:
Gui, Add, Text, x100 yp vEdit1Handle w100, %hEdit1%
Gui, Add, Text, xm, Edit2 handle:
Gui, Add, Text, x100 yp vEdit2Handle w100, %hEdit2%
Gui, Add, Text, xm, MsgNum:
Gui, Add, Text, x100 yp vMsgNumText w100
Gui, Add, Text, xm, Parm1:
Gui, Add, Text, x100 yp vParm1Text w100
Gui, Add, Text, xm+10, Word1:
Gui, Add, Text, x110 yp vWord1Text w100
Gui, Add, Text, xm+10, Word2:
Gui, Add, Text, x110 yp vWord2Text w100
Gui, Add, Text, xm, Parm2:
Gui, Add, Text, x100 yp vParm2Text w100
Gui, Add, Text, xm, Parm3:
Gui, Add, Text, x100 yp vParm3Text w100
Gui, Add, Text, xm, Parm4:
Gui, Add, Text, x100 yp vParm4Text w100
Gui, Add, Text, xm +Hidden vBlank, Edit 1 blank
Gui, Show, w200
hWindow:=WinExist("A")
GuiControl,,WindowHandle, %hWindow%
GuiControl,,MsgNumText, %WM_COMMAND%
OnMessage(WM_COMMAND,"MessageHandler")
Return
MessageHandler(Parm1,Parm2,Parm3,Parm4)
{
global
GuiControl,,Parm1Text, %Parm1% ; wParam
Word1:=(Parm1&0xFFFF0000)>>16 ; NotificationNum - EN_KillFocus=200
GuiControl,,Word1Text, %Word1%
GuiControl,,Word2Text, % (Parm1&0x0000FFFF) ; Control identifier ??
GuiControl,,Parm2Text, %Parm2% ; lParam - Control window_handle
GuiControl,,Parm3Text, %Parm3% ; Message ID
GuiControl,,Parm4Text, %Parm4% ; Window handle
GuiControlGet, Edit1
; MsgBox
If Parm2=%hEdit1%
If Word1=%EN_KILLFOCUS%
If Edit1=
GuiControl, Show, Blank
Else
GuiControl, Hide, Blank
}
GuiClose:
GuiEscape:
ExitApp
This is displaying lots of information for testing. This could be trimmed down quite a bit for actual use.
I also ran into the same problem as in this thread with not being able to get WM_KILLFOCUS to work. With Spy++ I can see that message get sent each time a control loses focus but I never could get the AHK code right to detect WM_KILLFOCUS.