%SendKeysTo% - Using Special Batch Command for Keystrokes

I know the answer may not be here in this community, but I’m going to take a chance and toss this out here if anyone may know a solution.

I’m attempting to send a keystroke command of CTRL + N on a window I want to duplicate using batch commands.

See below:

from batch script:

cd "C:\"
set SendKeysTo=CScript //nologo //E:JScript
%SendKeysTo%("~Desktop", "{CTRL}N")

Input error. There is no script file specified.

Need this to optimize my computer startup. :satellite:

So when you open a command prompt, and type in:
CScript //nologo //E:JScript("~Desktop", "{CTRL}N")
… Does it do what you want it to do?

I’m gonna guess not, because looking at the basic syntax for cscript | Microsoft Learn, it requires a scriptname (with extension) as its first unflagged parameter, and you’re not providing one.

Figure out the command you want to send first, then try and put it into batch :wink:

No, I’m still stuck on this.
I have been messing around with several different commands in cmd.exe.

This latest command doesn’t generate any error message (which is new), but it still seems my syntax is incorrect or something or that I’m using the function improperly:

set SendKeysTo=WScript /b /nologo /E:WScript

No result. I am trying different forms of the above including sending a string of text to notepad.exe, and it just returns the next line for another command. Seems like I may be on to something, but it’s not performing as desired.
Spent a while trying to figure this out. :face_with_monocle:

Is there some file / package of files that isn’t being accessed or isn’t available that has the API for these two functions?

If you have Visual Studio then you can use Spy++ (in the Tools menu) to determine what messages are being sent from where and to where. So you can determine if the messages are sent to the intended window and that can help diagnose what is happening. If you do not have VS then I found Winspector - Ultimate Windows Message Spy. I do not know if it works or anything, it might help.

People often have problems using functions like SendKeys. I do not do things like that. I send the actual messages, not just keyboard messages, to windows of other processes. The concepts and techniques are generally the same. If it were me, I would use Spy++ if I was not sure what the message looks like. I wrote an article about sending messages to windows of other processes but I am not sure they will let me post a link here. I will try. See Clicking a Button in Another Application. Of course you cannot do that from a batch file. The general idea is:

  • Find the other process
  • Get the handle of the window you want
  • Use GetDlgItem to get the control that you need to send a message to
  • Send the message

Probably the best solution for you is to learn PowerShell. PowerShell can be considered a replacement for batch files but PowerShell is much more powerful. I have not learned PowerShell; I most likely would just write a C# program to do what I need to do. PowerShell however can use much of what is in .Net; PowerShell is much more like C#, in terms of what you can do with it, than batch files. SendKeys Class (System.Windows.Forms) is something in .Net. Creating a Custom Input Box - PowerShell shows use of .Net in PowerShell.

I am not sure whether PowerShell is automatically installed with Windows; you might need to go to the Windows Programs and Features or whatever and install it. Or install PowerShell from the Microsoft Store. If PowerShell is installed then you can simply copy the sample code from the Custom Input Box page above into a file with a ps1 extension then go to a PowerShell window and execute the file. If you learn PowerShell then you will be able to do a whole lotta automating.

Microsoft has tutorials about PowerShell. Let me know if you want help finding them.

Again… nowhere in their code do they actually invoke a SendKeys command.

Calling a variable SendKeysTo does not automagically create a command or tell the batch shell (or cscript, or wscript) you want to do anything.

WScript(“aplace”,“CtrlN”) doesnt… do anything. Or make sense. Or…

It’s like naming a text file “Manual for Washing Machine.doc” and expecting to open it and find that Word has created a washing machine manual for you.

Personally i’d be using something like AutoIt to do something like this, that at least has contextual senses for things like… the window not being open yet.

Yes, I agree. I have only created a variable and don’t call any pre-defined function.

So, I downloaded AutoIt with the following objectives in mind:

  1. send keystrokes or keyboard combos to any open program or window name
  2. move / drag windows to specified coordinates in the screen resolution
  3. click anywhere on the screen

I reviewed some of the example scripts (.au3) to see some of the sample functions, but I don’t see a list of sample functions to search for something equivalent to SendKeysTo.

The original question stems from my desire to have 2 instances of the Desktop folder in my spread of open folders. When you try to open a secondary window of Desktop, the default behavior of Windows is to activate the original open folder. The way to work around this limitation is to press `{CTRL}N" in the active Desktop window.

Both replies yesterday were helpful. Just not sure where additional documentation on pre-defined functions in AutoIt3 and if it is suitable for my aims of automating my startup process. :desktop_computer:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.