GUI Button to Win+Shift+Right?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
XkyRauh
Posts: 6
Joined: 21 Jan 2022, 14:23

GUI Button to Win+Shift+Right?

Post by XkyRauh » 21 Jan 2022, 14:32

Hello,

I see many tutorials on using Alt+Tab as part of a hotkey script (i.e. "If I press F10, it'll Alt-Tab, paste data, and then Alt-Tab back to the previous window") but I use a touchscreen without easy access to a physical keyboard (I'm an Elementary School Teacher.) I would like Alt+Tab on a GUI button, to use as part of a larger script.

Background info: I've already put a GUI button on my touchscreen that will swap screens between two specific programs. (I have two monitors, one touch-screen and one regular monitor that displays additional data.) I can use this current button to make two precise programs swap screens by using If(WinExist) and then WinActivate, and sending Win+Shift+Right. It has worked like a charm!

The issue is, I want another button that will basically do Win+Shift+Right on the last active window, in case I'm using a different program to the two in my previous script. But if I try to put Win+Shift+Right on a GUI button, pressing the button makes the button the active window, and Win+Shift+Right won't make the old active window swap screens! I thought if I could add an Alt+Tab instruction to the GUI button, I'd be able to select the previously active window and *then* do the Win+Shift+Right, but my efforts thus far to have Alt+Tab function off of a GUI button have failed. Help!

TL;DR: I'm looking for a GUI button that, when pressed, will make the active window before the button press swap screens, as if I'd pressed Win+Shift+Right on the keyboard. A hotkey won't work, as I don't have access to a physical keyboard when I'm in teaching mode. I need a GUI button. Is this possible?

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

Re: GUI Button to Win+Shift+Right?

Post by mikeyww » 21 Jan 2022, 17:03

If you submit the GUI as a first step upon activating the button, the previously active window will become the active window at that point.

XkyRauh
Posts: 6
Joined: 21 Jan 2022, 14:23

Re: GUI Button to Win+Shift+Right?

Post by XkyRauh » 21 Jan 2022, 22:36

I'm afraid I don't understand quite what that means. Could you explain it to me like I'm 5 years old?

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: GUI Button to Win+Shift+Right?

Post by boiler » 22 Jan 2022, 06:18

This does what mikeyww described:

Code: Select all

Gui, +HwndGuiID
Gui, Add, Button, w200 h130 gMoveWindow, Click to move active window
Gui, Show, NoActivate, Move Window
return

MoveWindow:
	Gui, Submit
	WinWaitNotActive, ahk_id %GuiID%
	
	; start of move window code
	WinMove, A,, 0, 0 ; for demonstration
	; end of move window code
	
	Gui, Show, NoActivate
return

But do I understand it that you don't want to move the currently active window, but the one that was active previous to the current one? With the GUI window temporarily closed, can you now act on the window that was active previous to the current one the way your current script does? What happens if you put your code that you currently use to do that between the comments in place of the WinMove, A,, 0, 0 above?

XkyRauh
Posts: 6
Joined: 21 Jan 2022, 14:23

Re: GUI Button to Win+Shift+Right?

Post by XkyRauh » 22 Jan 2022, 11:15

Oh! That makes sense. I've never used 'Submit' before, but I think I understand what it's doing now. The other buttons I've had have been persistent and remained on-screen even after pressed, as they WinActivate other windows and then do the Shift+Win+Right key combo to swap monitors.

If I'm understanding correctly, 'Submit' will make the button disappear for a moment, which will make the previously active window the active window, so I can do the Shift+Win+Right key combo, and then it will Show itself again? That's lovely! Thanks for the explanation and the help!

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: GUI Button to Win+Shift+Right?

Post by boiler » 22 Jan 2022, 11:24

Yes, and if all you need to do is hide the window, you can use Gui, Cancel or Gui, Hide, while Gui, Submit performs the extra action of saving the contents of the GUI's controls to their associated variables, if any, in addition to hiding the window. Then you can use Gui, Show to display it again after any of those.

XkyRauh
Posts: 6
Joined: 21 Jan 2022, 14:23

Re: GUI Button to Win+Shift+Right?

Post by XkyRauh » 26 Jan 2022, 09:21

I feel like a fool, because this isn't working for me...

Code: Select all

SwapButton:
  Gui, 1:+LastFound +AlwaysOnTop -Caption +ToolWindow +Border
  Gui, 1:Color, 00CC44
  Gui, 1:Font,, Tahoma
  Gui, 1:Add, Text, x10 y5 w40 h30 +Center,
  Gui, 1:Add, Button, x10 y5 w40 h30, Swap
  Gui, 1:Show, x600 y20 NoActivate, Overlay
  GuiControl, -Default, Swap
1ButtonSwap:
  Gui, 1:Submit
  Send +#{Right}
  Gui, 1:Show
  Return  
 
That puts a "Swap" button on the screen, but when I click it, the system's focus shifts to... nothing? No windows change screens. What am I doing wrong? :(

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: GUI Button to Win+Shift+Right?

Post by boiler » 26 Jan 2022, 09:31

Remove the 1 from 1ButtonSwap. You are misapplying the label prefix that would be used for GUI window events. That does not apply to button labels.

You need to put a Return after your GuiControl line or else it will move whatever window is active as soon as you run your script because the code flow will fall through to the ButtonSwap subroutine and execute it immediately. See the documentation on Auto-exectue Section.

If that's your whole script, I don't see the purpose of the SwapButton label at the top of the script.

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

Re: GUI Button to Win+Shift+Right?

Post by mikeyww » 26 Jan 2022, 10:12

Just a trivia point of interest here. A label for a GUI button-triggered routine does often need a prefix that is the GUI name. I recently learned that this is not the case for the default GUI, and that actually having the "1" there fails. It would be true if the GUI had a different name.

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: GUI Button to Win+Shift+Right?

Post by boiler » 26 Jan 2022, 10:17

Good clarification. Thanks, mikeyww.

XkyRauh
Posts: 6
Joined: 21 Jan 2022, 14:23

Re: GUI Button to Win+Shift+Right?

Post by XkyRauh » 26 Jan 2022, 14:41

I'll just give the whole script here, for troubleshooting purposes. I'm sorry to be a burden, here!

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.

SwapButton:
  Gui, 1:+LastFound +AlwaysOnTop -Caption +ToolWindow +Border
  Gui, 1:Color, 00CC44
  Gui, 1:Font,, Tahoma
  Gui, 1:Add, Text, x10 y5 w40 h30 +Center,
  Gui, 1:Add, Button, x10 y5 w40 h30, Swap
  Gui, 1:Show, x1620 y20 NoActivate, Overlay
  GuiControl, -Default, Swap

ScreenSwapper:
  Gui, 2:+AlwaysOnTop -Caption +ToolWindow +Border
  Gui, 2:Color, 00FF00
  Gui, 2:Font,, Tahoma
  Gui, 2:Add, Text, x10 y5 w200 h30 +Center, 
  Gui, 2:Add, Button, x10 y5 w200 h30, Switch
  Gui, 2:Show, x1400 y20 NoActivate, Overlay
  GuiControl, -Default, Switch

YellowSlip:
  Gui, 3:+AlwaysOnTop -Caption +ToolWindow +Border
  Gui, 3:Color, FFFF00
  Gui, 3:Font,, Tahoma
  Gui, 3:Add, Text, x10 y5 w60 h30 +Center,
  Gui, 3:Add, Button, x10 y5 w60 h30, YSlip
  Gui, 3:Show, x1320 y20 NoActivate, Overlay
  GuiControl, -Default, YSlip
  Return

1ButtonSwap:
  Gui, 1:Submit
  WinWaitNotActive, ahk_id %GuiID%
  Send +#{Right}
  Gui, 1:Show
  Return

2ButtonSwitch:
  if WinExist("ahk_exe chrome.exe"){
    WinActivate, ahk_exe chrome.exe
    Send +#{Right}
  }
  if WinExist("ahk_exe inspire.exe"){
    WinActivate, ahk_exe inspire.exe
    Send +#{Right}
  }
  Return

3ButtonYSlip:
  if WinExist("ahk_exe chrome.exe"){
    WinActivate, ahk_exe chrome.exe
    ;MouseClick, left, 1341, 381
    ;Sleep 5
    Send, 5
    Send {Tab}
    Sleep 2
    Send, Yellow Slip
    Sleep 2
    Send {Enter}
    ;MouseMove, 1780, 150
    ;Sleep 2
  }
  Return
So I have my big green "Switch" button that swaps Chrome.exe and Inspire.exe between my two monitors--it has worked great and I love it. The "YSlip" button is for doing data entry in fields for when my students are awarded a Yellow Slip (classroom reward) that takes 5 points in ClassDojo. I'd experimented with doing some mouse clicks to save me a step, but the location of the area to click changed based on which class I was using, so I commented those out for the time being. The YSlip button is working as intended.

Where I'm having the issue is the Swap button, in the teal area. I had hoped that clicking that button would move whatever the in-focus window was (Notepad or Explorer or Finale or any other program I'm working with) to the other monitor/back. Currently, it does nothing but make the active window lose focus. :(

If I remove the 1/2/3 labels from the buttons, they stop working. Having 2ButtonSwitch in my code makes the Switch button work. When I tried changing it to just ButtonSwitch it did not function.

Thank you for your patience and kindness! I am grateful.

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: GUI Button to Win+Shift+Right?

Post by boiler » 26 Jan 2022, 16:02

XkyRauh wrote: If I remove the 1/2/3 labels from the buttons, they stop working. Having 2ButtonSwitch in my code makes the Switch button work. When I tried changing it to just ButtonSwitch it did not function.
As mikeyww pointed out, don't use the 1 label prefix because 1 is the default GUI and it's not expecting the prefix for it, but you need to use 2, 3 or whatever else the GUI's name/number is in those cases.

XkyRauh
Posts: 6
Joined: 21 Jan 2022, 14:23

Re: GUI Button to Win+Shift+Right?

Post by XkyRauh » 31 Jan 2022, 09:23

That seems to have fixed it! Changing the 1- prefix to 4- made everything function as desired.

Huge thank you to boiler and mikeyww for helping me troubleshoot and learn. :) I continue to be grateful!

Post Reply

Return to “Ask for Help (v1)”