Jump to content

Button -> X Actions :: X Actions -> Func :: Hotkeyset -> Func


nf67
 Share

Recommended Posts

Hi there :P,

If I have some commands under

$Case Button1

can I just paste these commands between

Func ButtonPress()
;and
EndFunc

, and then bind the function to a hotkey with

HotKeySet ("1", ButtonPress1())?

Or do I need to adjust them because the commands triggered by a button and a function don't get scripted the same way?

P.S: Will put some code up soon, it's on a different PC.

Edited by nf67
Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#Include <Timers.au3>

Global $LAST = 1
Global $Points = 0
Global $Streak = 0
Global $TIMER = TimerInit()
Global Const $TIMEOUT = 60000
$Slothor = GUICreate("Slothor", 173, 213, 192, 114)
$Commander = GUICtrlCreateEdit("1", 5, 10, 160, 120, BitOR($ES_CENTER,$ES_AUTOVSCROLL,$ES_WANTRETURN,$ES_READONLY,$ES_AUTOHSCROLL))
GUICtrlSetFont(-1, 37, 800, 0, "Arial")
$Button1 = GUICtrlCreateButton("1", 5, 128, 40, 60)
$Button2 = GUICtrlCreateButton("2", 45, 128, 40, 60)
$Button3 = GUICtrlCreateButton("3", 85, 128, 40, 60)
$Button4 = GUICtrlCreateButton("4", 125, 128, 40, 60)
$Score = GUICtrlCreateEdit("", 5, 184, 161, 25, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_WANTRETURN,$ES_READONLY))
GUICtrlSetData(-1, "Score")
GUICtrlSetFont(-1, 12, 800, 2, "Arial")
GUISetState(@SW_SHOW)

;--------------------------------------------------We're talking about this part of the code
HotKeySet ("1", Press1()) 
Func Press1()
            If $LAST = 1 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)         
            Beep ( 600, 250)
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points) 
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
EndFunc
;---------------------------------------------------------------------------------------

While 1

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If $LAST = 1 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)         
            Beep ( 600, 250)            
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points) 
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
                        
        Case $Button2
            If $LAST = 2 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)
            Beep ( 500, 250)            
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points)
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
        
        Case $Button3
            If $LAST = 3 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)
            Beep ( 400, 250)
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points)
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf
        
        Case $Button4
            If $LAST = 4 Then
            $Points = $Points+25
            $Streak = $Streak+1
            GUICtrlSetData ($Score, $Points)
            Beep (300, 250)
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            else 
            $Points = $Points-50
            $Streak = $Streak-$Streak
            GUICtrlSetData ($Score, $Points)
            GUICtrlSetData($Commander, "(-.-)"&@crlf, 1)
            Beep ( 100, 1500 )
            $LAST = Random(1,4,1)
            GUICtrlSetData($Commander, $LAST&@crlf, 1)
            EndIf

    EndSwitch
    
    If $Streak=15 Then
    $Points=$Points+200
    GUICtrlSetData ($Score, $Points)
    GUICtrlSetData($Commander, "+200"&@crlf, 1)
    Beep (900, 1500)
    $Streak = $Streak+1
    $LAST = Random(1,4,1)
    Sleep (300)
    GUICtrlSetData($Commander, $LAST&@crlf, 1)
    EndIf   
            
    If TimerDiff($TIMER) > $TIMEOUT Then
    $RetryAnswer = MsgBox(36, "Game Over", "You scored "&$Points &" points. Retry?", 5000)
        Select
            Case $RetryAnswer = 6
            WinKill ( "Game Over" ) 
            FileWrite(@ScriptDir & "\Scores.txt", $Points&@CRLF)
            $Streak = 0
            $Points = 0
            GuiCtrlSetData ($Score, $Points)
            _Timer_KillTimer($Slothor, $TIMER)
            Global $TIMER = TimerInit()
            Case $RetryAnswer = 7 
            WinKill ( "Game Over" )
            GUISetState(@SW_HIDE, $Slothor)
            FileWrite(@ScriptDir & "\Scores.txt", $Points&@CRLF)
                $Scores = FileRead(@ScriptDir & "\Scores.txt")
                $Highscores = GUICreate("Highscores", 163, 58, 193, 115)
                $ScoreView = GUICtrlCreateEdit("Scores:"&@CRLF & $Scores, 0, 0, 161, 57, BitOR($ES_WANTRETURN,$WS_VSCROLL,$ES_READONLY))
                GUISetState(@SW_SHOW)
                        While 3
                            $msg3 = GUIGetMsg()
                            Select
                                Case $msg3 = $GUI_EVENT_CLOSE
                                WinKill ("Slothor")
                                ExitLoop
                            EndSelect
                        WEnd
        EndSelect
    EndIf
WEnd

As you can see, I want to let the Hotkey do the same thing as Button1 by using a function.

Edited by nf67
Link to comment
Share on other sites

Replace your statements under your Case command with the function call:

While 1

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
           Press1()

Edit: By the way, your syntax for HotkeySet is wrong:, you don't use the brackets after the function name, and you need to put the function name in quotes:

should be

HotKeySet ("1", "Press1")
Edited by ResNullius
Link to comment
Share on other sites

Please, you don't need to send me two PM's

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>

Global $LAST = 1
Global $Points = 0
Global $Streak = 0
Global $TIMER = TimerInit()
Global Const $TIMEOUT = 60000

HotKeySet("1", "Press1")

