Jump to content
Tick

How to select (click) the pop-up menu item, when the screen is locked?

Recommended Posts

Posted (edited)
#include <GuiMenu.au3>
#include <SendMessage.au3>

$hWnd = WinWait("[class:#32768]")
;$hMenu = _GUICtrlMenu_GetMenu($hWnd) ;Not working, next line used
$hMenu = _SendMessage($hWnd, 0x01E1) ; MN_GETHMENU - don't know how but it works ))

For $i = 0 To _GUICtrlMenu_GetItemCount($hMenu) - 1
     MsgBox($MB_SYSTEMMODAL, '', _GUICtrlMenu_GetItemText($hMenu, $i) ,2) ;Shows every item of one-level menu
  WinMenuSelectItem($hMenu, "", $i); does not click any of the menu items
Next

Hello dear experts!
I can't figure out how to click on the menu item I need. Please help me figure it out.

Edited by Tick

Share this post


Link to post
Share on other sites

Hi Tick,
The only way I found to solve your case is to Send a couple of Key down then Send Enter when you reach the desired context menu item.
I really wish another reader could provide a better solution for this, as it would be useful in many cases.

Share this post


Link to post
Share on other sites

Hi LarsJ
I tried it yesterday with the correct syntax you indicate (3rd param = text of menu item) but it didn't select the item in the context menu (context menu = right click in Scite Window), for example :

#include <GuiMenu.au3>
#include <SendMessage.au3>

$hWnd = WinWait("[class:#32768]")
;$hMenu = _GUICtrlMenu_GetMenu($hWnd) ;Not working, next line used
$hMenu = _SendMessage($hWnd, 0x01E1) ; MN_GETHMENU - don't know how but it works ))

For $i = 0 To _GUICtrlMenu_GetItemCount($hMenu) - 1
     MsgBox($MB_SYSTEMMODAL, '', _GUICtrlMenu_GetItemText($hMenu, $i) ,1) ;Shows every item of one-level menu
    ; WinMenuSelectItem($hMenu, "", $i); does not click any of the menu items
Next

$iRet = WinMenuSelectItem($hMenu, "", _GUICtrlMenu_GetItemText($hMenu, 0))
ConsoleWrite($iRet & @CRLF) ; 0 ("menu not found") though the context menu is showing.
Sleep(2000)

Maybe I didn't get it and you had something else in mind.
Could you please post OP's amended script that works on your computer ?
Thanks

