How to use hotkey,off in #IfWinActive section - autohotkey

I am using the following code:
#IfWinActive ahk_class Notepad
4::Send,4
5::
Hotkey,4,off
;~ do something
Hotkey,4,on
Return
#IfWinActive
but it generates this error:
Prompt Error: Nonexistent hotkey variant (IfWin).

I don't understand what you mean by using 4 to send 4 - it seems redundant.
I tried it like this.
pushing 4 will send 6, and pushing 5 will send 4.
#IfWinActive ahk_class Notepad
{
4::Send,6
5::
Hotkey,4,off
send, 4
Hotkey,4,on
Return
}
The idea here is that the brackets encase the entire function within the IfWinActive. If notepad isn't active, nothing at all happens.

Related

How to send a key repeatedly in Autohotkey

I want to write an AutoHotkey script which loop a key X number of times.
For example, here's is a script which overwrites the function of ENTER key with function of F2 key in File Explorer.
#IfWinActive ahk_class CabinetWClass
Enter::
Send, {F2}
#IfWinActive ahk_class CabinetWClass
Enter::
Send, {ENTER}
#IfWinActive
The goal is to press ENTER to rename a select file, and then press ENTER to confirm the rename. Pressing ENTER on the same file that have just been renamed should send F2 key again (In case there is typo error).
Currently the second block doesn't work as I'm sending the same key, how to fix this?
The KeyWait command is your friend in this case.
There is still room to improve on how you handle the second Enter
#IfWinActive ahk_class CabinetWClass
$Enter::
sleep,100 ; giving time to detect the first Enter
Send, {F2}
Keywait, Enter, T5 D ; wait 5 seconds for the Enter to be pressed down
If (ErrorLevel == 0)
{
Send, {Enter}
sleep 200
Send, {F2}
}
else
{
traytip, , timeout ; Enter was not pressed down in 5 seconds
}
return
Basically, it appears you're trying to assign different tasks to the same hotkey and due to this being done seperately ahk is selecting one of the tasks and running with that task and only that task. If loops can be used within hotkeys, so I would suggest using this to rotate between the two expected outcomes. Please see example below:
temp:= 1
enter::
if(temp==1)
{
Send, {ENTER}
temp:=2
}
else if(temp==2)
{
Send, {F2}
temp:=1
}
return
1::
Temp:=1
return
2::
temp:=2
return
^x::ExitApp
I also added in hotkeys for 1/2 to allow you to manually decide the outcome rather than it being specifically assigned in the case of any issues.
Oh, and ctrl+x to close the macro.
You're trying to rebind the enter key twice.
Rebinding a key is like saying "When I press this key, do this:" - in this case it's under an #IfWinActive so it's more like "When this window is open and I press this key..."
When you break that down you have "When I press enter - press F2" as well as "When I press enter, press enter"
What you're wanting to achieve is make the rebind conditional - i.e. it only sends F2 under certain conditions.
It's hard to know how to help without more context. Is there any reason you can't use a different key combination? Like Ctrl + Shift + Enter?
Something like:
+^Enter::send, {F2}

How can I set AutoHotKey's code to run only in chrome?

I want to simulate a "find" operation when I double click a word in Chrome.
I've managed to do this :
~LButton::
SetTitleMatchMode, 2
#IfWinActive, ahk_class Chrome
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
{
SendInput ^c
Sleep, 10
SendInput ^f
Sleep, 10
SendInput ^v
}
Return
But it runs even for non-chrome process (when double click a word)
Question:
How can I make this script run only when double click in chrome ?
#IfWinActive, ahk_exe Chrome.exe ;; start of IfWinActive condition, for me it didn't work with ahk_class so i changed it to ahk_exe
~LButton::
SetTitleMatchMode, 2
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
{
SendInput ^c
Sleep, 10
SendInput ^f
Sleep, 10
SendInput ^v
}
Return
~RButton::
SendInput {Escape}
Return
#IfWinActive ;; end of condition IfWinActive
You can get the title of the window or active window with WinGet - and then only apply the code IF that given window is active.
SetTitleMatchMode, 2
IfWinActive, - Google Chrome
This is a snippet I found:
DetectHiddenText, On ; the url of the active Chrome window is hidden text...
SetTitleMatchMode, Slow ; ...we also need match mode 'slow' to detect it
; activate chrome window,
; just for demonstation:
WinActivate, ahk_class Chrome_WidgetWin_1
IfWinActive, ahk_class Chrome_WidgetWin_1 ; we only want to check for the hidden text if Chrome is the active window,
{
WinGetText, wintext, ahk_class Chrome_WidgetWin_1 ; if it is we grab the text from the window...
If InStr(wintext,"autohotkey.com") ; ...and if it contains a url that we want,
{
;### code conditional on url goes here ###
msgbox % "The active Chrome window is on the autohotkey.com domain! The page title and URL is:`n`n>" wintext ; <<== we run the desired code here.
}
}
exitapp
but it needs to be changed to fit your specific needs.

