Triggerin Actions with Hotstrings instead of Hotkeys Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gignu
Posts: 30
Joined: 14 Jun 2019, 09:00

Triggerin Actions with Hotstrings instead of Hotkeys

06 Jul 2019, 09:59

I downloaded a script that enables me to switch to a specific virtual desktop in Windows 10. When I press the Windows Key plus a number from 1 to 9 the script will send me to the desired desktop:

Changing to specific desktop with hotkeys (here everything works fine because I'm using hotkeys):

Code: Select all

        #SC002::switchDesktopByNumber(1)
        #SC003::switchDesktopByNumber(2)
        #SC004::switchDesktopByNumber(3)
        #SC005::switchDesktopByNumber(4)
The problem is, that if I try to replace the hotkeys with hotstrings it doesn't trigger the action like it does with the hotkeys, but it writes down the text that comes after the hotstring:

Code: Select all

        ::dt1::switchDesktopByNumber(1)
        ::dt2::switchDesktopByNumber(2)
        ::dt3::switchDesktopByNumber(3)
        ::dt4::switchDesktopByNumber(4)
I also tried the "Send", "SendEvent", "SendPlay", "SendRaw" and "SendInput" commands, but nothing seems to work.

Thanks in advance for helping me out with this ;)

Here the article where you can download the original script: https www.computerhope.com /tips/tip224.htm Broken Link for safety
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Triggerin Actions with Hotstrings instead of Hotkeys

06 Jul 2019, 10:54

Hotstrings will not interpret AHK command like that. The commands must be on separate lines to achieve that:

Code: Select all

::test::
    MsgBox Hotstring "test" detected
return
gignu
Posts: 30
Joined: 14 Jun 2019, 09:00

Re: Triggerin Actions with Hotstrings instead of Hotkeys

06 Jul 2019, 11:02

You're right! Thank you so much, I was trying at least an hour to make it work, and it was just this little trick :thumbup:

Now it works:

Code: Select all

 
 		:*:dt1::
		switchDesktopByNumber(1)
		:*:dt2::
		switchDesktopByNumber(2)
		:*:dt3::
		switchDesktopByNumber(3)
		:*:dt4::
		switchDesktopByNumber(4)
		:*:dt5::
		switchDesktopByNumber(5)
		:*:dt6::
		switchDesktopByNumber(6)
		:*:dt7::
		switchDesktopByNumber(7)
		:*:dt8::
		switchDesktopByNumber(8)
		:*:dt9::
		switchDesktopByNumber(9)
		:*:cdt::
		createVirtualDesktop()
		:*:ddt::
		deleteVirtualDesktop()	
		return
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Triggerin Actions with Hotstrings instead of Hotkeys

06 Jul 2019, 11:08

Or use the X option (short for eXecute), in AHK v1.1.28+:
wrote:X [v1.1.28+]: Execute. Instead of replacement text, the hotstring accepts a command or expression to execute. For example, :X:~mb::MsgBox would cause a message box to be displayed when the user types "~mb" instead of auto-replacing it with the word "MsgBox". This is most useful when defining a large number of hotstrings which call functions, as it would otherwise require three lines per hotstring.
so...

Code: Select all

:X*:dt7::switchDesktopByNumber(7)
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Triggerin Actions with Hotstrings instead of Hotkeys

06 Jul 2019, 11:08

I found this in documentation: https://www.autohotkey.com/docs/Hotstrings.htm#intro
@ gregster Thx for sharing, I was not aware of the x option, which is on the same page :)
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Triggerin Actions with Hotstrings instead of Hotkeys  Topic is solved

06 Jul 2019, 11:21

You are welcome, wolf_II.

@gignu: This is probably not what you want

Code: Select all

:*:dt1::
switchDesktopByNumber(1)
:*:dt2::
switchDesktopByNumber(2)
:*:dt3::
switchDesktopByNumber(3)
You are missing the returns after each hotkey and like this the code flow will fall through into the next hostring definitions (this would also happen with hotkeys, btw). See for yourself:

Code: Select all

:*:dt1::
	msgbox 1
:*:dt2::
	msgbox 2
:*:dt3::
	msgbox 3
Type dt1 or dt2 and see what happens...
You can only leave out the return (without these side-effects) in one-liner notation. See my post above about the X option.

Sp, this would be correct, if you want to use the multi-line syntax:

Code: Select all

:*:dt1::
	msgbox 1
return 
:*:dt2::
	msgbox 2
return 
:*:dt3::
	msgbox 3
return 
gignu
Posts: 30
Joined: 14 Jun 2019, 09:00

Re: Triggerin Actions with Hotstrings instead of Hotkeys

07 Jul 2019, 07:19

True, thank you so much gregster!!!
I see what you mean. I tried your message box example. Makes perfect sense!

Now I did it like this and it works great :D

Code: Select all

  :X*:dt1::switchDesktopByNumber(1)
		:X*:dt2::switchDesktopByNumber(2)
		:X*:dt3::switchDesktopByNumber(3)
		:X*:dt4::switchDesktopByNumber(4)
		:X*:dt5::switchDesktopByNumber(5)
		:X*:dt6::switchDesktopByNumber(6)
		:X*:dt7::switchDesktopByNumber(7)
		:X*:dt8::switchDesktopByNumber(8)
		:X*:dt9::switchDesktopByNumber(9)
		:X*:cdt::createVirtualDesktop()
		:X*:ddt::deleteVirtualDesktop()	
		return
nou
Posts: 26
Joined: 24 Feb 2019, 21:21

Re: Triggerin Actions with Hotstrings instead of Hotkeys

08 Jul 2019, 03:32

Did you know? you can do the dt1-dt9 by doing something like this:

Code: Select all

Loop, 9
	Hotstring(":X*:dt" A_Index, "thisLabel")
return 

ThisLabel:
	switchDesktopByNumber(substr(A_ThisHotkey, 0, 1))
return 
gignu
Posts: 30
Joined: 14 Jun 2019, 09:00

Re: Triggerin Actions with Hotstrings instead of Hotkeys

08 Jul 2019, 06:17

I didn't know that, thx for mentioning it, but I'm happy with my method. It works well enough :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], peter_ahk, Rauvagol and 334 guests