VBRookie Posted March 8, 2013 Share Posted March 8, 2013 (edited) In a VBScript that's run standalone (e.g. via double-click on a .vbs file), I'm trying to send an ALT-key combination to a non-active window with a given title, e.g. "Command Prompt" (CMD).(In CMD, for example, sending Alt-Space Alt-E Alt-S should call up the menu entry for highlighting everything. But CMD is just a sample for a program without API that I want to control via key sends without making it the active window.) Abusing Word.Tasks, I succeeded in sending normal WM_CHAR messages to CMD via task.sendwindowmessage.However, sending WM_KEYDOWN or WM_SYSKEYDOWN, VK_MENU, WM_SYSKEYUP etc. messages seemed not to have any effect, and WM_SYSCHAR messages or WM_CHAR messages with the ALT-bit 29 set in the lParam result in normal characters being sent. With Notepad, messages did not appear to work at all. Here's the code: ' === getAppTask ===' Gets the task for an application whose windows title contains' the string <appTitle>.' Returns "true" if successfull, "false" if not.' ==================Dim WordSet Word = CreateObject("Word.Application")Function getAppTask(appTitle, ByRef task)dim TasksSet Tasks = Word.TasksFor Each task in TasksIf Task.Visible Thendim tasknametaskname = task.nameif instr(1,taskname,appTitle) thengetAppTask = trueexit functionend ifend ifnextgetAppTask = falseEnd Function' === Test Code ===Const WM_KEYDOWN = &H100Const WM_KEYUP = &H101Const WM_CHAR = &H102Const WM_SYSKEYDOWN = &H104Const WM_SYSKEYUP = &H105Const WM_SYSCHAR =&H106Const WM_SYSCOMMAND =&H112Const WM_CLOSE = &H10Const VK_SHIFT = &H10Const VK_MENU = &H12Const SC_KEYMENU =&HF100dim task' === Test Notepad (must be open with Untitled) ===if getAppTask("Untitled - Notepad", task) thentask.Activate ' just for debugging, so I see the task has been found' this does not work:task.SendWindowMessage WM_CHAR, Asc("A"), 0' this does not work either:task.SendWindowMessage WM_SYSCOMMAND, SC_KEYMENU, Asc("F")task.SendWindowMessage WM_SYSCOMMAND, SC_KEYMENU, Asc("O")end if' === Test CMD (must be open) ===if getAppTask("Command Prompt", task) thentask.Activate ' just for debugging, so I see the task has been found' this works:task.SendWindowMessage WM_CHAR, Asc("A"), 0' this does not work:task.SendWindowMessage WM_SYSKEYDOWN, VK_MENU, &h20000000task.SendWindowMessage WM_SYSKEYDOWN, Asc(" "), &h20000000task.SendWindowMessage WM_SYSKEYUP, Asc(" "), &h20000000task.SendWindowMessage WM_SYSKEYDOWN, Asc("E"), &h20000000task.SendWindowMessage WM_SYSKEYUP, Asc("E"), &h20000000task.SendWindowMessage WM_SYSKEYDOWN, Asc("S"), &h20000000task.SendWindowMessage WM_SYSKEYUP, Asc("S"), &h20000000task.SendWindowMessage WM_SYSKEYUP, VK_MENU, 0' this does not work either:task.SendWindowMessage WM_SYSCHAR, Asc(" "), &h20000000task.SendWindowMessage WM_SYSCHAR, Asc("E"), &h20000000task.SendWindowMessage WM_SYSCHAR, Asc("S"), &h20000000end ifWord.Quit I tried different combinations of WM messages with different lParams, but without success.Maybe PostMessage instead of SendWindowMessage would work, but- I don't know whether it's available in stand-alone VBScript, and- I don't know how to get the HWND of the task (which is NOT started by the script) since I was not able to import FindWindow in stand-alone VBScript. Any suggestions? (Please NOT SendKey since it requires the window to have the focus.)Thanks! VBRookie Edited March 12, 2013 by VBRookie 1 Link to comment Share on other sites More sharing options...
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
Already have an account? Sign in here.Sign In Now