Jump to content

Receive Enter from Edit


Recommended Posts

I want to receive when Enter is pressed within the Edit control

for it i used a KeyHook

But,

If the edit control didn't had the $ES_WANTRETURN style then it eats up the Enter pressed

whereas if it has $ES_WANTRETURN then always a @CR is inserted

With the following code the Edit is not created with $ES_WANTRETURN therefore enter isnt detected,

Just place a $ES_WANTRETURN and then the code works

#include-once
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WINAPI.au3>

Global $Edit_Handle
Global $Func_Name
Global $_KeyPressed=0

Local $_hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
Local $_hmod = _WinAPI_GetModuleHandle(0)
Local $_hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($_hStub_KeyProc), $_hmod)
OnAutoItExitRegister("_OnAutoItExit")

Func _KeyProc($nCode, $wParam, $lParam)
Local $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
If $nCode < 0 Then
Return _WinAPI_CallNextHookEx($_hHook, $nCode, $wParam, $lParam)
EndIf
If $wParam = $WM_KEYDOWN Or $wParam=$WM_KEYUP Then
$vkKey = DllStructGetData($tKEYHOOKS, "vkCode")
$_KeyPressed=$vkKey
EndIf
Return _WinAPI_CallNextHookEx($_hHook, $nCode, $wParam, $lParam)
EndFunc

Func _OnAutoItExit()
_WinAPI_UnhookWindowsHookEx($_hHook)
DllCallbackFree($_hStub_KeyProc)
EndFunc


Local $GUi=GUICreate('Predict Text - Phoenix XL')
Local $Edit=GUICtrlCreateEdit('',10,10,400-20,300,BitOR($WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL)) ; If $ES_WANTRETURN is used then Enter is Detected But @CR is automatically inserted
Local $_Words[3]=['Hello','Abhishek','Now & Then']
$Edit_Handle=GUICtrlGetHandle($Edit)
GUIRegisterMsg($WM_COMMAND,'WM_COMMAND')
GUISetState()

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg
Local $iCode, $hWndEdit , $IDFrom
$iCode = BitShift($iwParam, 16)
$IDFrom= BitAND($iwParam, 0xFFFF)
Switch $ilParam
Case $Edit_Handle
Switch $iCode
Case $EN_CHANGE ;Sent when the user has taken an action that may have altered text in an edit control
Switch $_KeyPressed
Case 13 ;Enter
ConsoleWrite('Enter Pressed'&@CR)
Case 8 ;BackSpace
ConsoleWrite('BackSpace Pressed'&@CR)
EndSwitch
EndSwitch
EndSwitch
Return Call($Func_Name,$hWnd, $iMsg, $iwParam, $ilParam)
EndFunc ;==>WM_COMMAND

While GUIGetMsg()<>-3
Sleep(10)
WEnd

I just wanted to detect when the enter is pressed inside the Edit control without the Carriage Return

Please help me

thanks for your time and help :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

PhoenixXL,

to detect when the enter is pressed inside the Edit control

Why use a KeyHook when you can do it so simply like this: ;)

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$cInput = GUICtrlCreateInput("", 10, 10, 200, 20)

$cButton = GUICtrlCreateButton("Test", 10, 100, 80, 30)

$cEnter = GUICtrlCreateDummy()

GUISetState()

; Set an accelerator key - ENTER will fire $cEnter
Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]]
GUISetAccelerators($aAccelKeys)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            MsgBox(0, "Test", "Button Pressed")
        Case $cEnter
            ; Check that the input has focus
            If _WinAPI_GetFocus() = GUICtrlGetHandle($cInput) Then
                MsgBox(0, "Test", "Enter Pressed In Input")
            EndIf
    EndSwitch

WEnd

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks M23

its a very very simplified code rather than the complex hooks :)

Thank You very much :)

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

PhoenixXL,

Glad you like it. :)

If you need to use the {ENTER} key in another control, you must disable/reenable the Accel key and use ControlSend like this:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$cInput = GUICtrlCreateInput("", 10, 10, 200, 20)

$cButton = GUICtrlCreateButton("Test", 10, 100, 80, 30)

$cEdit = GUICtrlCreateEdit("", 10, 300, 200, 100)

$cEnter = GUICtrlCreateDummy()

