Jump to content

Want to Connect function to Enter key


mark2004
 Share

Recommended Posts

I would like to be able to run a certain function in my script when the Enter key is pressed

while in a specific edit box. I have tried ways in the past using WM_Notify_Events/WM_COMMAND functions

but have never been successful. I always end up activating the function when the edit field is

tabbed to/from or some other unwanted events. I just want it to run when the enter key is pressed in that

specific field.

Any ideas?

Link to comment
Share on other sites

Would this work?

If WinExists ("your window") Then
    HotKeySet ("{enter}", "your function")
EndIf

If Not WinExists ("your window") Then
    HotKeySet ("{enter}", "")
EndIf

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Link to comment
Share on other sites

I would like to be able to run a certain function in my script when the Enter key is pressed

while in a specific edit box. I have tried ways in the past using WM_Notify_Events/WM_COMMAND functions

but have never been successful. I always end up activating the function when the edit field is

tabbed to/from or some other unwanted events. I just want it to run when the enter key is pressed in that

specific field.

Any ideas?

try: http://www.autoitscript.com/forum/index.ph...st&p=296812
Link to comment
Share on other sites

Would this work?

If WinExists ("your window") Then
    HotKeySet ("{enter}", "your function")
EndIf

If Not WinExists ("your window") Then
    HotKeySet ("{enter}", "")
EndIf

No, that wouldn't work because the enter key would trigger the function no matter what field the cursor were in. I

want the enter key to trigger the function only when in a specific edit field.

Link to comment
Share on other sites

Unfortunately, none of those methods work. The problem is that if you use the WM_COMMAND method and look for $EN_UPDATE, then any change in the edit field will trigger the function (like entering characters). Also, with an input

field in autoit, pressing Enter doesn't trigger the $EN_UPDATE, only the entering of characters. Also, checking the

last 2 characters to see if they are @CRLF doesn't work either for some reason. Apparently the @CRLF isn't actually

being passed to the input (maybe since it is a single line control).

These are the same things I've run into in the past. I was just hoping that someone had "solved" it by now......

Link to comment
Share on other sites

Unfortunately, none of those methods work. The problem is that if you use the WM_COMMAND method and look for $EN_UPDATE, then any change in the edit field will trigger the function (like entering characters). Also, with an input

field in autoit, pressing Enter doesn't trigger the $EN_UPDATE, only the entering of characters. Also, checking the

last 2 characters to see if they are @CRLF doesn't work either for some reason. Apparently the @CRLF isn't actually

being passed to the input (maybe since it is a single line control).

These are the same things I've run into in the past. I was just hoping that someone had "solved" it by now......

OK what about this:

#include <GuiConstantsEx.au3>
#include <EditConstants.au3>

$TestGui = GUICreate("test")
$edit1 = GUICtrlCreateEdit("", 5, 10, 190, 17, $ES_AUTOVSCROLL)
$edit2 = GUICtrlCreateEdit("", 5, 40, 190, 17, $ES_AUTOVSCROLL)
$edit3 = GUICtrlCreateEdit("", 5, 70, 190, 17, $ES_AUTOVSCROLL)

$accEnter = GUICtrlCreateDummy()

Global $a_AccelKeys[1][2] = [["{ENTER}", $accEnter]]
GUISetAccelerators($a_AccelKeys)

GUISetState()

While 1
    $msg = GUIGetMsg()

    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $accEnter
            If _GuiCtrlGetFocus($TestGui) = $edit2 Then
                _MyEnterFunc()
            Else
                GUISetAccelerators("")
                ControlSend($TestGui, "", _GuiCtrlGetFocus($TestGui), "{ENTER}")
                GUISetAccelerators($a_AccelKeys)
            EndIf
    EndSwitch

WEnd


Func _MyEnterFunc()
    MsgBox(0, "", "Enter Pressed in Edit2")
EndFunc  ;==>_MyEnterFunc


Func _GuiCtrlGetFocus($hGui = "")
    Local $InputID = ControlGetHandle($hGui, "", ControlGetFocus($hGui))
    Return _ControlGetGuiID($InputID)
EndFunc  ;==>_GuiCtrlGetFocus

Func _ControlGetGuiID($hCtrl)
    Local $Result = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hCtrl)
    If @error = 0 Then Return $Result[0]
    Return SetError(1, 0, '')
EndFunc  ;==>_ControlGetGuiID
Link to comment
Share on other sites

OK what about this:

#include <GuiConstantsEx.au3>
#include <EditConstants.au3>

$TestGui = GUICreate("test")
$edit1 = GUICtrlCreateEdit("", 5, 10, 190, 17, $ES_AUTOVSCROLL)
$edit2 = GUICtrlCreateEdit("", 5, 40, 190, 17, $ES_AUTOVSCROLL)
$edit3 = GUICtrlCreateEdit("", 5, 70, 190, 17, $ES_AUTOVSCROLL)

$accEnter = GUICtrlCreateDummy()

Global $a_AccelKeys[1][2] = [["{ENTER}", $accEnter]]
GUISetAccelerators($a_AccelKeys)

