Photoshop picks up input before AHK - autohotkey

Few years ago a have write following script:
#IfWinActive ahk_class Photoshop
#Wheelup::
Send, {vkDD}
return
#IfWinActive ahk_class Photoshop
#WheelDown::
Send, {vkDB}
return
This script generate input '[' or ']' when I wheel mouse up or down while win-key is pressed. This script worked good, but now, when I have installed photoshop 2020, it's not working. I thought that ahk_class has been changed , but it's not the case. When I removed ifWinActive line, the script sent characters to the notepad, but not sent them to the photoshop. In addition, my other bingings, are not working while Photoshop is active too.
What should I do to solve this problem?

If Photoshop is running with admin privileges, then AHK won't intercept the key presses, and that could very well be the reason behind this problem.
If that is the case, try to run the AHK script as administrator by adding this to the auto-execute section (top of the script):
; If the script is not elevated, relaunch as administrator and kill current instance:
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try ; leads to having the script re-launching itself as administrator
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
For more details read https://autohotkey.com/docs/commands/Run.htm#RunAs.

Related

Controlsend to Visual Studio Code?

I have tried a number of things to make this work and all have failed:
ControlSend,, ^f, ahk_class Chrome_WidgetWin_1
ControlSend,, ^f, ahk_exe Code.exe
Both of the above failed to open the find bar in VSCode.
I had a similar issue once, when I was trying to use ControlSend with Firefox and adding ahk_parent worked then:
ControlSend, ahk_parent, ^f, ahk_exe Code.exe
ControlSend, ahk_parent, ^f, ahk_class Chrome_WidgetWin_1
Sadly, both of the above also failed to trigger anything.
I searched around but was not able to find anything for ControlSend with VSCode.
Has anyone had any success using ControlSend with VSCode?
Any help would be really wellcome
I tried ControlSending various commands to VSC. This seems to be a problem only to certain commands (another one is ^d), while other commands such as ^n work with no hitches.
Some things I noticed:
VSC must be activated (i.e. in focus) so ControlSend can work at all.
When debugging your script from VSC, sending only one ^f doesn't seem to work. However, funnily, when you send ^f twice, VSC always registers it.
Here's a "crude but it works" solution:
; Improves reliability - the script holds down Shift for 50 ms before sending f
SetKeyDelay,, 50
WinActivate, ahk_exe Code.exe
ControlSend,, ^f, ahk_exe Code.exe
ControlSend,, ^f, ahk_exe Code.exe
If you plan to launch your script as an .exe or outside VSC, then the second ControlSend can be omitted. But sending it twice doesn't cause any harm. The main drawback is that VSC will be activated (brought to front and then focused).

How to add administrator privileges to AutoHotkey script?

I compiled it to an executable, but to open it I have to right-click and press "Run as administrator". I want it to request admin privileges each time I run it, but how to do it?
I can't do this:
Because then it doesn't work when I copy it to a second computer.
Try adding this to the auto-execute section (top of the script):
; If the script is not elevated, relaunch as administrator and kill current instance:
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try ; leads to having the script re-launching itself as administrator
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
and recompile the script.
For more details read https://autohotkey.com/docs/commands/Run.htm#RunAs.
Here's a much simpler code for this purpose:
#SingleInstance Force
if not A_IsAdmin
Run *RunAs "%A_ScriptFullPath%"
It will run the script as Admin if it's not already running as Admin.
If you don't have #SingleInstance Force on top of your script, it will ask that if you want to replace the running script (not admin) with admin. So to prevent that, add the mentioned line on top of your script.
If you might compile your script in the future, it's better to use this one instead to make it future-proof:
#SingleInstance Force
if !A_IsAdmin
Run, % "*RunAs " (A_IsCompiled ? "" : A_AhkPath " ") Chr(34) A_ScriptFullPath Chr(34)
Chr(34) returns character "
Source: https://www.autohotkey.com/boards/viewtopic.php?t=39647

How to write a script that creates hotkeys?

