ale1981 Posted October 28, 2014 Share Posted October 28, 2014 I am writing a script where I would like the users to be able to hit enter as well as the tab key to navigate the inputs. I have this working apart from one problem, when a message box appears and the user presses enter to close it, this will then close the message box but still send the Tab key, here is the part of my code I am using; I thought the code would not run as it checks to see if the input is in focus, my problem is that after the message box my code returns the focus to the input, this then sends the Tab, I am not sure how to get around it? $cEnter = GUICtrlCreateDummy() Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]] GUISetAccelerators($aAccelKeys) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _SQL_Close() $btFormat.Close( 1 ) $btApp.Quit( 1 ) Exit case $cEnter enterPressed() EndSwitch WEnd Func enterPressed() $cFocus = _WinAPI_GetFocus() If $cFocus = GUICtrlGetHandle( $txtPackerID ) Then Send( "{TAB}" ) If $cFocus = GUICtrlGetHandle( $txtParcType ) Then Send( "{TAB}" ) If $cFocus = GUICtrlGetHandle( $txtWeight ) Then savePrint() EndFunc I hope I have explained well enough! Thanks in advance. Link to comment Share on other sites More sharing options...
jdelaney Posted October 28, 2014 Share Posted October 28, 2014 (edited) Why use tab, when you can use ControlFocus Or, add a condition that your window is also active. Edited October 28, 2014 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 28, 2014 Moderators Share Posted October 28, 2014 ale1981,This example script does not move the focus when using {ENTER} to clear a MsgBox: expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WinAPI.au3> $hGUI = GUICreate("Test", 500, 500) $cInput_1 = GUICtrlCreateInput("", 10, 10, 200, 20) $cInput_2 = GUICtrlCreateInput("", 10, 50, 200, 20) $cInput_3 = GUICtrlCreateInput("", 10, 90, 200, 20) $cEnter = GUICtrlCreateDummy() GUISetState() Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]] GUISetAccelerators($aAccelKeys) AdlibRegister("_MsgBox", 5000) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cEnter Switch _WinAPI_GetFocus() Case GUICtrlGetHandle($cInput_1) GUICtrlSetState($cInput_2, $GUI_FOCUS) ; Why Send {TAB} - this is more efficient Case GUICtrlGetHandle($cInput_2) Send ("{TAB}") ; But this still does not move focus Case GUICtrlGetHandle($cInput_3) ConsoleWrite("End" & @CRLF) EndSwitch EndSwitch WEnd Func _MsgBox() MsgBox($MB_SYSTEMMODAL, "Hi", "") EndFuncHow does your code differ from that? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ale1981 Posted October 29, 2014 Author Share Posted October 29, 2014 Thanks guys will experiment and get back to you, not quite sure why I was using Send instead of the SetState, making it difficult for myself! Link to comment Share on other sites More sharing options...
ale1981 Posted October 29, 2014 Author Share Posted October 29, 2014 @Melba23 OK, think I have found a problem, try this, when pressing Enter on the message box the focus will go to the next input and ignores the GUICtrlSetState in the checkEntry function, is it possible to stop this? expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WinAPI.au3> $hGUI = GUICreate("Test", 500, 500) $cInput_1 = GUICtrlCreateInput("", 10, 10, 200, 20) $cInput_2 = GUICtrlCreateInput("", 10, 50, 200, 20) $cInput_3 = GUICtrlCreateInput("", 10, 90, 200, 20) $cEnter = GUICtrlCreateDummy() GUISetState() Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]] GUISetAccelerators($aAccelKeys) Func checkEntry() MsgBox( 0, "Test", "Press Enter to close this message box" ) GUICtrlSetState( $cInput_1, $GUI_FOCUS ) EndFunc While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cInput_1 checkEntry() Case $cEnter Switch _WinAPI_GetFocus() Case GUICtrlGetHandle($cInput_1) GUICtrlSetState($cInput_2, $GUI_FOCUS) ; Why Send {TAB} - this is more efficient Case GUICtrlGetHandle($cInput_2) Send ("{TAB}") ; But this still does not move focus Case GUICtrlGetHandle($cInput_3) ConsoleWrite("End" & @CRLF) EndSwitch EndSwitch WEnd Link to comment Share on other sites More sharing options...
ale1981 Posted October 29, 2014 Author Share Posted October 29, 2014 Would it be possible to use this property in AutoIt? http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled(v=vs.110).aspx I could then say that the event has been handled elsewhere? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 29, 2014 Moderators Share Posted October 29, 2014 ale1981,Sorry, only just got in. I will look at both your posts later tonight / tomorrow. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 30, 2014 Moderators Share Posted October 30, 2014 ale1981,The problem is caused by your code structure - you are looking for the event of {ENTER} being pressed in an input AND as an accelerator key. Each time you press {ENTER} inside the input you are firing both events and so getting the undesired behaviour. >The way to get round this is to check the input content within the accelerator handler and then decide what to do:expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WinAPI.au3> $hGUI = GUICreate("Test", 500, 500) $cInput_1 = GUICtrlCreateInput("", 10, 10, 200, 20) $cInput_2 = GUICtrlCreateInput("", 10, 50, 200, 20) $cInput_3 = GUICtrlCreateInput("", 10, 90, 200, 20) $cEnter = GUICtrlCreateDummy() GUISetState() Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]] GUISetAccelerators($aAccelKeys) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cEnter Switch _WinAPI_GetFocus() Case GUICtrlGetHandle($cInput_1) ; Check the input content If checkEntry() = True Then ; Correct so move to next input GUICtrlSetState($cInput_2, $GUI_FOCUS) EndIf ; Not correct so stay in current input Case GUICtrlGetHandle($cInput_2) If checkEntry() = True Then GUICtrlSetState($cInput_3, $GUI_FOCUS) EndIf Case GUICtrlGetHandle($cInput_3) If checkEntry() = True Then ConsoleWrite("End" & @CRLF) EndIf EndSwitch EndSwitch WEnd Func checkEntry() ; Random chance of correct answer or fail If Mod(@SEC, 2) = 0 Then MsgBox($MB_SYSTEMMODAL, "Test Fail", "Press Enter to close this message box" ) Return False EndIf Return True EndFuncAll clear? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ale1981 Posted October 30, 2014 Author Share Posted October 30, 2014 (edited) Melba, Thanks for the reply. I see exactly what you are saying, now my problem is the Tab key does nothing, so if I amend the code to the below then press the Tab key, pressing Enter on the message box still seems to move the focus or I get two message boxes appear? expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WinAPI.au3> $hGUI = GUICreate("Test", 500, 500) $cInput_1 = GUICtrlCreateInput("", 10, 10, 200, 20) $cInput_2 = GUICtrlCreateInput("", 10, 50, 200, 20) $cInput_3 = GUICtrlCreateInput("", 10, 90, 200, 20) $cEnter = GUICtrlCreateDummy() GUISetState() Local $aAccelKeys[1][2] = [["{ENTER}", $cEnter]] GUISetAccelerators($aAccelKeys) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cInput_1 If checkEntry() = True Then ; Correct so move to next input GUICtrlSetState($cInput_2, $GUI_FOCUS) EndIf Case $cEnter Switch _WinAPI_GetFocus() Case GUICtrlGetHandle($cInput_1) ; Check the input content If checkEntry() = True Then ; Correct so move to next input GUICtrlSetState($cInput_2, $GUI_FOCUS) EndIf ; Not correct so stay in current input Case GUICtrlGetHandle($cInput_2) If checkEntry() = True Then GUICtrlSetState($cInput_3, $GUI_FOCUS) EndIf Case GUICtrlGetHandle($cInput_3) If checkEntry() = True Then ConsoleWrite("End" & @CRLF) EndIf EndSwitch EndSwitch WEnd Func checkEntry() ; Random chance of correct answer or fail If Mod(@SEC, 2) = 0 Then MsgBox($MB_SYSTEMMODAL, "Test Fail", "Press Enter to close this message box" ) Return False EndIf Return True EndFunc Edited October 30, 2014 by ale1981 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 30, 2014 Moderators Share Posted October 30, 2014 ale1981, now my problem is the Tab key does nothingIf you want the {TAB} key to run the check and move on if correct in the same manner as the {ENTER} key, just set it as another accelerator linked to the same dummy control:Local $aAccelKeys[2][2] = [["{ENTER}", $cEnter], ["{TAB}", $cEnter]]Now pressing either of the 2 keys will run the handler. M23 Skysnake 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ale1981 Posted October 30, 2014 Author Share Posted October 30, 2014 Melba I did think of doing this, but if I do that, the Tab key will only work on the inputs I set in the Accelerator handler? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 30, 2014 Moderators Share Posted October 30, 2014 ale1981,What else do you want it to do? Perhaps if you post the code of the GUI you are actually using rather than this minimal reproducer then things might become clearer. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ale1981 Posted October 30, 2014 Author Share Posted October 30, 2014 Melba, On most controls I would like the Tab key to do as it usually does but only on 3 of my inputs I want it to work with the Enter key. Can I PM you my code rather than post it here, it's 550 lines? Thanks Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 30, 2014 Moderators Share Posted October 30, 2014 ale1981,By all means send it via PM. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Moderators Solution Melba23 Posted October 30, 2014 Moderators Solution Share Posted October 30, 2014 ale1981,But what about this:expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WinAPI.au3> $hGUI = GUICreate("Test", 500, 500) $cInput_1 = GUICtrlCreateInput("", 10, 10, 200, 20) $cInput_2 = GUICtrlCreateInput("", 10, 50, 200, 20) $cInput_3 = GUICtrlCreateInput("", 10, 90, 200, 20) $cButton_1 = GUICtrlCreateButton("Button 1", 250, 10, 80, 30) $cButton_2 = GUICtrlCreateButton("Button 2", 250, 50, 80, 30) $cButton_3 = GUICtrlCreateButton("Button 3", 250, 90, 80, 30) $cEnter = GUICtrlCreateDummy() GUISetState() Local $aAccelKeys[2][2] = [["{ENTER}", $cEnter], ["{TAB}", $cEnter]] ; Make {TAB} an accelerator GUISetAccelerators($aAccelKeys) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cEnter Switch _WinAPI_GetFocus() Case GUICtrlGetHandle($cInput_1) ; Check the input content If checkEntry() = True Then ; Correct so move to next input GUICtrlSetState($cInput_2, $GUI_FOCUS) EndIf ; Not correct so stay in current input Case GUICtrlGetHandle($cInput_2) If checkEntry() = True Then GUICtrlSetState($cInput_3, $GUI_FOCUS) EndIf Case GUICtrlGetHandle($cInput_3) If checkEntry() = True Then ConsoleWrite("End" & @CRLF) EndIf Case Else GUISetAccelerators(0) ; Remove accelerator link ControlSend($hGUI, "", "", "{TAB}") ; Send {TAB} to the GUI GUISetAccelerators($aAccelKeys) ; Reset the accelerator EndSwitch EndSwitch WEnd Func checkEntry() ; Random chance of correct answer or fail If Mod(@SEC, 2) = 0 Then MsgBox($MB_SYSTEMMODAL, "Test Fail", "Press Enter to close this message box" ) Return False EndIf Return True EndFuncThat works for me - you can tab between the buttons without problem. M23 pixelsearch and ale1981 2 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ale1981 Posted October 30, 2014 Author Share Posted October 30, 2014 PM sent, just trying your new code now! Link to comment Share on other sites More sharing options...
ale1981 Posted October 30, 2014 Author Share Posted October 30, 2014 Melba, Is it possible to detect which of the Accelerator keys was pressed or would it be best to split them in to separate keys? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 30, 2014 Moderators Share Posted October 30, 2014 ale1981,You could create another dummy control and link the {TAB} key to that - then you would have separate event handler code in the idle loop. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ale1981 Posted October 30, 2014 Author Share Posted October 30, 2014 That's the route I have just tried and it works great Big thanks for all your help Melba. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 30, 2014 Moderators Share Posted October 30, 2014 ale1981,As always,, delighted I could help. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now