Autohotkey - Copy selected text, paste in Notepad++

I'm new to Autohotkey, tried to make a macro to:
Copy selection
Paste it in Notepad++
Playback Notepad++ macro (for formatting) shortcut: Ctrl+Shift+B
Copy all the edited text
Paste in Firefox text field
I tried starting with the following code, but I couldn't even get AHK to copy paste my selection to Notepad++.
^!x::
Send, ^c
ClipWait
IfWinExist, Notepad++
{
WinActivate
Send ^v
}
Here is working script:
^!x::
Send, ^c
ClipWait
SetTitleMatchMode, 2
IfWinExist, Notepad
{
WinActivate
Send, ^v
}
return
I added command SetTitleMatchMode, 2 as Joe DF mentioned in comments. That command (link) with parameter 2 sets the matching behavior of IfWinExist command so that a window's title can contain WinTitle anywhere inside it to be a match. Also added return in the end.

script opens document but not with hotkey

I'm creating a script to open and run with a program that has no help files but comes with a huge PDF manual. There is no way to open it from the program, so I have set up a command to open it. The command opens the PDF when run on its own, but when I assign a hotkey, it does not work. What very basic information am I missing?
thanks,
Ellen
SetTitleMatchMode, 2
runwait C:\Program Files\FontLab\TypeTool3\TTool3.exe, , max
IfWinExist TypeTool 3
Return
ExitApp
Return
#ifWinActive, TypeTool 3
$wheeldown::wheelup
$wheelup::wheeldown
F1::
Run, C:\Documents and Settings\Ellen\My Documents\TypeTool3WinMan.pdf
Return
#ifWinActive
Do you have a Return before your #IfWinActive statement? If not then during startup the script will run every line until it hits the first Return, which seems to be all the way to the end for you. So also place a return after your Run, C:..... command
And oh.. Are you sure about that weird long ahk_class? It could be correct but it looks strange and if this is just a littlebit off, your hotkey would never work, so try it first with the #IfWinActive line commented out.
SetTitleMatchMode, 2
;All the stuff you want to run at startup....
Return
#ifWinActive, (part of) the window name here e.g. Excel
$wheeldown::wheelup
$wheelup::wheeldown
F1::
Run, C:\Documents and Settings\Ellen\My Documents\TypeTool3WinMan.pdf
Return
#ifWinActive

Combining two sequences of keystrokes using autohotkey

I would like to send a sequence of keystrokes using just one key to accomplish certain task in sublime text 2 using autohotkey. To set a mark in sublime text 2 the key sequence is Ctrl+K followed by Ctrl+Space. I tried
#IfWinActive, ahk_class PX_WINDOW_CLASS
Numpad0::Send ^k sleep 5, Send ^+Space
#IfWinActive
which will activate the first part of the sequence but has the side effect of also typing send sleep 5, Send and trying to save the file which I am not trying to do.
If I remove the (sleep 5, Send ^+Space) I then have to push Ctrl+Space to finish off the sequence in order to set the mark. What do I need to add after the Ctrl+K to accomplish the ability to set a mark in sublime text with autohotkey?
When Send is used, it will try to send the rest of the line. That would be why the rest is being typed. However, I tried splitting them up, and it still did not function correctly.
The following works for me, give it a try:
#IfWinActive, ahk_class PX_WINDOW_CLASS
Numpad0::Send, {CTRLDOWN}k{CTRLUP}{CTRLDOWN}{SPACE}{CTRLUP}
#IfWinActive
Inside #IfWinActive, you can use multiple lines this way.
#IfWinActive, ahk_class PX_WINDOW_CLASS
Numpad0::
Send, ^k ; Send Ctrl k
Sleep, 5 ; Wait 5 ms (probably too short, I would use 400 ms)
Send, ^+{Space} ; Send Ctrl Shift Space
Return
#IfWinActive
Added Curly brackets around Enter!