$Slothor = GUICreate("Slothor", 173, 213, 192, 114)
$Commander = GUICtrlCreateEdit("1", 5, 10, 160, 120, BitOR($ES_CENTER, $ES_AUTOVSCROLL, $ES_WANTRETURN, $ES_READONLY, $ES_AUTOHSCROLL))
GUICtrlSetFont(-1, 37, 800, 0, "Arial")
$Button1 = GUICtrlCreateButton("1", 5, 128, 40, 60)
$Button2 = GUICtrlCreateButton("2", 45, 128, 40, 60)
$Button3 = GUICtrlCreateButton("3", 85, 128, 40, 60)
$Button4 = GUICtrlCreateButton("4", 125, 128, 40, 60)
$Score = GUICtrlCreateEdit("", 5, 184, 161, 25, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_WANTRETURN, $ES_READONLY))
GUICtrlSetData(-1, "Score")
GUICtrlSetFont(-1, 12, 800, 2, "Arial")
GUISetState(@SW_SHOW)


While 1

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Press1()
            
        Case $Button2
            If $LAST = 2 Then
                $Points = $Points + 25
                $Streak = $Streak + 1
                GUICtrlSetData($Score, $Points)
                Beep(500, 250)
                $LAST = Random(1, 4, 1)
                GUICtrlSetData($Commander, $LAST & @CRLF, 1)
            Else
                $Points = $Points - 50
                $Streak = $Streak - $Streak
                GUICtrlSetData($Score, $Points)
                GUICtrlSetData($Commander, "(-.-)" & @CRLF, 1)
                Beep(100, 1500)
                $LAST = Random(1, 4, 1)
                GUICtrlSetData($Commander, $LAST & @CRLF, 1)
            EndIf

        Case $Button3
            If $LAST = 3 Then
                $Points = $Points + 25
                $Streak = $Streak + 1
                GUICtrlSetData($Score, $Points)
                Beep(400, 250)
                $LAST = Random(1, 4, 1)
                GUICtrlSetData($Commander, $LAST & @CRLF, 1)
            Else
                $Points = $Points - 50
                $Streak = $Streak - $Streak
                GUICtrlSetData($Score, $Points)
                GUICtrlSetData($Commander, "(-.-)" & @CRLF, 1)
                Beep(100, 1500)
                $LAST = Random(1, 4, 1)
                GUICtrlSetData($Commander, $LAST & @CRLF, 1)
            EndIf

        Case $Button4
            If $LAST = 4 Then
                $Points = $Points + 25
                $Streak = $Streak + 1
                GUICtrlSetData($Score, $Points)
                Beep(300, 250)
                $LAST = Random(1, 4, 1)
                GUICtrlSetData($Commander, $LAST & @CRLF, 1)
            Else
                $Points = $Points - 50
                $Streak = $Streak - $Streak
                GUICtrlSetData($Score, $Points)
                GUICtrlSetData($Commander, "(-.-)" & @CRLF, 1)
                Beep(100, 1500)
                $LAST = Random(1, 4, 1)
                GUICtrlSetData($Commander, $LAST & @CRLF, 1)
            EndIf

    EndSwitch

    If $Streak = 15 Then
        $Points = $Points + 200
        GUICtrlSetData($Score, $Points)
        GUICtrlSetData($Commander, "+200" & @CRLF, 1)
        Beep(900, 1500)
        $Streak = $Streak + 1
        $LAST = Random(1, 4, 1)
        Sleep(300)
        GUICtrlSetData($Commander, $LAST & @CRLF, 1)
    EndIf

    If TimerDiff($TIMER) > $TIMEOUT Then
        $RetryAnswer = MsgBox(36, "Game Over", "You scored " & $Points & " points. Retry?", 5000)
        Select
            Case $RetryAnswer = 6
                WinKill("Game Over")
                FileWrite(@ScriptDir & "\Scores.txt", $Points & @CRLF)
                $Streak = 0
                $Points = 0
                GUICtrlSetData($Score, $Points)
                _Timer_KillTimer($Slothor, $TIMER)
                Global $TIMER = TimerInit()
            Case $RetryAnswer = 7
                WinKill("Game Over")
                GUISetState(@SW_HIDE, $Slothor)
                FileWrite(@ScriptDir & "\Scores.txt", $Points & @CRLF)
                $Scores = FileRead(@ScriptDir & "\Scores.txt")
                $Highscores = GUICreate("Highscores", 163, 58, 193, 115)
                $ScoreView = GUICtrlCreateEdit("Scores:" & @CRLF & $Scores, 0, 0, 161, 57, BitOR($ES_WANTRETURN, $WS_VSCROLL, $ES_READONLY))
                GUISetState(@SW_SHOW)
                While 3
                    $msg3 = GUIGetMsg()
                    Select
                        Case $msg3 = $GUI_EVENT_CLOSE
                            WinKill("Slothor")
                            ExitLoop
                    EndSelect
                WEnd
        EndSelect
    EndIf
WEnd


; ------------------ Functions -------------------------------

Func Press1()
    If $LAST = 1 Then
        $Points = $Points + 25
        $Streak = $Streak + 1
        GUICtrlSetData($Score, $Points)
        Beep(600, 250)
        $LAST = Random(1, 4, 1)
        GUICtrlSetData($Commander, $LAST & @CRLF, 1)
    Else
        $Points = $Points - 50
        $Streak = $Streak - $Streak
        GUICtrlSetData($Score, $Points)
        GUICtrlSetData($Commander, "(-.-)" & @CRLF, 1)
        Beep(100, 1500)
        $LAST = Random(1, 4, 1)
        GUICtrlSetData($Commander, $LAST & @CRLF, 1)
    EndIf
EndFunc   ;==>Press1

8)

NEWHeader1.png

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