Writing script for Autohotkey for invoking 'Shift+F5' keystrokes - autohotkey

I have to run few applications like browser and ticketing tool. But, I need to continuously press shift+F5 keystrokes for reloading the pages of my browser and ticketing tool.
Can I use any script through autohotkey for invoking 'Shift+F5' keys repeatedly for every 20 seconds regular interval?
Can anybody please help me with writing the script for autohotkey for invoking 'Shift+F5' keystrokes?

I assume that you want to be able to turn this feature on/off
#SingleInstance Force
#installKeybdHook
#Persistent
#F5:: ; [Win]+[F5] to start timer
SetTimer, RefreshPage, 20000
TrayTip, Refresh, Started, 1
ToolTip, Refresh Active
Return
+#F5:: ; [Shift]+[Win]+[F5] to stop timer
SetTimer, RefreshPage, Off
TrayTip, Refresh, Stopped, 1
ToolTip
Return
RefreshPage:
Send, +{F5}
Return

Related

Lost focus in browser when using clipboard for search query [autohotkey]

I am using a simple script to run a google/wikipedia/etc search using a hotkey, unfortunately after the search result appears in a new tab, I have to click because the tab is not on focus, although the browser windows is on focus. I tried to add a WinActivate but it's not working. This script used to work as expected before a new OS installation. Why is this script making lose focus on the browser?
Here's the script
^+g::
{
Send, ^c
sleep 200
Run, https://www.google.com/search?hl=en&q=%Clipboard%
sleep 50
WinActivate, ahk_exe waterfox.exe
}
Return
I don't know why, but it looks like increasing the delay between the Run and the WinActivate seems to fix it.
^+g::
{
Send, ^c
sleep 200
Run, https://www.google.com/search?hl=en&q=%Clipboard%
sleep 500 ;Up from 50, you might be able to fine-tune this number based on your computer's speed
WinActivate, ahk_exe waterfox.exe
}

How do I add a delay between SendInput commands in AutoHotkey?

I have an AutoHotkey script using SendInput which sends MouseClick commands too quickly for my program to handle. My script will send a MouseClick to focus an input field, then start typing before the field finishes focusing.
I've tried using SetKeyDelay to make my script run a bit slower, but this doesn't work with SendInput.
Note: SetKeyDelay is not obeyed by SendInput; there is no delay between keystrokes in that mode. This same is true for Send when SendMode Input is in effect.
Documentation for SetKeyDelay
My current workaround is to use sleep commands after each input, but this is less than ideal.
SendMode Input
F1::
MouseClick, left, 61, 50 ; select title field
sleep 100 ; artificial delay to prevent misfocused inputs
SendInput %user_input%{Enter} ; enter job title
sleep 100 ; artificial delay
MouseClick, left, 67, 408 ; select job
sleep 100 ; artificial delay
Return
Ideally I would like a more elegant solution for adding a delay between each SendInput command without manually using a sleep command each time.
How can I add a delay between SendInput commands in AutoHotkey without repeatedly using sleep?
Try using SendPlay instead of SendInput.
This sends text and mouse clicks with a 100ms delay following each click
user_input := "hello world"
SetMouseDelay 100, Play
SendPlay {Click 61,50}%user_input%{enter}{click 67,408}
From the documentation for SendPlay.
SendPlay
Note: SendPlay may have no effect at all if UAC is enabled, even if the script is running as an administrator. For more information, refer to the FAQ.
Like SendInput, SendPlay's keystrokes do not get interspersed with keystrokes typed by the user. Thus, if the user happens to type something during a SendPlay, those keystrokes are postponed until afterward.
Although SendPlay is considerably slower than SendInput, it is usually faster than the traditional SendEvent mode (even when KeyDelay is -1).
SendPlay does not use the standard settings of SetKeyDelay and SetMouseDelay. Instead, it defaults to no delay at all, which can be changed as shown in the following examples:
SetKeyDelay, 0, 10, Play ; Note that both 0 and -1 are the same in SendPlay mode.
SetMouseDelay, 10, Play

How do I update the auto hot keys config once saved from a script?

