niftyapple Posted February 27, 2015 Share Posted February 27, 2015 Here is a snippet of code that creates a singel GUI with simply a dropdown and an OK button. For some reason I am lapsing on how to get a hotkey to trigger the switch event for the OK button. My question, do I make a new case for the hotkey or can I trigger the case directly with the hotkey? Could I @hotkeypressed a case and run the necessary from another function? expandcollapse popupFunc _RunSingle() local $pFile = $path & "programs.txt" ;~ ensure file is in its spot before loading if NOT FileExists($pFile) Then FileWrite($pFile, "") EndIf local $count = _FileCountLines($pFile) if $count = 0 Then $count = 1 EndIf ;~Check for an empty file, write something to the first slot. will be removed later. Local $plist[$count] If FileGetSize($pfile) <= 1 Then $plist[0] = "" Else _FileReadToArray($pFile, $plist) for $i = UBound($plist) - 1 to 0 Step -1 if $plist[$i] = "" OR $plist[$i] = " " OR $plist[$i] = "|" Then _ArrayDelete($plist, $i) EndIf Next _ArrayDelete($plist, 0) EndIf ;~ Make sure there is data to put into the control before showing the GUI If UBound($plist) > 0 Then $GUISingle = GUICreate("Run Once", 500, 300) $GUISingleL1 = GUICtrlCreateCombo("", 10, 10, 200, 100) $GUISingleB1 = GUICtrlCreateButton("OK", 10, 130, 100, 100) For $t in $plist GUICtrlSetData($GUISingleL1, $t) Next GUISetState(@SW_SHOW, $GUISingle) While 1 HotKeySet("{ENTER}", "_pressedOK") Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $GUISingleB1 $runsingle = GUICtrlRead($GUISingleL1) if $runsingle = "" Then msgbox(0, "Error", "That is not a correct item. Please select one from the drop down.") Else Local $show[2] $show = _GetContents() RunAs(_decrypt($show[0]), $domain, _decrypt($show[1]), 2, $runsingle) ExitLoop EndIf EndSwitch WEnd GUISetState(@SW_HIDE, $GUISingle) EndIf EndFunc Link to comment Share on other sites More sharing options...
JohnOne Posted February 27, 2015 Share Posted February 27, 2015 You want to automate a button on your own GUI? Why? That;s crazy. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
orbs Posted February 27, 2015 Share Posted February 27, 2015 GUISetAccelerators Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
reb Posted February 27, 2015 Share Posted February 27, 2015 use hot key set like this <<<<<<<<<<<<<<< expandcollapse popupFunc _RunSingle() Local $pFile = $path & "programs.txt" ;~ ensure file is in its spot before loading If Not FileExists($pFile) Then FileWrite($pFile, "") EndIf Local $count = _FileCountLines($pFile) If $count = 0 Then $count = 1 EndIf ;~Check for an empty file, write something to the first slot. will be removed later. Local $plist[$count] HotKeySet("{ENTER}", "$GUISingleB1") ;<<<<<<<<<<<<<<< Note Placement is not in your loop If FileGetSize($pFile) <= 1 Then $plist[0] = "" Else _FileReadToArray($pFile, $plist) For $i = UBound($plist) - 1 To 0 Step -1 If $plist[$i] = "" Or $plist[$i] = " " Or $plist[$i] = "|" Then _ArrayDelete($plist, $i) EndIf Next _ArrayDelete($plist, 0) EndIf ;~ Make sure there is data to put into the control before showing the GUI If UBound($plist) > 0 Then $GUISingle = GUICreate("Run Once", 500, 300) $GUISingleL1 = GUICtrlCreateCombo("", 10, 10, 200, 100) $GUISingleB1 = GUICtrlCreateButton("OK", 10, 130, 100, 100) ;<<<<<<<<<<<<<<< Assigned to here For $t In $plist GUICtrlSetData($GUISingleL1, $t) Next GUISetState(@SW_SHOW, $GUISingle) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $GUISingleB1 $runsingle = GUICtrlRead($GUISingleL1) If $runsingle = "" Then MsgBox(0, "Error", "That is not a correct item. Please select one from the drop down.") Else Local $show[2] $show = _GetContents() RunAs(_decrypt($show[0]), $domain, _decrypt($show[1]), 2, $runsingle) ExitLoop EndIf EndSwitch WEnd GUISetState(@SW_HIDE, $GUISingle) EndIf EndFunc ;==>_RunSingle MEASURE TWICE - CUT ONCE Link to comment Share on other sites More sharing options...
Solution kylomas Posted February 27, 2015 Solution Share Posted February 27, 2015 niftyapple, If your purpose is to action the button when the enter key is pushed then an accelerator key (as orb pointed out) can do it like this... expandcollapse popupFunc _RunSingle() local $pFile = $path & "programs.txt" ;~ ensure file is in its spot before loading if NOT FileExists($pFile) Then FileWrite($pFile, "") EndIf local $count = _FileCountLines($pFile) if $count = 0 Then $count = 1 EndIf ;~Check for an empty file, write something to the first slot. will be removed later. Local $plist[$count] If FileGetSize($pfile) <= 1 Then $plist[0] = "" Else _FileReadToArray($pFile, $plist) for $i = UBound($plist) - 1 to 0 Step -1 if $plist[$i] = "" OR $plist[$i] = " " OR $plist[$i] = "|" Then _ArrayDelete($plist, $i) EndIf Next _ArrayDelete($plist, 0) EndIf ;~ Make sure there is data to put into the control before showing the GUI If UBound($plist) > 0 Then $GUISingle = GUICreate("Run Once", 500, 300) $GUISingleL1 = GUICtrlCreateCombo("", 10, 10, 200, 100) $GUISingleB1 = GUICtrlCreateButton("OK", 10, 130, 100, 100) $dummy_enter = GUICtrlCreatedummy() ; <----- add dummy def ; accelerator key definition Dim $aAccelKeys[1][2] = [["{ENTER}", $dummy_enter]] GUISetAccelerators($aAccelKeys) For $t in $plist GUICtrlSetData($GUISingleL1, $t) Next GUISetState(@SW_SHOW, $GUISingle) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $GUISingleB1, $dummy_enter ; <----- add dummy ctl $runsingle = GUICtrlRead($GUISingleL1) if $runsingle = "" Then msgbox(0, "Error", "That is not a correct item. Please select one from the drop down.") Else Local $show[2] $show = _GetContents() RunAs(_decrypt($show[0]), $domain, _decrypt($show[1]), 2, $runsingle) ExitLoop EndIf EndSwitch WEnd GUISetState(@SW_HIDE, $GUISingle) EndIf EndFunc not tested obviously kylomas llc 1 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 Link to comment Share on other sites More sharing options...
niftyapple Posted March 11, 2015 Author Share Posted March 11, 2015 Sorry for the late reply, but GUISetAccelerators was the key. Thanks! 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