Script to run a different shortcut each time is run

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Script to run a different shortcut each time is run

10 Mar 2018, 05:35

Hi All,

Kinda new to this autohotkey world, hopefully someone can point me in the right direction!

I'm trying to create a script which each time it is run it sends a different keyboard shortcut. Reason being I have an application which I need to change between mouse cursors. In the application each mouse cursor type has a different keyboard shortcut assigned to it, but no way to toggle between them.

This is kinda how I expect it should work, but been reading that I might need to use multithreading or such. So I'm a little confused... Here goes!

Set toggle=hand

While true do (us if if while not available)
Wait till key press Detected

If toggle = cross
Run alt+h
Set toggle=hand
Else
Run alt+3
toggle=cross

Return while

Thank you in advance, looking forward to learning this powerful tool!

Sunny
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script to run a different shortcut each time is run

10 Mar 2018, 06:50

I'm not too clear what you want, however, I would imagine that #If (possibly), A_Cursor (most likely), and KeyWait might be useful:
#If
https://autohotkey.com/docs/commands/_If.htm
Variables and Expressions
https://autohotkey.com/docs/Variables.htm#Cursor
KeyWait
https://autohotkey.com/docs/commands/KeyWait.htm

Code: Select all

;Also, probably you don't mean:
 Run alt+h
;But:
 Send !h

;And, you don't use Set, not:
 Set toggle=hand
;But:
 toggle=hand
 
;For A_Cursor:
MsgBox, % A_Cursor
if (A_Cursor = "Cross")
if (A_Cursor = "Unknown")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

10 Mar 2018, 09:09

Many thanks Jeeswg :)

Sorry I shouldn't have mentioned the cursor thing, that's handled by the application I'm running and I only need to run a one of two key combination to change the cursors. Autohotkey doesn't need to do any Cursor tricks. Hope that makes it clearer

I think you're right, I will need 'if', probably and also KeyWait... not sure if I need to enclose the if statement with a loop somehow or if the KeyWait would do that for me automatically?


What i'm after is this...

When the script runs it initially sets the variable 'toggle' to 'hand'.

Then enters into a loop which runs indefinitely, waits for a key combination to be received and then runs the if statement below

The If statement checks if toggle = cross then run key combination A and set toggle to 'cross'

else runs key combination B and set toggle to 'hand'

Then back to top of the main loop waiting for key combination


Thanks,
Sunny
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script to run a different shortcut each time is run

10 Mar 2018, 11:08

waits for a key combination to be received
- A key combination pressed by the user?
- This script may do something like what you want.
- Do you actually want to send alt+something else, usually that just displays a menu.

Code: Select all

toggle := "hand"

q::
if (toggle = "cross")
{
	SendInput, !h
	toggle := "hand"
}
else
{
	SendInput, !3
	toggle := "cross"
}
return

;simpler equivalent script
w::
toggle := !toggle
SendInput, % toggle ? "!3" : "!h"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

10 Mar 2018, 14:32

hi, that is awesome, thanks so much for this. Yes I mean that it waits for a key combination to be sent by the user... I'll have a test, thanks again!

Sunny
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

10 Mar 2018, 14:40

can't believe how much shorter your equivalent version is. That's nuts. Not at pc ATM, but will test this tonight. Btw, yes the key combos I want to send to the app include alt, the shortcuts are default ones from the app itself... :)
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

10 Mar 2018, 16:42

Well that worked brilliantly. I've mapped it to a mouse button and it's made my workflow much better thanks for your help :)

Btw. I did a search in the references for the SendInput function to see what the '%' and '?' did, but can't find anything. Looks like an if statement, but could you please clarify? Will be making use of that trick extensively!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script to run a different shortcut each time is run

10 Mar 2018, 17:01

for % see force an expression:
Variables and Expressions
https://autohotkey.com/docs/Variables.htm#Expressions

for a?b:c see ternary operator:
Variables and Expressions
https://autohotkey.com/docs/Variables.htm#ternary

Yes, the ternary operator is like a shortened if-else statement.

Code: Select all

;before:
if (var1 = var2)
	func1()
else
	func2()
;after:
(var1 = var2) ? func1() : func2()

;before:
if (var1 = var2)
	var3 := "text1"
else
	var3 := "text2"
;after:
(var1 = var2) ? (var3 := "text1") : (var3 := "text2")
;or alternatively:
var3 := (var1 = var2) ? "text1" : "text2"
Since individual characters are difficult to look up in the documentation, I made a list here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

11 Mar 2018, 04:36

You are a Legend. Thanks Jeeswg :)
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

23 Mar 2018, 12:44

Hey Folks, Jeeswg,

The script seems to be working well most of the time but sometimes I'm finding that the Alt button gets stuck on when I run the shortcut which runs the script. I can work around it by hitting the Alt key manually on the keyboard and it turns the Alt key off and the script works normally again

Any idea?

Thanks,
Sunny
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script to run a different shortcut each time is run

23 Mar 2018, 12:57

It would probably help if you shared your script (or a simplified version of it, if it's long). Generally I avoid sending the alt key. You could try Send, {LAlt up} at the end (or SendInput instead of Send), to release the key.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

23 Mar 2018, 13:05

Hi,

So this is the script... I kept the header code from the default template but not sure if it's causing the issue

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;simpler equivalent SC Pointer script 

!=::
toggle := !toggle
SendInput, % toggle ? "!3" : "!h"
return
So change to something like this? (Or i will just swap the Alt out if it's a problematic key)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;simpler equivalent SC Pointer script 

!=::
toggle := !toggle
SendInput, % toggle ? "!3" : "!h"
Send, {LAlt up}
return

Cheers
S
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script to run a different shortcut each time is run

23 Mar 2018, 13:37

- Yes that looks about right, either Send or SendInput. If that doesn't work, hopefully some other users will have some ideas.
- I tend to avoid sending alt, often it's possible to use WM_COMMAND and PostMessage/SendMessage to invoke menu items, but perhaps you have to send alt in this case. I use alt in hotkeys occasionally.
- A shorter way would be:

Code: Select all

SendInput, % (toggle ? "!3" : "!h") "{LAlt up}"
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
sunnyod
Posts: 13
Joined: 10 Mar 2018, 05:22

Re: Script to run a different shortcut each time is run

23 Mar 2018, 14:36

Thanks again for your help, bud. Really appreciated. Have a great weekend...

Cheers
Sunny

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 329 guests