GUISetState()

; Set an accelerator key - ENTER will fire $cEnter
Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]]
GUISetAccelerators($aAccelKeys)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            ConsoleWrite(StringToBinary(GUICtrlRead($cEdit)) & @CRLF)
        Case $cEnter
            ; Get control with focus
            $hFocus = _WinAPI_GetFocus()
            If $hFocus = GUICtrlGetHandle($cInput) Then
                ; If input
                MsgBox(0, "Test", "Enter Pressed In Input")
            Else
                ; If not we need to disable the Accel key
                GUISetAccelerators(0)
                ; Send a CR to the control
                ControlSend($hGUI, "", $hFocus, @CR)
                ; And then reenable the Accel key
                GUISetAccelerators($aAccelKeys)
            EndIf
    EndSwitch

WEnd

You can test yourself to see what happens if you do not do the disable/reenable cycle. Or the result if you do not ControlSend an {ENTER} to the other control. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 4 years later...

Tried to make it works with multiple gui but it seems to not work perfectly

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$cInput = GUICtrlCreateInput("", 10, 10, 200, 20)

$cButton = GUICtrlCreateButton("Test", 10, 100, 80, 30)

$cEdit = GUICtrlCreateEdit("", 10, 300, 200, 100)

$cEnter = GUICtrlCreateDummy()

; Set an accelerator key - ENTER will fire $cEnter
Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]]
GUISetAccelerators($aAccelKeys)

GUISetState()

$hGUI_Second = GUICreate("Test_Second", 500, 500)

$cInput_Second = GUICtrlCreateInput("", 10, 10, 200, 20)

$cEnter_Second = GUICtrlCreateDummy()

; Set an accelerator key - ENTER will fire $cEnter
Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter_Second]]
GUISetAccelerators($aAccelKeys)

GUISetState()

While 1
    $aMsg = GUIGetMsg(1) ;1 = $GUI_EVENT_ARRAY
    Switch $aMsg[1]
        Case $hGUI
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $cButton
                    ConsoleWrite(StringToBinary(GUICtrlRead($cEdit)) & @CRLF)
                Case $cEnter
                    ; Get control with focus
                    $hFocus = _WinAPI_GetFocus()
                    If $hFocus = GUICtrlGetHandle($cInput) Then
                        ; If input
                        MsgBox(0, "Test", "Enter Pressed In Input (FIRST GUI)")
                    Else
                        ; If not we need to disable the Accel key
                        GUISetAccelerators(0)
                        ; Send a CR to the control
                        ControlSend($hGUI, "", $hFocus, @CR)
                        ; And then reenable the Accel key
                        GUISetAccelerators($aAccelKeys)
                    EndIf
            EndSwitch
        Case $hGUI_Second
            Switch $aMsg[0]
                Case $cEnter_Second
                    MsgBox(0, "Test_Second", "Enter pressed in Input")
            EndSwitch
    EndSwitch
WEnd

When I focus $cEdit and hit the enter - nothing happens - ok

after that, when i move the the focus on $cInput (click on it) then msgbox appear (looks like it remember that I pressed enter before)

Any idea why this happening?

Link to comment
Share on other sites

  • Moderators

maniootek,

The problem occurs because GUISetAccelerators prevents the edit control form honouring the {ENTER} key as a new line - hence the event is stored until you refocus the input. You can see that the edit control refuses to enter a new line unless you comment out the GUISetAccelerators lines.

I will look into how you might combine the 2 requirements over the weekend.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I have had success using _IsPressed() in combination with identifying control/window focus. Of course using accelerator keys can be very handy, but there may be drawbacks with complicated registering/unregistering keys in certain cases. Hotkeys, accelerators and _Ispressed() all have their use. :)

Here's an example: https://www.autoitscript.com/forum/topic/184606-row-sum-σ-formula-validation/ 
This is the line (although it looks a bit messy):

If $msg2 = $hOkay Or (_IsPressed("0D") And BitAND(WinGetState($hChild), 8) = 8 And (ControlGetFocus($hChild) = "Edit2" Or ControlGetFocus($hChild) = "Edit1")) Then

Some accelerators are already set, and I might change the method later. This approach may be more suitable for multiple GUIs.

Edited by czardas
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...