Send keys to hidden explorer window

Get help with using AutoHotkey and its commands and hotkeys
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Send keys to hidden explorer window

17 Sep 2017, 14:35

I'm trying to open Windows Explorer to a particular folder and send keys to it.

I can open explorer just fine, but then ahk can't find it's PID after it's fetched.

Code: Select all

explorercmd := "explorer /open,c:\temp\test" 
run, %explorercmd%,,, explorerPID

winwait, ahk_pid %explorerPID%, , 10
if(errorlevel)
	msgbox, couldn't find it

controlsend, , ^a, ahk_pid %explorerPID%
if(errorlevel)
        msgbox, didn't work
How can I get a handle on that window after I've created it? My goal in the end is to make it hidden and work with it hidden, so I don't want to use active window.
teadrinker
Posts: 905
Joined: 29 Mar 2015, 09:41
Contact:

Re: Send keys to hidden explorer window

17 Sep 2017, 16:23

Hi, BGM!

Code: Select all

run, %explorercmd%,,, explorerPID

winwait, ahk_pid %explorerPID%, , 10
It's a wrong approach, since the explorer uses its existing instance to open new windows.
You may use something like this:

Code: Select all

FolderPath := A_MyDocuments

DetectHiddenWindows, On
windowsArray := []
WinGet, List, List, ahk_class CabinetWClass
Loop % List
   windowsArray[List%A_Index%] := true

Run, explorer /open`,"%FolderPath%",, Hide
Loop  {
   WinGet, List, List, ahk_class CabinetWClass
   Loop % List
      if !windowsArray.HasKey(List%A_Index%) && hwnd := List%A_Index%
         break 2
}
MsgBox, % WinExist("ahk_id" hwnd)

; to select all items

for window in ComObjCreate("Shell.Application").Windows  {
   if (window.hwnd = hwnd)  {
      ShellFolderView := window.Document
      for item in ShellFolderView.Folder.Items
         ShellFolderView.SelectItem(item, 1)
      break
   }
}
MsgBox, Selected

WinShow
WinActivate
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Send keys to hidden explorer window

17 Sep 2017, 16:38

Wow, that does, indeed fetch the window handle! Thank you!
My real intention is to to controlsend ^z to the window.

controlsend, , ^z, ahk_id %hwnd%
if(errorlevel)
alert("didn't work")

Any idea on that one, too?
teadrinker
Posts: 905
Joined: 29 Mar 2015, 09:41
Contact:

Re: Send keys to hidden explorer window

17 Sep 2017, 16:48

Code: Select all

controlsend, ahk_parent, ^z, ahk_id %hwnd%
But if a window is hidden, this may not work.
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Send keys to hidden explorer window

17 Sep 2017, 18:05

Well, I don't get anything back from errorlevel;
but neither does it seem to work. If I push the keys myself it works, but controlsend doesn't do it, not even if the window isn't hidden.
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Send keys to hidden explorer window

17 Sep 2017, 18:17

Assuming you mean to send ctrl+z to achieve Undo, this might work (tested on Windows 7):

Code: Select all

q::
PostMessage, 0x111, 28699, 0, SHELLDLL_DefView1, A ;WM_COMMAND := 0x111 ;Undo
return
For message IDs:
Automating Windows Explorer - Page 2 - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/5936 ... rer/page-2
Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Send keys to hidden explorer window

17 Sep 2017, 19:57

I'm still not getting any joy here.
And I'm not even yet working on a hidden window.

Code: Select all

	hiddenbuffer := a_detecthiddenwindows
	DetectHiddenWindows, On		;necessary to send commands to hidden explorer windows

	windowsArray := []
	WinGet, List, List, ahk_class CabinetWClass
	Loop % List
	   windowsArray[List%A_Index%] := true

	run, explorer /open,c:\temp\test
	Loop{
	   WinGet, List, List, ahk_class CabinetWClass
	   Loop % List
		  if !windowsArray.HasKey(List%A_Index%) && hwnd := List%A_Index%
			 break 2
	}

       PostMessage, 0x111, 28699, 0, ahk_id %hwnd%,  ;WM_COMMAND := 0x111 ;Undo	
       DetectHiddenWindows, %hiddenbuffer%	
I've tried with this, too: PostMessage, 0x111, 28699, 0, ahk_class CabinetWClass, ahk_id %hwnd%, ;WM_COMMAND := 0x111 ;Undo
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Send keys to hidden explorer window

18 Sep 2017, 03:05

Try this:

Code: Select all

q::
PostMessage, 0x111, 28699, 0, SHELLDLL_DefView1, % "ahk_id " hWnd ;WM_COMMAND := 0x111 ;Undo
return
In Windows XP, you could send the message to the window, in Windows 7, you have to send it to a control.

It's a curious thing to send ctrl+z, if you're sure you want to undo the last Explorer action, whatever that happened to be.

[EDIT:] Btw to undo the last Explorer action, you can also send a message to Desktop.

Code: Select all

q::
PostMessage, 0x111, 28699, 0, SHELLDLL_DefView1, ahk_class Progman ;Undo
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Send keys to hidden explorer window

18 Sep 2017, 08:43

jeeswg - thanks for that! Sending the message to the desktop works, but nothing seems to work trying to send to that explorer window.

Yes, I want to undo the last explorer action; I'm writing a workaround for FreeCommander file-browser, which does not yet have it's own undo command.

This last code works and I don't need the explorer window at all! I wonder if it works in Windows 10?

I searched through the other thread with all the explorer codes, but can't find one for redo (ctrl+y). Any clue for that?
BoBo
Posts: 2519
Joined: 13 May 2014, 17:15

Re: Send keys to hidden explorer window

18 Sep 2017, 09:15

I'd think about to create something like [this] as a workaround that can be handled with AHK afterward!
Related, but probably to low-level: [Capture: A Tool for Behavioral Analysis of Applications and Documents].
Good luck :)
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Send keys to hidden explorer window

18 Sep 2017, 09:38

Here's the finality. It works in Windows 7.

Code: Select all

Explorer_Undo:
	PostMessage, 0x111, 28699, 0, SHELLDLL_DefView1, ahk_class Progman	;send undo command to desktop
return

Explorer_Redo:
	PostMessage, 0x111, 28704, 0, SHELLDLL_DefView1, ahk_class Progman	;send redo command to desktop
return
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Send keys to hidden explorer window

18 Sep 2017, 12:55

I use this to retrieve menu item IDs, although it is not always as simple as sending WM_COMMAND or WM_SYSCOMMAND to a window. Sometimes the menu needs to be updated before sending the message, sometimes the ID numbers change, sometimes it is unclear which window to send the message to.

Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971

I'm curious regarding undo/redo. If you do something manually in Explorer, you can undo/redo it via the menu. AFAIK none of the FileXXX commands in AutoHotkey can be undone/redone in this way. And I would suppose that any Explorer alternatives do things that cannot be undo/redone in this way ... although if they can, that would be interesting.

Also, if there is a way to grab the undo/redo text that would be interesting.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Send keys to hidden explorer window

18 Sep 2017, 13:06

For my purposes, the program that is doing the file actions has settings to allow it to use Windows to perform them. I'm not using ahk to perform any file actions - I'm using FreeCommander to do it, and FC is using Windows, which is how ahk can tell Windows to undo/redo.

Thanks for the reference to the context menu - I think you posted it above, too.

Return to “Ask For Help”

Who is online

Users browsing this forum: Getfree, gwarble, Samantha, swagfag and 141 guests