I have a shortcut to open the current running script in a text editor and then save it and close it. Upon closing, how do I update ahk to use the new updated settings?
This is what I have:
IfWinExist, test.ahk ahk_exe notepad.exe
{
WinActivate
Sleep, 100
Send, ^s
Sleep, 200
WinClose
}
else
MsgBox, window not found
app1Open=false
Reload
If you want to restart the script itself (that is, from its own process), use the Reload command.
Otherwise, if you want to restart the script from another process, you have to close it and then run the script again. In this case, you can either kill the script's process and run it again (not nice!). Or you can tell the script to gracefully Reload, for example in one of these two ways:
Inter-process communication: OnMessage() for the receiving process and PostMessage/SendMessage in the sending process
Defining a simple hotkey in your receiving script and sending that hotkey from another process
As MCL mentioned, use Reload. There is no need to close the script. Here is an example:
^!#r:: ; Save and Reload the script with Alt+Ctrl+Win+r
IfWinExist, SciTE4AutoHotKey
{
WinActivate
Sleep, 100
Send, ^s
Sleep, 300
}
Reload
Return

Auto Maximize skype live conversation window with Autohotkey

Here is what I'm trying to do and would greatly appreciate any help here.
I am trying to automate maximizing the live conversation window in skype with an Autohotkey script. I'm trying to make it so I can call into a remote machine using skype and have it auto answer (this is native in skype)...once I have a live conversation window I would like to maximize the live conversation window to fill the screen.
I've given this a shot but somehow don't think that I have the correct ahk_class for the live conversation window but there may be something else I am missing. I've placed a the code I've tried using below...any help would be great.
#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.
#Persistent
IfWinExist, TLiveConversationWindow ;
{
WinActivate
WinMaximize
send !{Enter};When using skype normally this Alt+Enter will maximize the window
return
}
I also tried to use this code to determine the proper class for the live conversation window...but has not helped as of yet.
Alt & Enter::
IfWinExist, TLiveConversationWindow
WinActivate
ControlFocus, ClassNN TLiveConversation1
ControlClick, ClassNN TLiveConversation1, , , , 2,
MouseClick, left, , , 2
send !{Enter}
; now we get the ID & CLASS
WinGet, Active_Window_ID, ID, A
WinGetClass, Active_Window_Class, A
MsgBox, The active window's class is "%Active_Window_class%" and ID is %Active_Window_ID%
Was able to get a simplified version of the code to work with a hotkey to initiate but have not been able to get the WinWait function to work as per #Schneyer.
Functioning Code activated by hotkey
#NoEnv
#Warn
#Persistent
SendMode Input
SetWorkingDir %A_ScriptDir%
; Skype Maximizer initiating functions
^!p::
;WinWait ahk_class TLiveConversation1
;WinWait ahk_class TConversationForm
;WinWait ahk_class TLiveConversationWindow
;WinMaximize ahk_class TLiveConversation1
;functioning code
;Activate tSkMainForm.
WinActivate ahk_class tSkMainForm
;Send Alt Enter Input to maximize.
SendInput !{Enter}
;TLiveConversationWindow Always On Top
WinSet, AlwaysOnTop,,ahk_class TLiveConversationWindow
;Minimize main form
WinMinimize ahk_class tSkMainForm
Return
When swapping the ^!p:: with any of the WinWait Functions nothing seems to happen. The WinWait seems like it should be the proper method, any thoughts on why it won't work?
Problems
#persistent lets the script run, but your code still gets executed only once when you start the script. After that it stays active but does nothing.
Use a WinWait to wait for the window to appear (wrap it in a Loop if you want it to run more than once).
Use ahk_class to to search for a window class instead of the window title
Working code
You can use the Window Spy tool which is included in AHK. Use the tray icon menu of a running AHK script to start it.
I use TConversationForm in the code, but it works with every window class.
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
WinWait ahk_class TConversationForm
WinActivate
WinMaximize
Scanning through AHK forums I was able to find a post that led me to the answer for this little problem. Check the below link for further information.
https://autohotkey.com/board/topic/96491-detect-when-a-classnn-window-exists/
The problem with using ahk_class to identify when a skype call was active is that the "active call window" identified as classNN TLiveConversation1 in the inspector was actually a Control within the window of ahk_class tSkMainForm rather than a Window. This made the WinWait function ineffective at identifying it when the call initiated.
In order to identify the Control it is necessary to loop through the controls (using WinGet) in ahk_class tSkMainForm until the TLiveConversation exists and then kick off any subroutines needed. For me that was to maximize the live conversation window.
You'll see the code to do this starting with the WinGet function. All of this is wrapped in a while loop so that it will run persistently allowing it to be called over and over. In essence this code will do the following:
Create a list (SkypeControlList) of controls existing in ahk_class tSkMainForm.
Continually Loop Through SkypeControlList
When a new Live Conversation is initiated a control TLiveConversation1 will exist
Once TLiveConversation1 esists run necessary code
#NoEnv
#Warn
#Persistent
SendMode Event
SetWorkingDir %A_ScriptDir%
DetectHiddenWindows, on
stop = 0
Loop
{
While stop = 0
{
WinGet, SkypeControlList, ControlList, ahk_class tSkMainForm
Loop, Parse,SkypeControlList, `n
{
;Loop to search for control TLiveConversation1
if (A_LoopField = "TLiveConversation1")
{
;Deactivate active screensaver
PostMessage, 0x0112, 0x0F060, 0,, A
;RegWrite REG_SZ, HKEY_CURRENT_USER, Control Panel\Desktop, ScreenSaveActive, 0
;SetKeyDelay, 500
Send {Esc}
;Activate tSkMainForm.
WinActivate ahk_class tSkMainForm
;Send Alt Enter Input to maximize.
Send !{Enter}
;TLiveConversationWindow Always On Top
WinSet, AlwaysOnTop,,ahk_class TLiveConversationWindow
stop = 1
sleep, 100
}
}
}
IfWinExist ahk_class TLiveConversationWindow
{
stop = 1
sleep, 1000
}
IfWinNotExist ahk_class TLiveConversationWindow
{
;Minimize all windows by win+D show desktop
send #d
sleep,1000
; is that call quality feedback window up? kill it.
SetTitleMatchMode, Regex
WinClose, ^Skype.*Call quality feedback$
stop = 0
}
sleep 1000
}
Return
The above code works great as long as there is no screen saver active on the machine being called. If there is an active screen saver the call will answer but the screen saver will not go away. You can see remnants of code trying to wake the computer from a screen saver (This does not currently work).
I've also added a few bits of code to clean up the experience such as removing the call quality popup window that skype throws up after a call as well as clearing the desktop after the call ends.
Thanks to #Blauhirn and #Schneyer for their input in trying to get this solved.

Hotkey to restart autohotkey script?

Say I have an autohotkey script C:\path\to\my\script running. Is there a way to define a hotkey that re-starts it?
In order to prevent duplicate instances, I normally do not re-launch a script but use the build-in function Reload. I launch this with Ctrl+Win+Alt+R and use Ctrl+Win+Alt+E to edit the main AHK script.
^#!r::Reload
Actually, my script looks like this:
^#!r::
Send, ^s ; To save a changed script
Sleep, 300 ; give it time to save the script
Reload
Return
^!#e::Edit
As a matter of fact, all the way at the top of my script I have this to give me a visual and audio indication that the script was restarted:
#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Icon , Shell32.dll, 25, 1
TrayTip, AutoHotKey, Started, 1
SoundBeep, 300, 150
Return
Make a hotkey that runs a script, which in this case is the same script and then exit.
somehotkey::
Run, C:\path\to\my\script.ahk
ExitApp
return
I found this to be the safest option of them all, because it takes care that the correct script is reloaded when you have multiple scripts running simultaneously, which was a recurring issue for me. The combination of the following also ensures that only one instance of a script will ever run at a time. The ScriptFullPath variable includes the name of the script.
#SingleInstance Force ;put this at the top of the script
^r::run, %A_ScriptFullPath%