DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: IDEA: focus get (and keep, for a while)  (Read 17,066 times)

bft_chromeguy

  • Participant
  • Joined in 2008
  • *
  • Posts: 15
IDEA: focus get (and keep, for a while)
« on: October 27, 2008, 01:05 AM »
Hi everyppl,

I know this idea is floating around on the boards but I haven't been able to get it working properly.

Basically, I have 2 text windows open - one I want to type into all the time, and one I want to type in for short periods of time.
EG: Dreamweaver and an IM chat.

I want to be able to have the main window (dreamweaver) under focus by default, but I want to be able to click in the chatbox (or any other) and for as long as I am typing there it keeps focus. After an extended period of non-activity (eg, no more typing, say, 5~8 seconds) I want to switch back to the main window (returning to work)

The following code almost does what I want, except for the "keep focus while I am typing" part.
Code
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#SingleInstance, force
#Persistent
#WinActivateForce
SetTitleMatchMode, 2
Inputbox, name, Program to focus, Type the name of the program to keep in focus:, , 250, 150
if name = ; If nothing is entered for the app name exit script
{
	exitapp
}
Inputbox, time, How long to wait, How long should I wait before reactivating? (milliseconds):, , 250, 150
If time =
{
	time := 5000
}
SetTimer, WatchWindow, %time%
return

WatchWindow:
IfWinActive, %name%
{
	return
}
Else
{
	Sleep 5000
	;some if-key-pressed-reset-sleep-timer code
	WinActivate, %name%
}
Return
Thanks in advance
http://www.bugfreetech.com - come visit my site

TucknDar

  • Advanced coder of Nowt
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 1,133
Re: IDEA: focus get (and keep, for a while)
« Reply #1 on: October 27, 2008, 06:52 AM »
Not much help from me, but just wanted to say that this sounds like an excellent idea :-[

justice

  • Solve issues simply.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,898
    • Participated in NANY Challenge Four Times
    • Cody Award
    • Coding Snacks
    • DJ Badge - DJ
Re: IDEA: focus get (and keep, for a while)
« Reply #2 on: October 28, 2008, 11:42 AM »
I didn't really delve to deep into your script because it seemed very specific. To keep focus on a certain window:
Code: (autohotkey)
SetTitleMatchMode, 2
Win = AutoHotkey Help
Loop,
{
	IfWinNotActive, %win%
		WinActivate, %win%
	Sleep 200
}
Maybe that will help. Change Win and Sleep where appropriate.

justice

  • Solve issues simply.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,898
    • Participated in NANY Challenge Four Times
    • Cody Award
    • Coding Snacks
    • DJ Badge - DJ
Re: IDEA: focus get (and keep, for a while)
« Reply #3 on: October 28, 2008, 11:44 AM »
Even better:

Code: (authotkey)
SetTitleMatchMode, 2
Win = AutoHotkey Help

WorkItBaby:
WinWaitNotActive, %Win%
		WinActivate, %win%
return
Gosub, WorkItBaby

bft_chromeguy

  • Participant
  • Joined in 2008
  • *
  • Posts: 15
Re: IDEA: focus get (and keep, for a while)
« Reply #4 on: October 29, 2008, 03:00 PM »
hi justice,
your function WorkItBaby is the same as the one in there already WatchWindow. It is already returning focus after the timeout (5 sec)
What I really want is for the timeout to become reset while I type, effectively allowing the current window to retain focus until I stop typing for a period of time.
Ideally mouse movements would reset the timeout too.
http://www.bugfreetech.com - come visit my site

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 933
    • Participated in NANY Challenge Twice
    • Coding Snacks
Re: IDEA: focus get (and keep, for a while)
« Reply #5 on: November 08, 2008, 06:09 PM »
:) Try ReFocus!

- Refocuses a window after 5 seconds of inactivity.
- Press F1 to change the window to refocus.

Skrommel

Code
;ReFocus.ahk
; Refocuses a window after 5 seconds of inactivity.
; Press F1 to change the window to refocus.
;Skrommel @ 2008 

idletime=5000
focus:=WinExist("Notepad")

#SingleInstance,Force
#NoEnv
SetWinDelay,0

applicationname=ReFocus

SetTimer,TIMER,1000

TrayTip,%applicationname%,- Refocuses a window after 5 seconds of inactivity.`n- Press F1 to change the window to refocus.
Return

F1::
focus:=WinExist("A")
WinGetActiveTitle,title
TrayTip,%applicationname%,%title%
Return


TIMER:
IfWinNotActive,ahk_id %focus%
If (A_TimeIdlePhysical>idletime)
  WinActivate,ahk_id %focus%
Return
« Last Edit: November 08, 2008, 06:11 PM by skrommel »

urlwolf

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,837
    • Gathering Badge
    • Linux User
Re: IDEA: focus get (and keep, for a while)
« Reply #6 on: November 08, 2008, 06:20 PM »
And skrommel is back into action!

bft_chromeguy

  • Participant
  • Joined in 2008
  • *
  • Posts: 15
Re: IDEA: focus get (and keep, for a while)
« Reply #7 on: November 10, 2008, 08:24 PM »
oh wow!! I never knew it would be as easy as "A_TimeIdlePhysical"
I seriously gotta delve into the docs a bit more for AHK...
skrommel, you are my hero  :Thmbsup:
http://www.bugfreetech.com - come visit my site

Original address: https://www.donationcoder.com/forum/index.php?topic=15495.0