Alt-tab from active window.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Alt-tab from active window.

04 Oct 2021, 05:49

So here is the code.

Code: Select all

^w:: ; 
IfWinExist, Для Т 2021.xls
{
WinActivate, Для Т 2021.xls
}
Sleep 1000
Send {Altdown}{Tab}{Altup}
And the problem is... It doesn't switch to previous window. I can barely see windows switching interface, it literally just blinks. After activating window I cannot press Alt-Tab once even manually. But it's possible to press (and switch) more than once.
Is there a way to solve the problem or at least detour it?
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

04 Oct 2021, 06:43

That is the entire script? It worked here.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

04 Oct 2021, 06:45

Well it should be (and is - in different file) a larger one, but to find out the problem I've shortened it.
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

04 Oct 2021, 07:10

Try the following, with no other code, and no other scripts running.

Code: Select all

F3::
Send !{Tab}
KeyWait, %A_ThisHotkey%
Return
See WinTitle. Window Spy can show you the complete WinTitle information.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

04 Oct 2021, 07:43

And looks like there isn't one problem.
If I have one Excel file opened, the scripts finds the "Microsoft Excel - Для Т 2021.xls" as name and activates it (and I guess even switches to another window). But If there are two Excel files open, It works only with "Для Т 2021.xls" as a name and doesn't switch. I've added couple of lines for check.

Code: Select all

^w:: ; 
IfWinExist, Для Т 2021.xls
{
WinActivate, Для Т 2021.xls
}
else, 
{
MsgBox, Nope
}
Sleep 1000
Send {Altdown}{Tab}{Altup}
Adding your lines doesn't help. It's no use of trying 'em without WinActivate since the unswitching problem (looks like) appears only after activating window.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

04 Oct 2021, 08:06

They say it's kinda Excel glitch or feature...
https://www.autohotkey.com/board/topic/51249-activate-one-of-mutiple-excel-windows/
Guess I've found some workaround for activating, but it still doesn't switch to previous window if 2 Excel files are opened.

Code: Select all

^w:: ; 
IfWinExist, Для Т 2021.xls
{
WinActivate, Для Т 2021.xls
}
else, 
{
WinActivate, Microsoft Excel - Для Т 2021.xls
}
Sleep 1000
Send {Altdown}{Tab}{Altup}
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

04 Oct 2021, 10:52

Code: Select all

GroupAdd, xl, ahk_exe EXCEL.exe
^w::GroupActivate, xl, R
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

05 Oct 2021, 01:02

Now there is no problem to activate a window. Now it is (still) a problem to return to previous since single Alt-Tab doesn't work. Even manual one so I belive it's not because sendind wrong keys.
I guess I need to clearify the idea. I need a script that would switch to Excel, does some stuff there and return to previous window (which name could be different). Or, in a case Excel is active, does some stuff there and return to previous window.
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

05 Oct 2021, 08:10

Code: Select all

#IfWinExist ahk_exe EXCEL.exe
F3::
WinActivate
; Do some stuff
WinMinimize
Return
#IfWinExist
You now have a few different approaches that you can adjust to meet your needs. These will need different kinds of handling according to exactly what you need to happen with existing windows, non-existing windows, multiple windows, etc. At least for me, "switch to Excel" is actually too vague to code.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

06 Oct 2021, 02:37

Code: Select all

WinMinimize
doesn't seem to work. But

Code: Select all

WinMinimize, A
does. Gonna try a bit.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

08 Oct 2021, 01:38

Looks like I've made it to work the way I wanted, but...
Using WinActivate in active window somehow makes active "File" menu so I had to make an extra Send, {Esc} line(s).
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

08 Oct 2021, 05:43

That could happen if your hotkey contains !.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

08 Oct 2021, 06:01

The single one is still ^w. :roll:
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

08 Oct 2021, 06:20

Feel free to post your revised script for additional feedback about it.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

11 Oct 2021, 01:34

That's embarrasing a bit. :D I believe there is a lot of unnecessary stuff there. But here it goes...

Code: Select all

^w:: ; 
IfWinExist, Для Т 2021.xls
{
WinActivate, Для Т 2021.xls
Send, {Esc}
}
else, 
{
WinActivate, Microsoft Excel - Для Т 2021.xls
Send, {Esc}
}