I have a lot of little hot keys defined, such as:
; Open CMD
#c::
Run, cmd.exe
WinWait, ahk_exe cmd.exe
WinActivate
Return
I'd like to build a function that takes the exe and hot key, and it will bind the app with that hot key. Here's what I have so far:
bind_exe_to_hotkey(exe,hotkey)
{
run_label:
Run, %exe%
WinWait, ahk %exe%
WinActivate
Return
HotKey, %hotkey%, run_label
}
bind_exe_to_hotkey("cmd.exe","#c")
However, this just opens a command window. What am I doing wrong? Is there an easier/better way to accomplish this?
Binds key to a function that handles launching an executable:
#c: launch("cmd.exe")
#n: launch("notepad.exe")
launch(exe)
{
Run, %exe%
WinWait, ahk %exe%
WinActivate
}

What's the difference between Run command and commands in cmd?

I have the following script:
^!c::
Run stop
Return
Stop is configured to run a program via environment variables.
So if I open up cmd and type “stop” and hit enter the program opens as intended, even if I push winkey + R it does the same thing. However if I use the script with ctrl+alt+c. I do not get the same result.
Why is the script doing something different?
How can I change my script to behave the same way as if it was typed into cmd or winkey + R?
Simple:
run, %comspec% /c stop
Or if this doesn't work you could just start a cmd window and send it directly
run, %comspec% /k
WinWait, %comspec%
WinActivate
Send stop{Enter}
/c tells the console window to close after execution, /k lets it stay open
or you could use an COM object and even get the output.
objShell := ComObjCreate("WScript.Shell")
objExec := objShell.Exec(ComSpec " /c stop")
strStdOut := ""
while, !objExec.StdOut.AtEndOfStream
{
strStdOut := objExec.StdOut.ReadAll()
}
Update:
Without the run command at all:
SetTitleMatchMode, 2
send #r
WinWait, TITLE_OF_THE_RUN_WINDOW
WinActivate
send cmd{Enter}
WinWait, cmd.exe
WinActivate
WinGetTitle, title
Send stop{Enter}
WinWait, %title%,,, stop
WinClose,
TITLE_OF_THE_RUN_WINDOW replace this with the title of the window, which opens on Win+r. A windows cmd window has the command in its title while it gets executed. So we save the title of the command window, and wait for it to drop the command ("stop") and close it then.
UPDATE: Cmd window close added to solution 4

Substitute AltTab in one key

Is it possible to substitute AltTab with only one key press ?
i tried this one but it doesn't work
`::AltTab
I use this, you may need to change the Sleep delay.
`::
Send {Alt Down}{Tab}
Sleep 100
Send {Alt Up}
return
I am running Windows 8.1 64-bit and AutoHotkey v1.1.16.05. And my C:\Program Files\AutoHotkey\AutoHotkeyU64.exe is digitally signed by running the script described here (EnableUIAccess.zip) so that Windows allows it to simulate Alt+Tab. The digital sign is required if you are using Windows Vista and onwards.
Download the zip file and extract it. Then run EnableUIAccess.ahk:
It will ask which AutoHotkey executable to sign. Pick one that you need (AutoHotkeyA32.exe, AutoHotkeyU32.exe, AutoHotkeyU64.exe or AutoHotkey.exe).
Then it will ask to save the new executable. You can choose to overwrite the original file or save as another executable.
Finally it will ask to create a "Run Script with UI Access" context menu item. If you choose 'Yes', then you can right-click a .ahk file and choose "Run Script with UI Access", which will use the digitally signed executable to run the .ahk file. But if you choose to overwrite the original file in step 2, then it is not necessary to create this context menu item.
From the docs:
Each Alt-Tab hotkey must be a combination of two keys, which is typically achieved via the ampersand symbol (&).
http://ahkscript.org/docs/Hotkeys.htm#AltTabDetail
You might even be able to avoid messing around with UIAccess and administrator privileges. This works for me on Windows 8.1:
`::Run "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"
And for others who are struggling with getting a two-key combination working on Windows 8 or 10, here's an example using Ctrl-Tab to trigger the window switcher:
^Tab::
Run "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"
SetTimer EnterOnKeyUp, -1
return
EnterOnKeyUp:
WinWaitActive ahk_class TaskSwitcherWnd
KeyWait Ctrl
Send {Enter}
SetTimer EnterOnKeyUp, Off
return
* Inspired by: Fully Working Alt Tab Win 8