PS: I also tried yesterday what's in this link, but it didn't help (except to retrieve the Items ID's, Texts etc...)

Share this post


Link to post
Share on other sites
Posted (edited)

Thanks pixelsearchLarsJ!

pixelsearchI think your method is working in common, but not in my case - there are no hotkeys in my menu and arrows are not working.

LarsJI've make a try but with negative result( 

 

#include <GuiMenu.au3>
#include <SendMessage.au3>

$hWnd = WinWait("[class:#32768]")
$hMenu = _SendMessage($hWnd, 0x01E1)

MsgBox($MB_SYSTEMMODAL, '', _GUICtrlMenu_GetItemText($hMenu, 4) ,2)
WinMenuSelectItem($hMenu, "", _GUICtrlMenu_GetItemText($hMenu, 4))

Messagebox shows correct menu item, but  WinMenuSelectItem does not click.

I've use UIAutomate and it works!!

 

#include <UIAutomate.au3>
#include <MsgBoxConstants.au3>

Opt("WinSearchChildren", 1)
Opt("WinTitleMatchMode", 2)
ControlClick(WinGetHandle (" MAIN WINDOW"),"","[CLASS:AfxWnd140; INSTANCE:24]","left",1,2,2); - This string calling the menu
$hWnd = WinWait("[class:#32768]")
$oParent = _UIA_GetElementFromHandle($hWnd)
$aElements = _UIA_FindAllElements($oParent, "ControlType", $UIA_MenuItemControlTypeId)
_UIA_ElementMouseClick($aElements[4],"left",2,2,2,True); - click desired element
;_UIA_ElementDoDefaultAction($aElements[4]) - do the same thing

But I found another problem

 

When screen is locked - I still can not select the menu item.

That Controlclick, which calling the menu - works even when the screen is locked, or even when I disconnected from remote desktop.

That menu is shows on screen, but they both not working on locked screen :(

This is so bad, because my computer must be unlocked all the time...

Maybe you know, what to do to select menu item on locked screen?

 

Maybe I must make the different topic, because original problem was solved?..

 

Edited by Tick

Share this post


Link to post
Share on other sites

I've found the manual))

https://www.autoitscript.com/wiki/FAQ#Why_doesn.27t_my_script_work_on_a_locked_workstation.3F

Quote

On a locked station any window will never be active (active is only dialog with text "Press Ctrl+Alt+Del"). In Windows locked state applications run hidden (behind that visible dialog) and do not have focus and active status. So generally don't use Send() MouseClick() WinActivate() WinWaitActive() WinActive() etc. Instead use ControlSend() ControlSetText() ControlClick() WinWait() WinExists() WinMenuSelectItem() etc. Doing so allows you to interact with an application regardless of whether it is active or not. It's possible to run such a script from scheduler on locked Windows stations.

They says that I must use ControlClick or WinmenuSelectItem. But I can't understand, how to controlclick on menu element, and WinmenuSelectItem is not work.

_UIA_ElementDoDefaultAction

also not working on locked screen((

Share this post


Link to post
Share on other sites

If you are on Windows 10, then I don't think it's possible to automate a menu on a locked PC. For safety reasons. Also, keep in mind that all AutoIt documentation was written before Windows 10 was released.

Share this post


Link to post
Share on other sites

LarsJ  thank you, I understand your message. But I still hope that it is possible to solve this problem somehow ))

Anyway Controlclick is working even on locked screen.. So only one step remains - click on the menu item)

 

Share this post


Link to post
Share on other sites
; Script opens SciTE pop-up menu and selects "Select All" item

#include <GuiMenu.au3>
#include <SendMessage.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

$sText = "Select All" ; menu item text

Sleep(3333) ; lock desktop manually during this time (Win+L)

ControlClick("[Class:SciTEWindow]", "", "Scintilla1", "right")
$hWnd = WinWait("[class:#32768]")
$hMenu = _SendMessage($hWnd, 0x01E1) ; MN_GETHMENU
$iItem = _GUICtrlMenu_FindItem($hMenu, $sText)
$aRect = _GUICtrlMenu_GetItemRect($hWnd, $hMenu, $iItem)
_MouseLeftClick($hWnd, $aRect[0] + 3, $aRect[1] + 3)

Func _MouseLeftClick($hWnd, $X, $Y, $Sleep = 10)
  Local Const $MK_LBUTTON = 0x0001
  _WinAPI_PostMessage($hWnd, $WM_SETCURSOR, $hWnd, _WinAPI_MakeLong($HTCLIENT, $WM_LBUTTONDOWN))
  _WinAPI_PostMessage($hWnd, $WM_LBUTTONDOWN, $MK_LBUTTON, _WinAPI_MakeLong($X, $Y))
  Sleep($Sleep)
  _WinAPI_PostMessage($hWnd, $WM_SETCURSOR, $hWnd, _WinAPI_MakeLong($HTCLIENT, $WM_LBUTTONUP))
  _WinAPI_PostMessage($hWnd, $WM_LBUTTONUP, 0, _WinAPI_MakeLong($X, $Y))
EndFunc

 

Share this post


Link to post
Share on other sites
Posted (edited)

Well done InnI :)
So we can't use MouseClick() as in your simpler 2017 script because of locked screen, right ?

Edit: I'm actually reworking a bit your function from 2017. It will allow me to automate a direct mouseclick on a desired context menu item, instead of these "key down + key enter" I got in a couple of scripts. Thanks for your original function, nicely scripted.