Sleep, 100
Send {F9}
Send, {F5} ; открывает окно перехода к ячейке
sleep 100
send A3 ; номер нужной ячейки (пол)
Send, {Enter}
Sleep 100
Send, {F2} ; вход в ячейку
Send {Shiftdown}{Left}{Shiftup}
Send {controldown}c{controlup}
Send, {Esc}
sleep 100
WinMinimize, A ; Внимание!
Sleep 100
Send, ^f
Send, Пол (м/ж)
sleep 100
Send, {Enter}
sleep 100
Send, {Esc}
sleep 100
Send {Right 2}
Sleep 100
Send {Shiftdown}{Right}{Shiftup}
SendInput ^v ; Вставка блока (пол)
Send, {Right}
sleep 100

Send {Altdown}{Tab}{Altup}
Send, {F5} ; открывает окно перехода к ячейке
sleep 100
send D17 ; номер нужной ячейки
Send {Enter}
Send {Shiftdown}{Down 5}{Right 2}{Shiftup}
sleep 100
Send {controldown}c{controlup}
sleep 100
Send {Altdown}{Tab}{Altup}
Sleep 100
Send, ^f
Sleep 100
Send, Физическая динамическая нагрузка
sleep 100
Send, {Enter}
sleep 100
Send, {Esc}
sleep 100
Send {Down 3}
sleep 100
Send {Altdown}{Home}{Altup}
sleep 100
Send {Shiftdown}{Altdown}{End}{PgDn}{Altup}{Shiftup} ; выделяет таблицу (с текущей ячейки) до конца
sleep 100
Send {Del}
sleep 100
Send {Right 4}
sleep 100
Send {Enter}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
sleep 100
Send {Home}{Up}
sleep 100
Send {Shiftdown}{Down 5}{Right 2}{Shiftup}
sleep 100
SendInput ^v ; Вставка первого блока
Sleep 100
Send, {Down}
Sleep 100

Send {Altdown}{Tab}{Altup}
Send, {F5} ; открывает окно перехода к ячейке
sleep 100
send H17 ; номер нужной ячейки
Send {Enter}
Send {Shiftdown}{Down 5}{Right 2}{Shiftup}
sleep 100
Send {controldown}c{controlup}
Send {Altdown}{Tab}{Altup}
Sleep 100
Send, ^f
Send, Масса поднимаемого и перемещаемого груза вручную
Sleep 100
Send, {Enter}
Sleep 100
Send, {Esc}
Send, {Down 5}
Send {Altdown}{Home}{Altup}
sleep 100
Send {Shiftdown}{Altdown}{End}{PgDn}{Altup}{Shiftup} ; выделяет таблицу (с текущей ячейки) до конца
Send {Del}
Send {Right 4}
Send {Enter}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Send {Home}{Up}
Send {Shiftdown}{Down 5}{Right 2}{Shiftup}
SendInput ^v ; Вставка второго блока
Sleep 100
Send {Down}
Sleep 100
Send {Altdown}{Tab}{Altup}

Send, {F5} ; открывает окно перехода к ячейке
sleep 100
send L17 ; номер нужной ячейки
Send {Enter}
Send {Shiftdown}{Down 5}{Right 3}{Shiftup}
sleep 100
Send {controldown}c{controlup}
Send {Altdown}{Tab}{Altup}
Sleep 100
Send, ^f
Send, Статическая нагрузка
Sleep 100
Send, {Enter}
Sleep 100
Send {Esc}
Send {Down 5}
Send {Altdown}{Home}{Altup}
sleep 100
Send {Shiftdown}{Altdown}{End}{PgDn}{Altup}{Shiftup} ; выделяет таблицу (с текущей ячейки) до конца
Send {Del}
Send {Right 5}
Send {Enter}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Send {Home}{Up}
Send {Shiftdown}{Down 5}{Right 3}{Shiftup}
SendInput ^v ; Вставка блока (стат. нагр.)
Sleep 100
Send {Right} ; тут иначе, надо бы и выше так же сделать, чтоб было одинаково, но лень
Sleep 100

