Windows key + Left/Right arrow combinations in AutoHotkey

5,256

Solution 1

Your code is fine regarding which keys it sends. It doesn't work, because it sends them too fast. After the #e it takes some time to start the new window.

A simple solution would be to wait a fix amount of time. But a better approch would be to wait dynamicly for the new window. For example achiveable through the COM-Object "Shell.Application" (see answer by Grey) or AHK-Command "WinGet, , List" (see below).

Fix-Wait-Time:

#space::
  startExplorerAttached("Left")
  startExplorerAttached("Right")
return

startExplorerAttached(sideToAttacheTo)
  {
  Send, #e
  Sleep, 500
  Send, #{%sideToAttacheTo%}
  }

Dynamic:

#space::
  startExplorerAttached("Left")
  startExplorerAttached("Right")
return

startExplorerAttached(sideToAttacheTo)
  {
  hWnd := startExplorer()
  if ( hWnd == -1 )
    {
    Msgbox, Could not start/find new explorer window!
    return
    }

  WinActivate, ahk_id %hWnd%
  Send, #{%sideToAttacheTo%}
  }

startExplorer()
  {
  ; Starts a new Windows Explorer with the Computer-View. 
  ; Returns the handle from the new window or -1 if no new window is found

  static AFTER_START_DELAY := 150
  static AFTER_FAILED_SEARCH_DELAY := 20
  static MAX_SEARCH_ATTEMPTS := 100

  explorerWinTitle = ahk_class CabinetWClass
  WinGet, explorerWinHandlesBeforeStart, List, %explorerWinTitle%

  argToStartWithComputerView = /e`,
  Run, explorer.exe %argToStartWithComputerView%
  Sleep, %AFTER_START_DELAY%

  Loop, %MAX_SEARCH_ATTEMPTS%
    {
    WinGet, explorerWinHandlesAfterStart, List, %explorerWinTitle%
    Loop, %explorerWinHandlesAfterStart%
      {
      curAfterStartWinHandle := explorerWinHandlesAfterStart%A_Index%
      isOldWin := false
      Loop, %explorerWinHandlesBeforeStart%
        {
        curBeforeStartWinHandle := explorerWinHandlesBeforeStart%A_Index%
        if ( curBeforeStartWinHandle == curAfterStartWinHandle )
          {
          isOldWin := true
          break
          }
        }

      if ( not isOldWin )
        {
        return curAfterStartWinHandle
        }
      }

    Sleep, %AFTER_FAILED_SEARCH_DELAY%
    }

  return -1
  }

Solution 2

oShellWindows:=(oShell:=ComObjCreate("Shell.Application")).windows

#Space:: ; win+space
   KeyWait, Space
   oShell.MinimizeAll
   Loop, 2 ; any reasonable amount
   {
      curCnt:=oShellWindows.count
      Send, {LWinDown}{vk45}{LWinUp} ; vk45 - e
      While, curCnt=oShellWindows.count
         Sleep, 500
   }
   Exit, oShell.TileVertically, curCnt:=""
Share:
5,256

Related videos on Youtube

Semir
Author by

Semir

Updated on September 18, 2022

Comments

  • Semir
    Semir over 1 year

    I'm trying to make an AutoHotkey script that would run the following sequence once I press Win+Space:

    1. Win+E -- Open an Explorer window
    2. Win+Left Arrow -- Snap to left of screen
    3. Win+E -- Open an Explorer window
    4. Win+Right Arrow -- Snap to right of screen

    I tried the following, but it didn't work:

    # space::
    Send #e
    Send {LWin Down}{Left}{LWin Up}
    Send #e
    Send {LWin Down}{Right}{LWin Up}
    return
    
    • user2995603
      user2995603 about 10 years
      I have some AHK scripts that do what you are trying to do. They are included in my project at code.google.com/p/rwintools. Using my scripts, CTRL+WIN-ARROW will dock the current active window. SHIFT+WIN+ARROW will move the left-top corner of the current window. ALT+WIN+ARROW will move the right-bottom corner of the window. If you just want to know the implementation, you can take a look at the move-window.ahk script. Note, in Win 7, you may want to disable the default window moving hotkeys because they are conflict with my script.