Alex F
Alex F

Reputation: 955

AHK shortcuts for Alt+Space in Windows

running AHK in Windows 10, and I'm trying to create shortcuts for the Alt+Space menu. Specifically, I often use this sequence:

  1. Press keys Alt+Space (bring up the window menu)
  2. Press key S (select Size to resize)
  3. Press key Left (now moving the mouse is resizing your window horizontally)
  4. Press key Up (now moving the mouse is resizing your window in all directions)

Once this sequence is pressed, you can move your mouse to resize the active window as if you click-dragged the top-right corner, without having to hunt for that corner. (to finalize the resizing you can press Enter of LeftMouseKey, or Esc to abort). I'd like to get to this state with a single shortcut.

Here is my current script, binding to Winkey+Ctrl+Shift+1

;resize window from Top-Left
#^+1::
SendInput !{Space}
Sleep 100
SendInput s
Sleep 100
SendInput {Left}+{Up}
Return

It works mostly, but sometimes the active window will consume the s {Left} {Up} commands, rather than the popup menu. Thus, sometimes this shortcut will result in the active window like VSCode having the "s" character and the cursor 1 line up from before (as if typing s {Left} {Up}), and a visible Alt+Space menu.

I initially used Sleep 10 and thought Sleep 100 would fix this, but it didn't. The shortcut already feels slow with 2x Sleep 100 built in.

I'd like to test if the Alt+Space menu is open before SendInput s and preferably make sure I'm sending to the menu rather than the main application.

Upvotes: 0

Views: 2854

Answers (2)

Jim U
Jim U

Reputation: 3366

I was unable to reproduce the problem using your method. Perhaps try using Send, SendEvent, SendPlay, SendRaw, #InstallKeybdHook, #UseHook

Alternatively, use Autohotkey's WinMove statement:

This resizes the active window such that the upper left hand corner is at the current mouse position

#^+1::
  CoordMode Mouse, screen
  id := WinExist("A")
  WinGetPos x, y, width, height, ahk_id %id%
  MouseGetPos mx, my
  neww := width  + x - mx
  newh := height + y - my
  WinMove % "ahk_id" id,, mx, my, neww, newh
  return

Upvotes: 1

user10364927
user10364927

Reputation:

The menu itself is ahk_class #32768, so waiting for it to exist seems to work for me.

#^+1::
Send , !{space}
WinWait , ahk_class #32768 ,, 1 ; Waits 1s for menu to exist
If !ErrorLevel ; ErrorLevel is 0 if menu exists
    Send , s{left}+{up}
Return

Jim U's alternative solution is a more reliable way of doing what you're trying to achieve, but this will make what you currently have work.

Upvotes: 1

Related Questions