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
Script to run a different shortcut each time is run
Re: Script to run a different shortcut each time is run
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
#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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to run a different shortcut each time is run
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
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
Re: Script to run a different shortcut each time is run
- A key combination pressed by the user?waits for a key combination to be received
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to run a different shortcut each time is run
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
Sunny
Re: Script to run a different shortcut each time is run
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...
Re: Script to run a different shortcut each time is run
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!
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!
Re: Script to run a different shortcut each time is run
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.
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
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"
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to run a different shortcut each time is run
You are a Legend. Thanks Jeeswg
Re: Script to run a different shortcut each time is run
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
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
Re: Script to run a different shortcut each time is run
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to run a different shortcut each time is run
Hi,
So this is the script... I kept the header code from the default template but not sure if it's causing the issue
So change to something like this? (Or i will just swap the Alt out if it's a problematic key)
Cheers
S
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
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
S
Re: Script to run a different shortcut each time is run
- 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:
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to run a different shortcut each time is run
Thanks again for your help, bud. Really appreciated. Have a great weekend...
Cheers
Sunny
Cheers
Sunny
Who is online
Users browsing this forum: mikeyww and 329 guests