3

I love automating daily tasks using AutoHotKey and Python. I like using my keyboard instead of my mouse, so I tend to make hotkeys with AutoHotKey that do various actions that you'd otherwise need to use the mouse for, or that you'd need to use too many keyboard actions to do.

But there are some actions that I don't succeed in automating. For example, the dropdown menu for changing the number of monitors that a VM uses in VMWare Workstation. I think that one thing that would really help me with this automation task, and possibly with more in the future, is to be able to simulate menu item clicks.

Is this possible? I want to fool a program into thinking that one of its menu items were clicked. I know to program in Python and AutoHotKey. Is it possible at all, and specifically in these two languages?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

1 Answers1

1

Presuming that this is Windows, if the app is using standard HMENU items you can do one of two things with (relatively) minimal effort

  1. If there is a hotkey, like Ctrl+S for Save, you could just send Ctrl+S to the window using the SendKeys API (https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx)

  2. If there isn't, you can try to find which WM_COMMAND is sent to the window after the item is selected. You can use Spy++ from Microsoft (https://msdn.microsoft.com/en-us/library/dd460756.aspx) to get the code and then do a PostMessage to that window with the code.

Mind you, you'll need to be sure that you do a FindWindow to get the right HWND handle to send it to.

Best of luck :)

Edit Followup: Quick tutorial on how to use Spy++ with Notepad.exe

  1. Start Spy++
    • NOTE: There are two of them, spyxx.exe and spyxx_amd64.exe. If one doesn't log messages - use the other
    • NOTE: if you started Spy++ first, hit F5 to refresh the window list AFTER Notepad.exe starts
  2. Spy++ main menu > Search > Find Window
  3. on the "Window search" dialog, click and hold on the "target" icon next to "finder tool"
  4. drag your cursor over the Notepad title bar
  5. Spy++ will show Caption: "Untitled - Notepad" and Class: "Notepad"
  6. Click "OK"
  7. You will now see Notepad in the window list
  8. Right-click on that entry and select "Messages"
  9. To control the flow, Main Menu > Messages > Logging Options, Messages Tab
  10. Clear All, only select WM_COMMAND, click ok
  11. in Notepad, go to the Main Menu > Edit > Go to
  12. in Spy++, you will see a line reading "WM_COMMAND wNotifyCode: 0 (sent from a menu) wID:24"

Result: Now we know that if you were to PostMessage(WM_COMMAND, MAKE_WPARAM(0,24), HWND of any Notepad.exe on the system), the "Go to" window would appear.

Jason De Arte
  • 467
  • 1
  • 3
  • 14
  • I tried using Spy++ to listen to WM_COMMAND messages, but I can't see where on the program to do this. Any idea? – Ram Rachum Mar 01 '16 at 18:18
  • I hope that helps :) – Jason De Arte Mar 01 '16 at 19:18
  • I am in the same situation. How can I use the windows ID (wID) that i got from Spy++? here is the line i get from using the tool when i click on my menu item i want to automate: WM_COMMAND wNotifyCode:0 (sent from a menu) wID:57642 – shafuq Oct 07 '18 at 14:07