AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to detect loss of focus in edit control - SOLVED

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
dncarac



Joined: 31 Aug 2006
Posts: 64

PostPosted: Mon Feb 11, 2008 4:15 pm    Post subject: How to detect loss of focus in edit control - SOLVED Reply with quote

On more research, I found that the WM_KILLFOCUS message applied to windows. The killfocus message for controls is a notification: EN_KILLFOCUS, which is delivered to the window via a WM_COMMAND message.

Here's a program which will trigger on an edit box losing focus - in this case it detects whether the edit box is blank. It also displays all the pertinent values for the WM_COMMAND message. There's one message for the edit box losing focus and another for the edit box gaining focus. To see both messages, uncomment the MsgBox line in the MessageHandler. Of course, the guts of this is the OnMessage statement, and the MessageHandler function.

Code:
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


The only question I have now is the value of Word2. It is supposed to be a Control Identifier. But as you will see, for Edit1, the value is 3 and for Edit2, the value is 4. This doesn't seem to correspond to the ClassNN value.

Can anyone tell me what those numbers represent?

DNC


=======Original question =======================

I am making a small editor and need to detect if an edit control is blank when it loses focus so that the slot it represents may be deleted from a series of such slots before anything else is done (that is, no blank slots are allowed).

I looked at the OnMessage(0x07...) which is the KillFocus notification, but the test program I wrote won't trigger when focus is changed from the edit control. Here's the test program: (it works fine for Move ??!!)

Code:
Gui, Add, Edit, vEdit1, Edit1
Gui, Add, Edit, vEdit2, Edit2
Gui, Show, w200

; 0x03 - Move  0x07 - KillFocus 0x201 - lbuttondown
MsgNum=0x07
OnMessage(MsgNum,"MessageHandler")
MsgBox % "Messsage " . MsgNum . " handler: " . OnMessage(MsgNum)
Return

MessageHandler(Parm1,Parm2,Parm3,Parm4)
{
  MsgBox MessageHandler called
  MsgBox Parm1: %Parm1%, Parm2: %Parm2%, Parm3: %Parm3%, Parm4: %Parm4%
}
GuiClose:
GuiEscape:
ExitApp


I also looked at ControlGetFocus, but that is a one time check. If I put it on a timer, it is still possible for something else to happen before the loss of focus is detected and handled.

OnMessage seems exactly what I need. Is there some way of making it work for this purpose?

DNC
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group