Jump to content

Hotkey to trigger GUI button


Go to solution Solved by kylomas,

Recommended Posts

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?

Func _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

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

use hot key set like this <<<<<<<<<<<<<<<

Func _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

  • Solution

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...

Func _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

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

  • 2 weeks later...

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...