How to make windows not lose focus

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Glendek
Posts: 7
Joined: 22 Jan 2022, 21:21

How to make windows not lose focus

Post by Glendek » 22 Jan 2022, 21:31

Greetings AutoHotkey community!

I am very new to all of this coding stuff, however I have one simple question regarding window focus loss once the script runs.

I'm currently using FL Studio (a program to make music) and I've found this very basic script which allows me to run a command to immediately create an automation clip for any of the boxes / wheels I click.

Code: Select all

#IfWinActive ahk_class TFruityLoopsMainForm 
!a::
SendInput {F12}
SendInput {F6}
SendInput {F3}
SendInput {Left}
SendInput {Left}
SendInput {L}
SendInput {A}
SendInput {F9}
SendInput {F5}
Return
It works really great, however I have two issues with it and I'm not sure if it's ''fixable'':

1) the screen flashes
2) it removes the focus of a current window, which I dislike

is there any way around this?

Example of the script and how it behaves I uploaded in the video below (you can watch it online, no need to download).

https://gofile.io/d/OXz13B


Thank you for any help I can get!

Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to make windows not lose focus

Post by Rohwedder » 23 Jan 2022, 04:03

Hallo,
only a try (untested!):

Code: Select all

#IfWinActive ahk_class TFruityLoopsMainForm 
!a::ControlSend,, {F12}{F6}{F3}{Left}{Left}{l}{a}{F9}{F5}
	, ahk_class TFruityLoopsMainForm
Esc::ExitApp

Glendek
Posts: 7
Joined: 22 Jan 2022, 21:21

Re: How to make windows not lose focus

Post by Glendek » 23 Jan 2022, 09:53

Thank you for the reply!

I've tested the code out, it seems to behave the same way.

Now that I think of it, the flashing of the screen might be necessary due to the fact that it changes over 5 different windows in order to get to the command that I require.

But as for the window focus loss inside the program when using plugins / addons, I'm not sure if that can be solved!

Glendek
Posts: 7
Joined: 22 Jan 2022, 21:21

Re: How to make windows not lose focus

Post by Glendek » 24 Jan 2022, 10:20

Bump!

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: How to make windows not lose focus

Post by mikeyww » 24 Jan 2022, 10:31

Another idea is below.

Code: Select all

#IfWinActive ahk_class TFruityLoopsMainForm
!a::
Send {F12}{F6}{F3}{Left 2}la{F9}{F5}
WinActivate
Return
#IfWinActive

Glendek
Posts: 7
Joined: 22 Jan 2022, 21:21

Re: How to make windows not lose focus

Post by Glendek » 24 Jan 2022, 11:57

mikeyww wrote:
24 Jan 2022, 10:31
Another idea is below.

Code: Select all

#IfWinActive ahk_class TFruityLoopsMainForm
!a::
Send {F12}{F6}{F3}{Left 2}la{F9}{F5}
WinActivate
Return
#IfWinActive

Thank you for the reply and the awesome help you're providing!

The script unfortunately works the same way as the script 1 and 2 :(

Weird thing is, when I do the thing that I need manually (without the script) the windows / tabs do not lose focus.

Demonstration below!

https://gofile.io/d/XysFkI

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: How to make windows not lose focus

Post by mikeyww » 24 Jan 2022, 12:02

I do not have an answer. You can break it down by testing just a few of the key sends at a time. In some cases, increasing the key delay may help. If the script works in Notepad, then you know that the issue is how your target window is responding to it.

Glendek
Posts: 7
Joined: 22 Jan 2022, 21:21

Re: How to make windows not lose focus

Post by Glendek » 24 Jan 2022, 17:11

Okay, and how would I increase the delay time between keys?

Sorry for the basic questions, I'm very new to this!

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: How to make windows not lose focus

Post by mikeyww » 24 Jan 2022, 17:31

No problem. If you click the link that I posted, it provides the details. You could alter the press duration if you like, or-- for between keys-- the delay (first parameter instead of the second). You could change both. For either, try something between 25 and 50. You can put the command at the top of your hotkey routine (hotkey-specific), or at the top of the script (global effect).

Glendek
Posts: 7
Joined: 22 Jan 2022, 21:21

Re: How to make windows not lose focus

Post by Glendek » 24 Jan 2022, 17:44

Thank you!

I've found another solution which works, can anyone please confirm if this is okay (it's working, but just want to make sure)

also sometimes when the authotkey closes, my regular functions on keyboard get messed up, as in:

I press a - it presses e
i click on left mouse - it right clicks

is there a command that shuts autohotkey for good after the script ends or anything of that sort to prevent that?

Code: Select all

#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.

; Run, Last Tweaked - Link Control_2021.ahk
Run, Last Tweaked - Auto Clip_2021.ahk
return

Code: Select all

#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.

SetTimer, close, 2000

#IfWinActive ahk_class TFruityLoopsMainForm

!a::

MouseGetPos x, y

Sleep 50

MouseClick, left, 240, 40

Sleep 50

Send {Up 3}

Sleep 50

Send {Right}

Sleep 100

Send a

Sleep 50

MouseMove %x%, %y%

Close:
  IfWinNotExist, ahk_class TFruityLoopsMainForm
    exitapp

return

#IfWinActive

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: How to make windows not lose focus

Post by mikeyww » 24 Jan 2022, 21:02

Hmm, that's strange-- not explained by the script itself. A script like this would not normally need to be closed, but if it works, then go with it!

Glendek
Posts: 7
Joined: 22 Jan 2022, 21:21

Re: How to make windows not lose focus

Post by Glendek » 25 Jan 2022, 17:04

mikeyww wrote:
24 Jan 2022, 21:02
Hmm, that's strange-- not explained by the script itself. A script like this would not normally need to be closed, but if it works, then go with it!
Indeed it works, I was just hoping to add a few more lines to it in order to force it to close if Fl Studio is not open, since as I've said it sometimes bugs out and messes with input keys of Windows.

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: How to make windows not lose focus

Post by LAPIII » 26 Jan 2022, 15:18

To escape a script I use ^Esc::ExitApp. To focus a window I use #T:: Winset, Alwaysontop, , A.

Post Reply

Return to “Ask for Help (v1)”