Edited by pixelsearch

Share this post


Link to post
Share on other sites

Is there a specific reason to use your _MouseLeftClick function instead of  ControlClick($hWnd, "", "", "left", 1, $X, $Y), which works fine ?

Share this post


Link to post
Share on other sites

_MouseLeftClick is an extended ControlClick for left mouse button. Some windows need WM_SETCURSOR message. But if ControlClick works fine, use it.

Share this post


Link to post
Share on other sites

Thank you so much InnI - your script is working great in my case!!!

But for some reason, on the first try, I just got the menu bar I needed highlighted but not pressed. I just added another command _MouseLeftClick next, and it worked.

I think this is some kind of specially introduced difficulties, only this menu in the program for some reason does not allow the cursor to move through the menu items.
In any case, I am very grateful, I myself would not have been able to get such a result.

Share this post


Link to post
Share on other sites
20 minutes ago, Tick said:

on the first try, I just got the menu bar I needed highlighted but not pressed

Try replace to ControlClick

;~ _MouseLeftClick($hWnd, $aRect[0] + 3, $aRect[1] + 3)
ControlClick($hWnd, "", "", "left", 1, $aRect[0] + 3, $aRect[1] + 3)

Will anything change?

Share this post


Link to post
Share on other sites

@pixelsearch & @Tick

#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Example()
MsgBox(0, "", 'the end')

Func Example()

    WinWait("[CLASS:Notepad]", "", 10)
    If Not WinExists("[CLASS:Notepad]") Then Return

    Local $hWnd = WinGetHandle("[CLASS:Notepad]")

    ControlFocus($hWnd, "", "Edit1")
    Send("{APPSKEY}")

    Local $0_PopUPhwnd = WinGetHandle("[CLASS:#32768]")
    Local $0_PopUPMenu = _SendMessage($0_PopUPhwnd, 0x01E1)
    Local $0_FindItem = _GUICtrlMenu_FindItem($0_PopUPMenu, 'Paste')

    _WinAPI_PostMessage( $hWnd, $WM_COMMAND, _GUICtrlMenu_GetItemID($0_PopUPMenu, $0_FindItem), 0 )
    ;_WinAPI_PostMessage($0_PopUPhwnd, $WM_LBUTTONDBLCLK, 0, _WinAPI_MakeLong($x, $Y))  ;~ same as controlclick
EndFunc   ;==>Example

 

Share this post


Link to post
Share on other sites
3 hours ago, InnI said:

Try replace to ControlClick

;~ _MouseLeftClick($hWnd, $aRect[0] + 3, $aRect[1] + 3)
ControlClick($hWnd, "", "", "left", 1, $aRect[0] + 3, $aRect[1] + 3)

Will anything change?

It works too, but also when used twice :)

 

Share this post


Link to post
Share on other sites
3 hours ago, jugador said:

@pixelsearch & @Tick

#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Example()
MsgBox(0, "", 'the end')

Func Example()

    WinWait("[CLASS:Notepad]", "", 10)
    If Not WinExists("[CLASS:Notepad]") Then Return

    Local $hWnd = WinGetHandle("[CLASS:Notepad]")

    ControlFocus($hWnd, "", "Edit1")
    Send("{APPSKEY}")

    Local $0_PopUPhwnd = WinGetHandle("[CLASS:#32768]")
    Local $0_PopUPMenu = _SendMessage($0_PopUPhwnd, 0x01E1)
    Local $0_FindItem = _GUICtrlMenu_FindItem($0_PopUPMenu, 'Paste')

    _WinAPI_PostMessage( $hWnd, $WM_COMMAND, _GUICtrlMenu_GetItemID($0_PopUPMenu, $0_FindItem), 0 )
    ;_WinAPI_PostMessage($0_PopUPhwnd, $WM_LBUTTONDBLCLK, 0, _WinAPI_MakeLong($x, $Y))  ;~ same as controlclick
EndFunc   ;==>Example

 

Thanks a lot! It works!

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...