Send {Altdown}{Tab}{Altup}
Send, {F5} ; открывает окно перехода к ячейке
sleep 100
send Q3 ; номер нужной ячейки (поза)
Send {Enter}
Send {Shiftdown}{Right 5}{Shiftup}
sleep 100
Send {controldown}c{controlup}
Send {Altdown}{Tab}{Altup}
Send, ^f
Send, Рабочая поза
Send, {Enter}
Sleep 100
Send {Esc}
Sleep 100
Send {Down 6}
Sleep 100
Send {Altdown}{Home}{Altup}
sleep 100
Send {Shiftdown}{Altdown}{End}{Altup}{Shiftup} ; выделяет строку (с текущей ячейки) до конца
Sleep 100
SendInput ^v ; Вставка блока (поза), в следующем нет поиска
Sleep 100
Send {Down}
Sleep 100
Send {Altdown}{Tab}{Altup}
Send, {F5} ; открывает окно перехода к ячейке
sleep 100
send Q17 ; номер нужной ячейки (наклоны)
Send {Enter}
Send {Shiftdown}{Down 4}{Right}{Shiftup}
sleep 100
Send {controldown}c{controlup}
Send {Altdown}{Tab}{Altup}
Send {Down 3}
Sleep 100
Send {Altdown}{Home}{Altup}
sleep 100
Send {Shiftdown}{Altdown}{End}{PgDn}{Altup}{Shiftup} ; выделяет таблицу (с текущей ячейки) до конца
Send {Del}
Send {Right 3}
Send {Enter}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {controldown}y{controlup}
Sleep 100
Send {Home}{Up}
Send {Shiftdown}{Down 4}{Right}{Shiftup}
SendInput ^v ; Вставка блока (наклоны)
Sleep 100
Send {Right} ; тут иначе, надо бы и выше так же сделать, чтоб было одинаково, но лень
Sleep 100

Send {Altdown}{Tab}{Altup}
Send, {F5} ; открывает окно перехода к ячейке
sleep 100
send T7 ; номер нужной ячейки (перемещения)
Send {Enter}
Send {Shiftdown}{Down 3}{Right 3}{Shiftup}
sleep 100
Send {controldown}c{controlup}
Send {Altdown}{Tab}{Altup}
Send, ^f
Send, - по горизонтали:
Send, {Enter}
Sleep 100
Send {Esc}
Sleep 1000
Send {Shiftdown}{Down 3}{Right 3}{Shiftup} ; Кажется, что "вправо" надо 4 раза нажать. Почему 3 - не понял.
SendInput ^v ; Вставка блока (перемещения)
Sleep 100
Send {Right}
Sleep 500
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

11 Oct 2021, 06:47

You can try this one. It worked in my testing.

Code: Select all

n = 0
#IfWinExist Для Т 2021.xls ahk_exe EXCEL.exe
^w:: ; "I need a script that would switch to Excel, does some stuff there and return to previous window
     ; (which name could be different). Or, in a case Excel is active, does some stuff there and return
     ; to previous window."
If !WinActive()
 WinActivate
Send % ++n "{Enter}"
Sleep, 50
WinMinimize
Return
#IfWinExist
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

18 Oct 2021, 03:15

Thanks for help. I guess I wouldn't need this script for some time (month or so maybe). Gonna check your suggestions then.
FadingDuch
Posts: 55
Joined: 04 Oct 2021, 05:43

Re: Alt-tab from active window.

17 Nov 2021, 01:04

To (sort of) close the topic posting a report. Maybe would be a partial repeat.
Looks like the name of Excel window depends on nubmer of opened Excel sheets. And (maybe) if they opened in the same or different process(es).
So I've solved the problem this way. It surely may be a partial solution, but it's quite often I have 2 or three Excel sheets opened at a time.

Code: Select all

SetTitleMatchMode 2
IfWinExist, Для Т 2021
	WinActivate, Для Т 2021
Most likely I'm (at least a bit) wrong and more advanced users would correct me. As I got it, second matchmode let's the script address to the window, having title you point as a part of it's name. So it helps a lot in a cases you know the name, but it changes slightly in time.
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Alt-tab from active window.

17 Nov 2021, 07:01

Yes, that is what a match mode of 2 does. It seems to meet your need well. :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 320 guests