Lost focus in browser when using clipboard for search query [autohotkey] - 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
}

Related

What in this script is causing my window to minimize?

The goal of this simple script is to detect an idle interval when a specific program is in focus and then send a simple keystroke when that idle interval has passed. I'm running this script on 4 PC's and I'm getting unexpected results. Some PC's minimize the window when the script runs. Other PC's run it as expected. The script is identical on each PC.
I'm invoking this script by right clicking on the script (that is, not running a compiled exe version of it). Running it as administrator seems to achieve better results on some clients, on one it makes no difference and minimizes the window.
As stated, on some PC's the script works as intended. There are no error messages, it just causes my window to minimize. Nothing in that code, to my newb eyes, should cause the window to minimize.
#Persistent
SetTimer, Timer_check,3000
Timer_check:
if WinActive("ahk_exe gta5.exe")
{
if (A_TimeIdlePhysical > 31301 && WinActive("ahk_exe gta5.exe")) {
Gosub, keepActive
ToolTip, We're currently idle TimeIdle is %A_TimeIdle% and TimeIdlePhysical is %A_TimeIdlePhysical%
sleep 1000
ToolTip
}
if (A_TimeIdle < 31301) {
ToolTip
}
}
return
keepActive: ; keep active sub.
if WinActive("ahk_exe gta5.exe")
{
Send, {` down} ; Press the ` key to keep us active. It holds the key for 0.2 seconds.
Sleep 200
Send, {` up}
}
return```
You're trying to send the accent/backtick, which is default escape character in AHK (`). To fix this, send a different character or escape the escape character, like so:
Send, {`` down}
Sleep 200
Send, {`` up}
Without it being escaped, it just sends the down- and up-keys. The WinKey+down combination minimizes a non-maximized window and that may somehow be related to why you're seeing the game window occasionally minimized.
https://www.autohotkey.com/docs/commands/_EscapeChar.htm
Edit: Added script for testing
#Persistent
SetTimer, Timer_check, 3000
Timer_check:
If WinActive("ahk_exe gta5.exe") {
If (A_TimeIdlePhysical > 31301 && WinActive("ahk_exe gta5.exe")) {
Send , z
ToolTip, We're currently idle TimeIdle is %A_TimeIdle% and TimeIdlePhysical is %A_TimeIdlePhysical%
sleep 1000
ToolTip
}
If (A_TimeIdle < 31301)
ToolTip
}
Return

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.

AutoHotKey - Run a program once send several times

I am pretty new to AutoHotkey, but I managed it to start my desired program and send an Enter-Key to it, but the probleme here is, the program should only start once and if started it should only receive that enter key, when I press the key stroke again and again it should only send that enter key.
And the program should should stay in the background and not focus after it receives the enter key.
My Code:
#n::
Run F:\V..c.exe
Send {enter}
return
Detect if the process exists and start the program minimized, then wait for its window to appear.
#n::
process, exist, PROGRAM.EXE
if (errorlevel = 0) {
run, d:\program.exe, , min
winwait, ahk_class PROGRAM_WINDOW_CLASS
}
controlSend, , {Enter}, ahk_class PROGRAM_WINDOW_CLASS
;or use the line below
;controlSend, ahk_parent, {Enter}, ahk_class PROGRAM_WINDOW_CLASS
return
Replace PROGRAM.EXE with the executable name of your program and PROGRAM_WINDOW_CLASS with the window class as seen in the Autohotkey Window Spy utility available in Start menu or in the folder of the Autohotkey (AU3_Spy.exe) or in the right click menu of the Autohotkey tray icon.
Instead of running the program minimized it's also possible to use SW_SHOWNOACTIVATE flag of shellExecute, so you can replace the run, d:\program,, min with this:
dllCall("shell32\ShellExecute", uint,0, uint,0, str,"d:\program.exe", uint,0, uint,0
,uint,SW_SHOWNOACTIVATE:=4)
you could try:
hasran := false
#n::
if (!hasran) {
Run F:\V..c.exe
hasran := true
}
Send {enter}
return
It wont check if that windows exists, but it will only run the program once. then you can navigate to that program and it will only hit the enter key. (if that program is not gui I dont think you can send key events to it)

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

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