Hobbyist Posted September 2, 2014 Posted September 2, 2014 I am attempting to code "Buttons" for "Enter" functions. Found some post but still pretty much not sure about what I am doing and what I am doing wrong. In the code attached I have two buttons and would like to be able to have either one do their own code - I have for testing purposes used Msg Boxes just to look for success or fail. Found failure. I keep getting the same MsbBox regardless of which button I use. I want to be able to tab to either button and then hit the enter key - that seems to work but given what I mentioned above I would be executing the same code on one button regardless of choice. Don't want the Enter key used on other controls to cause execution of these two button. Would appreciated any help and guidance. Thanks for help on this and any and all help in previous posts. Hobbyist expandcollapse popup;<<<<<<<<<<<<<<<<<<<<<<<< #include <Array.au3> ;for American Express function #include <ButtonConstants.au3> #include <ColorConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> ;<<<<<<<<<<<< #Region ### START Koda GUI section ### Form=C:\Users\Steven\Autoit Trys\Vendors Trials\My combo Form Test.kxf $main = GUICreate("Buttons", 680, 515, 150, 100) ;height was 480 $Button12 = GUICtrlCreateButton("Save Files", 10, 60, 158, 33) GUICtrlSetState($Button12,$GUI_focus) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0xFF0000) GUICtrlSetBkColor(-1, 0xE3E3E3) $Button13 = GUICtrlCreateButton("Delete Record", 10, 100, 158, 33) GUICtrlSetState($Button13,$GUI_enable) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0xFF0000) GUICtrlSetBkColor(-1, 0xE3E3E3) $Label22 = GUICtrlCreateLabel("Category", 16, 8, 54, 17) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") GUISetState(@SW_SHOW) ;#EndRegion ### END Koda GUI section ### $cEnterPressed = GUICtrlCreateDummy() Global $aAccelKeys[1][2] = [["{ENTER}", $cEnterPressed]] GUISetAccelerators($aAccelKeys) global $choiceswitch = 1 ;choose between button12 or button13 While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cEnterPressed ;strike ENTER key to run code for button selected switch $choiceswitch Case 1 GUICtrlSetState ($button12, $GUI_FOCUS) MsgBox(4096, "Test", "[Enter] key is detected! SAVE ") ;actually would be calling a function Case 2 GUICtrlSetState ($button13, $GUI_FOCUS) MsgBox(4096, "Test", "[Enter] Delete !") ;actually would be calling a function EndSwitch WEnd
Moderators Melba23 Posted September 2, 2014 Moderators Posted September 2, 2014 Hobbyist, Don't want the Enter key used on other controls to cause execution of these two button.Then remove the colouring from the buttons. There is a bug deep in the AutoIt core (which is not going to be fixed any time soon) which means that coloured buttons have a few problems - among which is trapping the {ENTER} key. 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
Hobbyist Posted September 2, 2014 Author Posted September 2, 2014 Thanks Melba! Did not know that little treasure existed or could haunt me. I made the change in the above code - just commented out those lines. I tried a few other directions while at it, but I obviously haven't grasped it all, as I'm still on square one. Hobbyist
kylomas Posted September 2, 2014 Posted September 2, 2014 @M23 *ducking behind tree* The problem is that $choiceswitch is set to "1" and will alway match that case stmt. @Hobbyist - Your use of button seem unusual. Can you explain what you are trying todo? kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
Moderators Melba23 Posted September 2, 2014 Moderators Posted September 2, 2014 kylomas,No need to duck. I did not even look closely at the code - as soon as I saw the GUICtrlSetColor & GUICtrlSetBkColor lines I thought that was likely to be the cause of the problem. Thanks for taking the time to look more deeply. 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
Hobbyist Posted September 2, 2014 Author Posted September 2, 2014 @kylomas First - I am sooooo very new to this. The use of the buttons on the attached code (ie calling a MsgBox) is just to see if I was successful in the script. Other than that it would be silly to do such a thing........... I want to be able to know how to have more than one button on a form and have each button initiate some code/function when I hit the Enter key on the keyboard. Point and click has been no problem. I learned about the accelerator keys and now want to put it to use(included in my code). So I started with a two button experiment model. So basically I'm attempting to put a button on a form and use it like the Enter key on the keyboard. For instance I have a button on a form that when clicked saves files. So my next thought was - why not use the Enter key as well. Melba recently showed me the use of "Switch" in a script - I can CLEARLY see now having looked over my post that the way I used it here is very incorrectly. None the less, I am still lost has how to do it. I recognize "something" has to determine which button choice gets executed, just not sure of the nuts and bolts. I hope I have cleared up any uncertainties in my post. Thanks Hobbyist
kylomas Posted September 2, 2014 Posted September 2, 2014 (edited) Hobbyist, Do you want to do something for all buttons everytime the enter key is hit? Other than that you would not normally find more than one button associated with the enter key because buttons imply that you are doing something specific for each button. Or perhaps I don't understand your situation yet. kylomas edit: This is a piece of your code: Case $cEnterPressed ;strike ENTER key to run code for button selected What do you mean by "button selected"? Edited September 2, 2014 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
Hobbyist Posted September 2, 2014 Author Posted September 2, 2014 (edited) For example on the same form (no tabs): Button#1 = Save a file, by way of its associated coding, in a function pershaps. Hitting the Enter key on the keyboard would then run the code. Button#2 = Delete records according to specific coding. Hitting the Enter key on the keyboard would then run the code. I would be selecting Button #1 or Button #2, neither being dependent upon the other. I could make one button have the default focus and think I should be able to hit Enter and execute the code. Alternatively I could tab to the second button and hit Enter. Yes, each button is specific. They are not acting together. So I was (in my first post) using $cEnterPressed as the Enter or hotkey. And thought the use of "switch" would then be used for which ever Button had the focus. Edited September 2, 2014 by Hobbyist
misioooo Posted September 3, 2014 Posted September 3, 2014 (edited) So in general - you want to be able to TAB through all the buttons and press them using Enter? - using GUI without a mouse? In taht case look at https://www.autoitscript.com/autoit3/docs/appendix/GUIStyles.htm There are some styles that allow/disallow of selecting given control using TAB key etc. {Enter} key works out-of-the box for me (i can {tab} through buttons in my GUI and pressing {enter} is the same as mouse click on selected button). GuiSetAccelerators is for creating keyboard shortcuts for GUI controls (including buttons), not for managing [enter] and [tab]. Edited September 3, 2014 by misioooo
Hobbyist Posted September 3, 2014 Author Posted September 3, 2014 @misiooo You are correct in your understanding of my objective. I have had out of the box programs in the past that function that way. I didn't think it would be such a big problem in Autoit. Thanks for the link. I didn't know there were some constraints on some controls.
misioooo Posted September 3, 2014 Posted September 3, 2014 (edited) But using only keyboard to control Autoit-made GUI works out of the box! #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Local $button1, $button2, $label1 ;WE MUST CREATE GUI: GUICreate("MyGui",@DesktopWidth,@DesktopHeight) $button1 = GUICtrlCreateButton("Button 1",100,100,100,100) ;we create button 1, size 100x100 and starting position (top-left corner) 100/100 $button2 = GUICtrlCreateButton("Button 2",100+120,100,100,100) ; we create second button, to the right of first one $label1 = GUICtrlCreateLabel("This is some sample label, it is not a button",100,100+120,200,100) ;some label - control that shouldnt be accessible through {tab} GUISetState(@SW_SHOW) ;needed so GUI will be shown after script starts running While 1 Switch GUIGetMsg() ;we are polling for some feedback from user, if any control is clicked/changed etc... Case $GUI_EVENT_CLOSE ;close the gui when we press "close window" button ({esc} also works) Exit Case $button1 ;when we press button 1 - let it be by mouse or {tab}/{enter}... MsgBox(0,"","Button 1 pressed!") ;do something Case $button2 ;when we press button 2 - let it be by mouse or {tab}/{enter}... MsgBox(0,"","Button 2 pressed!") ;do something EndSwitch WEnd Try above code - it just works using {tab} and {enter} - no need to program anything special there! Just create controls, buttons etc. Edited September 3, 2014 by misioooo
Hobbyist Posted September 3, 2014 Author Posted September 3, 2014 @misiooo Just went to the link and I didn't find anything that would impede the coding of the functionality I am referring to.
Hobbyist Posted September 3, 2014 Author Posted September 3, 2014 @misioooo Just pasted the While....Wend portion of your example into my code listed above (and changed names so everything matched up). Your code does in fact work IF I comment out the following in my code: $cEnterPressed = GUICtrlCreateDummy() Global $aAccelKeys[1][2] = [["{ENTER}", $cEnterPressed]] GUISetAccelerators($aAccelKeys) I have included the above in other parts of my code for functionality and everything there works, so excluding it now seems less than desirable. I am a rookie at all this, but it seems to me "Enter" in either case should be workable. Unless there is a huge Autoit constraint, I would think its plausible.
misioooo Posted September 3, 2014 Posted September 3, 2014 (edited) What you did is redefined what enter does... (using accelerators)... That kind of messing with keyboard input will mess ANY program in ANY language - it is just like you change keyboard layout in windows... It is changed for everything you do. So imo it is not AutoIt fault or constraint. It is strictly programmer error Enter is strictly tied to dummy control and looses its "overal, standard" behaviour. In general - if u need some shortcuts use shortcuts like mix of ctrl+something etc. Not enter, esc or any other single, commonly used key. Some time ago i did the same thing with ESC key and ctrl+x shortcut (default for "cut")... Edited September 3, 2014 by misioooo
Hobbyist Posted September 4, 2014 Author Posted September 4, 2014 Ok - so looking at the code by misioooo, I'm thinking herein lies a solution. First I am using Accelerators, which misioooo points out messes up my ENTER key. Second I have a need for Accelerator to work in other parts of the code. Third - shouldn't it be possible for me to turn Accelerator on and off in the same script - thus using it when I need it and turning it off when I don't???? I was thinking GUISetAccelerators(0) to turn it off. Not sure if this would work. Anybody???? Would it be as easy as something like : IF GUICtrlSetState($Button12,$GUI_FOCUS) then GUISetAccelerators(0) endif Thanks.
Solution misioooo Posted September 5, 2014 Solution Posted September 5, 2014 From GUISetAccelerators() help file: Passing this function a non-array will unset all accelerators for the given winhandle. So yes, if you pass single value (not array) should un-set all accelerators. Or you can pass it arrai, but with modified entry for Enter: $cEnterPressed = GUICtrlCreateDummy() Global $aAccelKeys[1][2] = [["{ENTER}", $cEnterPressed]] GUISetAccelerators($aAccelKeys) if GUICtrlSetState($Button12,$GUI_FOCUS) then GUISetAccelerators(0) endif
Hobbyist Posted September 5, 2014 Author Posted September 5, 2014 Thank you much misiooo!! I have learned much from this topic/post. This gives me the best of both worlds for my application. Hobbyist
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