GUISetState()

While 1
    $msg = GUIGetMsg()

    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $accEnter
            If _GuiCtrlGetFocus($TestGui) = $edit2 Then
                _MyEnterFunc()
            Else
                GUISetAccelerators("")
                ControlSend($TestGui, "", _GuiCtrlGetFocus($TestGui), "{ENTER}")
                GUISetAccelerators($a_AccelKeys)
            EndIf
    EndSwitch

WEnd


Func _MyEnterFunc()
    MsgBox(0, "", "Enter Pressed in Edit2")
EndFunc ;==>_MyEnterFunc


Func _GuiCtrlGetFocus($hGui = "")
    Local $InputID = ControlGetHandle($hGui, "", ControlGetFocus($hGui))
    Return _ControlGetGuiID($InputID)
EndFunc ;==>_GuiCtrlGetFocus

Func _ControlGetGuiID($hCtrl)
    Local $Result = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hCtrl)
    If @error = 0 Then Return $Result[0]
    Return SetError(1, 0, '')
EndFunc ;==>_ControlGetGuiID
Thanks for that. It definitely works. I just need to read up on using Accelerators and Dummy controls before I fully

understand what you did..... Thanks again!!

Link to comment
Share on other sites

Thanks for that. It definitely works. I just need to read up on using Accelerators and Dummy controls before I fully

understand what you did..... Thanks again!!

By all means, do some reading.

To sum it up briefly:

Accelerators are like HotKeySet() except that they work only when your GUI is active.

Accelerators however are tied to controls as opposed to functions.

So, we create a dummy control to be fired by the accelerator & poll for that in our GuiGetMsg loop.

From there, it's check which control has the focus when the accelerator (ENTER) is hit. If its the control we want to act on, call our custom function.

If the focus isn't on the control we want, turn off the accelerator(s), ControlSend() the regular action of the key used as the accelerator to the current control, then turn the accelerator(s) back on.

We turn the accelerator(s) off and then back on so we don't get stuck in a loop of sending {ENTER} all the time.

Credit for the use of dummy controls with Accelerators goes to Zedna http://www.autoitscript.com/forum/index.ph...st&p=493696

Link to comment
Share on other sites

By all means, do some reading.

To sum it up briefly:

Accelerators are like HotKeySet() except that they work only when your GUI is active.

Accelerators however are tied to controls as opposed to functions.

So, we create a dummy control to be fired by the accelerator & poll for that in our GuiGetMsg loop.

From there, it's check which control has the focus when the accelerator (ENTER) is hit. If its the control we want to act on, call our custom function.

If the focus isn't on the control we want, turn off the accelerator(s), ControlSend() the regular action of the key used as the accelerator to the current control, then turn the accelerator(s) back on.

We turn the accelerator(s) off and then back on so we don't get stuck in a loop of sending {ENTER} all the time.

Credit for the use of dummy controls with Accelerators goes to Zedna http://www.autoitscript.com/forum/index.ph...st&p=493696

That's great stuff. Yet another tool in my toolbox. Thanks for the great breakdown of the script's logic.

Is this the only use for dummy controls? They obviously fit the bill here beautifully but I can't think of another scenario

where they can be used. I'm just asking because I want to make sure I fully understand their utility.

Link to comment
Share on other sites

That's great stuff. Yet another tool in my toolbox. Thanks for the great breakdown of the script's logic.

Is this the only use for dummy controls? They obviously fit the bill here beautifully but I can't think of another scenario

where they can be used. I'm just asking because I want to make sure I fully understand their utility.

Dummies can be used for many things:

You can group controls together for easy enabling/disabling or showing/hiding in bulk:

http://www.autoitscript.com/forum/index.ph...st&p=579483

You can set data to a dummy, then read it later so you can keep track of options without using other variables:

$tmp = GuiCtrlCreateDummy()
If $msg = $btn1 then GUICtrlSendToDummy($test,"Advanced")
;....................
;....................
If GuiCtrlRead($tmp) = "Advanced" then 
;..................

Check the help file example under GUICtrlCreateContextMenu for another example.

Useful in OnEvent mode: search the forrums for GuiCtrlCreateDummy and you'll find more.

Have fun! :P

Link to comment
Share on other sites

Dummies can be used for many things:

You can group controls together for easy enabling/disabling or showing/hiding in bulk:

http://www.autoitscript.com/forum/index.ph...st&p=579483

You can set data to a dummy, then read it later so you can keep track of options without using other variables:

$tmp = GuiCtrlCreateDummy()
If $msg = $btn1 then GUICtrlSendToDummy($test,"Advanced")
;....................
;....................
If GuiCtrlRead($tmp) = "Advanced" then 
;..................

Check the help file example under GUICtrlCreateContextMenu for another example.

Useful in OnEvent mode: search the forrums for GuiCtrlCreateDummy and you'll find more.

Have fun! :P

Thanks for all the great info. It looks like Dummy controls are pretty handy. I can't wait to write another script

so I can make use of them.

Cheers.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...