简体   繁体   中英

Can't get functions to work properly

So I just got started with auto hotkeys and I'm having a bit o trouble getting my functions and variables to play nicely..

This is what I have:

PSbri0 = C:\Controls\Set_0Bri_PS.bat
PSbri50 = C:\Controls\Set_50Bri_PS.bat
PSbri100 = C:\Controls\Set_100Bri_PS.bat
HPbri0 = C:\Controls\Set_0Bri_HP.bat
HPbri50 = C:\Controls\Set_50Bri_HP.bat
HPbri100 = C:\Controls\Set_100Bri_HP.bat

Run %PSbri0%

current_setting := 0 
current_power := 0 ;Power Saver = 0 High performance = 1

setPower(){
    global current_power
    if(%current_power% == 1){
        MsgBox Pow 1
        %current_power% := 0
    }else{
        MsgBox pow 2
        %current_power% := 1
    }   
}

getChange(direction)
{
    global current_power
    MsgBox dir %direction%
    if (%current_power% == 0){
        ;MsgBox run 0
        getChangePS(%direction%)
    }
    else if (%current_power% == 1){
        ;MsgBox run 1
        getChangeHP(%direction%)    
    }
}

getChangePS(direction)
{
    global current_setting
    ;MsgBox %current_setting%
    MsgBox Direction: %direction%

    if(direction == 1){
        ;MsgBox %current_setting%
        if(%current_setting% == 0){
        }
        else if(%current_setting% == 50){
            %current_setting% := 0
            Run %PSbri0%
        }
        else if(%current_setting% == 100){
            %current_setting% := 50
            Run %PSbri50%
        }
    }    
    else if(direction == 0){
    ;MsgBox %current_setting%
        if(%current_setting% == 100){
        }
        else if(%current_setting% == 50){
            %current_setting% := 100
            Run %PSbri100%
        }
        else if(%current_setting% == 0){
            %current_setting% := 50
            Run %PSbri50%
        }
    }
}
getChangeHP(direction)
{
    global

    if(direction == 1){
        ;MsgBox %current_setting%
        if(%current_setting% == 0){
        }
        else if(%current_setting% == 50){
            %current_setting% := 0
            Run %HPbri0%
        }
        else if(%current_setting% == 100){
            %current_setting% := 50
            Run %HPbri50%
        }
    }    
    else if(direction == 0){
        ;MsgBox %current_setting%
        if(%current_setting% == 100){
        }
        else if(%current_setting% == 50){
            %current_setting% := 100
            Run %HPbri100%
        }
        else if(%current_setting% == 0){
            %current_setting% := 50
            Run %HPbri50%
        }
    }
}

^F5:: getChange(0)
^F6:: getChange(1)
!^P:: setPower()

I've been looking over the documentation and through other posts online but I cannot seem to find out what I'm doing wrong..

My intended goal is to be able to switch power profiles easily I have the 6 profiles declared in the beginning with thee levels of high performance (HP) and three Power Saver levels(PS) I want to use Alt + Ctrl + P to change between power options and the F5 / F6 to add brightness or dim the screen. The bat files work perfectly and they change my settings as they should so I know that isn't an issue..

I've already tried without the global declaration in the functions first but that didn't work and neither does it with the declaration.

Thanks for your help guys!

Sorry to say but you are using the % in way too many places

Its use depends on where you are using the variable if it in an expression you do not use % around variables, in most other places you do, next thing is then to know when you're typing in something that accepts an expression, that take a little time to learn but here are two links that may help

http://ahkscript.org/docs/Variables.htm#Expressions

http://www.autohotkey.com/board/topic/118109-hard-rules-when-using-variables/

Here is a little example of how it needs to look

setPower(){
    global current_power
    if (current_power == 1){
        MsgBox Pow 1
        current_power := 0
    }else{
        MsgBox pow 2
        current_power := 1
    }   
}

getChange(direction)
{
    global current_power
    MsgBox dir %direction%
    if (current_power == 0){
        ;MsgBox run 0
        getChangePS(direction)
    }
    else if (current_power == 1){
        ;MsgBox run 1
        getChangeHP(direction)    
    }
}

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM