[Q] assignment and value

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
av930
Posts: 31
Joined: 24 Dec 2017, 10:21

[Q] assignment and value

03 Feb 2018, 01:10

Hello, I am a newbie
I don't understand why this happens
please look at this example below.

Code: Select all

ProgramKey := []
ProgramKey.cmd := "^+s"

If % ProgramKey["cmd"] is alnum  {                
    Msgbox, ,"alnum" ,% ProgramKey["cmd"]    ;;; this is working, even though value of ProgramKey["cmd"] is "^+s" 
    temp := ProgramKey["cmd"]
    If temp is alnum                   ;;; but, this not working, value is ^+s  without double quote.
        msgbox, ,"temp", "test"
}
I don't get it. please explain me..
I need to make this working : If % ProgramKey["cmd"] is alnum
Is there any way to do it.
Last edited by av930 on 03 Feb 2018, 07:03, edited 1 time in total.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: [Q] assignment and value

03 Feb 2018, 04:29

"^+s" is not alpha-numeric. You can see this by simplifying your code to:

Code: Select all

ProgramKey := []
ProgramKey.cmd := "^+s"

temp := ProgramKey["cmd"]
If temp is alnum
  MsgBox, That is alpha-numeric!
The condition is not satisfied; whereas, if you change "^+s" to, simply, "s", it is satisfied.

As for why your If % ProgramKey["cmd"] is alnum statement is satisfied, my guess is that it's a fluke. According to the help file, forcing expressions (with %) "can be used in the InputVar parameters of all commands except the traditional IF commands (use If (expression) instead)." A traditional IF statement is what you're using. On top of that, there's another note in the help file that says that "is" can't be used in expressions. Both of these probably should throw errors, but, instead, the statement is somehow evaluating to true when, as I showed above, it should not be.

Anyways, since checking for alnum is going to fail when the code is correct, you should probably scrap that idea and check for something else, instead. For example, if what you're doing is checking whether a given hotkey is valid, you can use the IsLabel() function, like so:

Code: Select all

ProgramKey := []
ProgramKey.cmd := "^+s"

If IsLabel(ProgramKey["cmd"]) {
  MsgBox, That is a valid hotkey!
}

^+s::Return
av930
Posts: 31
Joined: 24 Dec 2017, 10:21

Re: [Q] assignment and value

03 Feb 2018, 07:15

I cannot use IsLabel() function, because this is small part of my code, there are lots of hotkeys, I cannot define all of it, they are easily changed.

Is there any way to get correct value and compare if it is alnum ?
something like this..

Code: Select all

If ProgramKey["cmd"].getvalue() is alnum
In Addition, ProgramKey["cmd"] in my code can have these values.
1. object
2. string
3. hotkey

when I get the content, I need to know which type of command is in.
av930
Posts: 31
Joined: 24 Dec 2017, 10:21

Re: [Q] assignment and value

03 Feb 2018, 10:32

Ok I found a way, please refer if you have a interest
I use the traditional and expression.

Code: Select all

ProgramKey := []
;ProgramKey.cmd := "^+s"
ProgramKey.cmd := "adsfadsfasd423523"
;ProgramKey.cmd := Func( "_OSHotKeys" ) 


CMD := ProgramKey["cmd"]

if IsObject( CMD ) {
    msgBox, , "object", % CMD
}
Else If CMD is alnum  
{
    Msgbox, ,"alnum" ,% CMD   ;;; this is working, value is "^+s"
}
Else{
    msgBox, , "hotkey", % CMD
}

_OSHotkeys() {
    ListHotkeys
}
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: [Q] assignment and value

03 Feb 2018, 10:54

In regards to OP's code appearing to "work" and make the If statement return True, this is because everything after the % is its own variable. Essentially, if any of ProgramKey["cmd"] is alnum hold a true value, the whole thing returns true; kind of like using OR operators. But what is really going on is concatenation -- myVar:=ProgramKey["cmd"] is alnum = myVar:=ProgramKey["cmd"] . is . alnum; it's just that is and alnum are blank.

As far as I know, the suggested/discovered work around is to assign the particular object's key's value to an independent variable to then use in the IfIs command is the proper way to handle it. I think you could do some RegExMatch(), like using

Code: Select all

ProgramKey := []
;ProgramKey.cmd := "^+s"
ProgramKey.cmd := "adsfadsfasd423523"

If !RegExMatch(ProgramKey.cmd,"\W")
   MsgBox it is alnum (plus underscore)
\W means any non-word character (lowercase w would mean word character); word characters are A-Z, a-z, 0-9 *and underscore* _. So if the RegExMatch() comes back true, we know it is NOT alnum; the ! (logical-not operator) is out front to make it so we're looking for "If NOT NOT alnum" where the NOTs "cancel" -> "If alnum".
;"
If you want to make sure underscore isn't counted, you could use "[^a-zA-Z0-9" instead of "\W", I think.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Descolada, Oblomov228 and 175 guests