This is my first AHK script and I'm having trouble getting it to do what I want. Using the key combination Ctrl+Alt+A in Notepad, I want to jump to the beginning of a line of text, select the first character, copy it to the clipboard, and check what letter it is. Right now, I'm just trying to check the contents of the clipboard using a MsgBox but there's a problem: the MsgBox displays it's contents before the copy command! Here's the script:
#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.
#IfWinActive, ahk_class Notepad
^!a::
Send, {Home}
Send, {LShift Down}{Right}{LShift Up}
Send, ^c
MsgBox, %clipboard%
return
Running the command multiple times, you will see that it copies the selected text after it displays the MsgBox. It displays the text from the previous command. Any ideas of what I might be doing wrong?
Update: It would seem that the little piece of script that I neglected to include in the question might be the culprit: SendMode Input ; Recommended for new scripts due to its superior speed and reliability. I've updated the script above also. When I remove that line, it works as expected. Can anyone tell me why?
Is it simply that you've not added the return, line to the end of the code?
^!a::
Send, {Home}
Send, {LShift Down}{Right}{LShift Up}
Send, ^c
MsgBox, %clipboard%
return,
alternatively you could add a delay, the command for this is sleep, 1000 = 1 second.
If it fails, try this:
^!a::
Send, {Home}
Send, {LShift Down}{Right}{LShift Up}
Send, ^c
sleep, 200
MsgBox, %clipboard%
return,
The removal of SendMode Input ; Recommended for new scripts due to its superior speed and reliability. rectified the problem.
Related
I am trying to open up a webpage and save it as a complete file. This should be a relatively easy task for autohotkey. For some reason my script is not sending key events to the window. Here's the script:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn All, StdOut ; 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.
Run, www.google.com
WinWaitActive firefox.exe
sleep, 4000
ControlSend, ahk_class MozillaWindowClass, ^s
What would be a better approach to this problem?
Here is my solution:
Run, www.google.com
WinWaitActive ahk_exe firefox.exe
sleep, 4000
Send ^s
There were two main things that were changed for this to work
Include ahk_exe before the name of the exe in the WinWaitActive conditional
Removed the ControlSend and replaced it with a regular Send, since we are already waiting for the Window to be activated
Hope this helps, feel free to ask for any more clarification or help if you need it
ControlSend, ahk_parent, ^s, ahk_class MozillaWindowClass
I have this code, and i want to whenever i hold ctrl+n it will click and drag. It doesn't loop with the Send commands in place. It only runs once, unless i let go of ctrl and n and press again. If i comment them out the hotkey loops perfectly fine when i hold it down. Heres my script:
#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.
^n::
CoordMode, Mouse, Screen
Send {LButton down} ;if i comment these two out it works fine
MouseMove, 0, 30, 20, R
Send {LButton up} ; ditto
return
The problem was ctrl key. When pressed down it stops new execution of hotkey. I tried it without ctrl prefix and used just n:: and it worked fine. It also works if you press ctrl+n then release it then press it again.
Anyways when you add Hotkey Modifier $ sign in front it works as you wanted.
Hotkey Modifier $ explanation:
This is usually only necessary if the script uses the Send command to send the keys that comprise the hotkey itself, which might otherwise cause it to trigger itself. The $ prefix forces the keyboard hook to be used to implement this hotkey, which as a side-effect prevents the Send command from triggering it. The $ prefix is equivalent to having specified #UseHook somewhere above the definition of this hotkey.
More info:
https://www.autohotkey.com/docs/Hotkeys.htm
$^n::
CoordMode, Mouse, Screen
Send {LButton down}
MouseMove, 0, 30, 20, R
Send {LButton up}
return
I'm at a loss here. As soon as I add the LButton hotkey, the Send, {LButton} doesn't seem to work, but they show up in recently executed lines.
Env. Windows 7x64, Disabled Touchpad, AHK v1.1.31.01.
I try to emulate the Wink application (from DebugMode) to capture screenshots for training purposes. For that reason, I want to capture a screenshot just before a mouse click. This looks easy, and I even vaguely remember doing similar mouse hotkeys in the past. However I can't get this to work.
Step 1: I just reduced it to this simple script:
#InstallKeybdHook
#InstallMouseHook
#UseHook
#Persistent
Return
a::
Send, {LButton}
Return
q::
ExitApp
When using this script, I can simulate clicking the Left Mouse Button through the a key. Nothing special.
However as soon as I add either a line with "Hotkey, $LButton, MySendClick", or "$LButton::" the previously working a hotkey no longer works. In the recently executed lines, you can see the "Send, {LButton}" lines, but nothing is being send. Unexpectedly, the a hotkey actually causes the "$LButton::" hotkey to trigger (without it sending {LButton}). When I change the a hotkey to send "RButton" and the $LButton:: to $RButton::, then Send {Click} works perfectly (eventhough the a hotkey should never be able to trigger $RButton::).
Originally I just wanted to have the following HotKey:
$LButton::
SoundBeep, 300, 150 ; For testing only
; Send, ^{PrintScreen} ; To trigger Greenshot in the background
Sleep, 100
Send, {LButton}
Return
I upgraded from AHK v1.1.22.04 to v1.1.31.01. No improvement.
I tried "Click", "sendInput, {LButton}", "Send {Click}", "MouseClick, Left".
I tried "$LButton::", "vk01sc000::", "Hotkey, $LButton, MyClick".
Is this an issue with my specific Windows 7 configuration or an "undocumented AHK feature"?
#InstallKeybdHook
#InstallMouseHook
#UseHook
#Persistent
Return
a::
Send, {LButton}
Return
$LButton::
SoundBeep, 300, 150 ; Should be Send, ^{PrintScreen} ; To trigger Greenshot in the background
MouseClick, Left
Return
q::
ExitApp
In this last test example, When $LButton:: is disabled, the a hotkey works like a charm, but as soon as I enable $LButton::, the a hotkey triggers $LButton:: and no mouse click is being sent to the windows applications.
I would appreciate it when other Windows 7 users could quickly test this issue.
In my experience, using keys that you still want the input to pass through need the Tilde prefix.
https://www.autohotkey.com/docs/Hotkeys.htm#Tilde
~LButton::
SoundBeep, 300, 150 ; Should be Send, ^{PrintScreen} ; To trigger Greenshot in the background
KeyWait, LButton ; Wait for lbutton to be released.
Return
Recently I've been trying to rebind the 3-finger gestures of my Logitech Wireless Trackpad to switch between desktops in Windows 10. The code I used was the following:
#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.
XButton1::SendInput {LCtrl down}{LWin down}{Right}{LCtrl up}{LWin up}
XButton2::SendInput {LCtrl down}{LWin down}{Left}{LCtrl up}{LWin up}
#F::Send {LWin down}{Tab}{LWin up}
However, when I press the buttons on my mouse I use for Browser_forward and Browser_Back, the mouse switches between desktops as well. Can you force AutoHotKey to only use the trackpad for it's gestures? If so, how?
You have two options here!
You can exclude certain windows from activating your hotkey
Make your hotkey only active on certain windows excluding all others
You can do this by using #IfWinActive #If WinActive IfWinActive WinGet, lots of options on Functions and Commands for this...
Since you mentioned desktop, I got curious how one would go about detecting if the desktop was Active, below is the result:
#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.
$XButton1::
If (IsDeskTopActive())
SendInput {LCtrl down}{LWin down}{Right}{LCtrl up}{LWin up}
Else
SendInput {XButton1}
Return
$XButton2::
If (IsDeskTopActive())
SendInput {LCtrl down}{LWin down}{Left}{LCtrl up}{LWin up}
Else
SendInput {XButton2}
return
#F::Send {LWin down}{Tab}{LWin up}
IsDesktopActive() { ; Modified.. orignal by HotKeyIt
MouseGetPos,,,win
WinGetClass, class, ahk_id %win%
If class in Progman,WorkerW
Return True
Return False
}
I know I'm missing something obvious but I can't figure out why this doesn't work. Why doesn't hello show up in the first msgbox I know it says the variable isn't assigned if I uncomment #Warn? This is the only thing in the ahk file.
#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.
#SingleInstance force
; Reload the script
^!z::
WinActivate, ahk_class Notepad++
Send {ctrl down}s{ctrl up}
sleep 100
Reload
return
ADPass = hello
!5::
MsgBox, %ADPass%
Msgbox, test
return
Your assignment of ADPass never gets executed because it is between 2 hotkeys. You must place it before you begin your hotkeys (before ^!z) or place it within your hotkey (!5) to ensure it is executed.
I think the other answer would probably work, you need the variable to be set before the return. The return means the machine is never getting to that line of code, Try this.
#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.
#SingleInstance force
ADPass = hello; this doesn't have to stay here, just make sure it's before the return.
; Reload the script
^!z::
WinActivate, ahk_class Notepad++
Send {ctrl down}s{ctrl up}
sleep 100
Reload
return
!5::
MsgBox, %ADPass%
Msgbox, test
return
or
#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.
#SingleInstance force
; Reload the script
^!z::
WinActivate, ahk_class Notepad++
Send {ctrl down}s{ctrl up}
sleep 100
Reload
return
!5::
goto set
point:
MsgBox, %ADPass%
Msgbox, test
return
set:
ADPass = hello
goto point
Return