Sending Keyboard Input to a Program from Command-Line

Sending keyboard input to a program from command-line

xdotool does have a way of sending keystrokes if limited to a focused window:

WID=`xdotool search "Mozilla Firefox" | head -1`
xdotool windowactivate $WID
xdotool key ctrl+l

Send a key-code to a running application in windows

(echo with createobject^("wscript.shell"^)
echo .run "notepad.exe"
echo wscript.sleep 1000
echo .sendkeys "IN-THE-NAME-OF-ALLAH"
echo wscript.sleep 300
echo .sendkeys "(%%F)S"
echo wscript.sleep 2000
echo .sendkeys "myText.txt"
echo .sendkeys "{enter}"
echo wscript.sleep 1000
echo .sendkeys "%%{F4}"
echo end with) > %temp%\sk.vbs
start /w %temp%\sk.vbs

This example show how can send key from cmd to application like Notepad.exe to control .

in your situation no need for this line 2 echo .run "notepad.exe" because you already run your application if you like to run your application through this batch file you will remove notepad.exe and add Full path for your application instead .

and then increase in line 3 sleep time as you like remember 1000 mean 1 sec

in line 6 (%%F)S mean send key Alt+F to open File tab menu then Send key S to choose Save from this menu

in line 11 %%{F4} mean send key Alt+F4 to close the application

How to send hardware level keyboard input by program?

I once used SendInput to control a game character. Game (icy tower?) was using directx input system (I think?) and somehow it was ignoring keybd_event calls but this method worked. I do not know how close to hardware you need to be but did this the trick for me. I used virtual key codes but turned them into scancodes for this answer.

UINT PressKeyScan(WORD scanCode)
{
INPUT input[1] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = NULL;
input[0].ki.wScan = scanCode;
input[0].ki.dwFlags = KEYEVENTF_SCANCODE;

UINT ret = SendInput(1, input, sizeof(INPUT));

return ret;
}

UINT ReleaseKeyScan(WORD scanCode)
{
INPUT input[1] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = NULL;
input[0].ki.wScan = scanCode;
input[0].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;

UINT ret = SendInput(1, input, sizeof(INPUT));

return ret;
}

To simulate press and release you use them sequentially (or you may create a separate function for press and release that use same INPUT structure).

WORD scanCodeSpace = 0x39;
PressKeyScan(scanCodeSpace);
ReleaseKeyScan(scanCodeSpace)

You can use MapVirtualKeyA to get scan code from virtual key code.

Send Windows Key in batch script

There is currently no way to simulate the windows home logo in sendkey's, howevery this does not mean it's not possible.

If you take a look at the windows shortcut keys you will find you can simulate Open Start with the following key combinations: Ctrl + Esc.

To simulate this in batch, you can use: powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^{ESCAPE}') or in your case: %SendKeys% "^{ESCAPE}".

As stated in sendkeys:

  • "^" - Simulates a Ctrl key press.
  • "{ESCAPE}" - Simulates a Esc key press.

Sending Commands to a Windowless Command Line process (started from the C# Application)

Well, I seem to have found an answer to my own question.
It's a real "kludged together" solution, but it works - and for all the intents and purposes of the application I'm building, it doesn't matter.

So, what I did was use two WinAPI functions called

static extern bool ShowWindow(IntPtr WindowHandle, int nCmdShow);
static extern bool SetForegroundWindow(IntPtr WindowHandle);

The first one can be used to Show/Hide a window by changing nCmdShow to 1 and 0 respectively. The other one puts the window (determined by WindowHandle) to the front. Combining these two together, I was able to programmaticly bring the console window up front, do a simple SendKeys.Send(); operation and then hide it again.

// Use a WIN API command to bring the command line to front
SetForegroundWindow(workerProcess.MainWindowHandle);
// Send a keystore to re-display the STATUS of the worker
SendKeys.Send("s");
// Hide the window again.
ShowWindow(workerProcess.MainWindowHandle, 0);

Now, it's a real kludge job, but it gets the job done. One potential pitfall would be if a user is using the computer for something else, and would nail that 1 in a 10000000 moment when the window is active with a 'q' - it would quit the worker program. But the application is intended to be used on dedicated machines that most likely won't even have monitors, keyboards or mice attached to them so it wouldn't be an issue.

Thanks to all who answered, since you did - in one way or another, steer me towards the right solution.

Programatically send keyboard input to graphical application (game) in Windows

It appears that the problem is that while text input generally pulls input from a buffer, realtime games will miss keypresses that come and go as fast as a virtual keyboard can press them. The solution is simply adding a short call to Sleep() between the keypress and the keyup.

Additionally, for anybody working in Python who like me tried the often-suggested SendKeys.py with no success, look for the 'playkeys' function in the code and add time.sleep(pause) after key_down(vk) , this should resolve the problem (it worked for me).

Programmatically send key strokes to a window program in Groovy or bat script

Here is a powershell example to activate winmerge and send some keys.

EDIT: Reduced copy pasta with some .NET variables. $SK = Sendkeys $AA = AppActivate $LRA = Reflect .NET.

$startapp = "C:\DevTools\WinMerge\WinMergeU.exe"
ii $startapp
$SK = "[System.Windows.Forms.SendKeys]::SendWait"
$AA = "[Microsoft.VisualBasic.Interaction]::AppActivate"
$LRA = "[void][System.Reflection.Assembly]::LoadWithPartialName"
Start-Sleep 1
$LRA+'("Microsoft.VisualBasic")'
$AA+'("WinMerge")'
Start-Sleep -m 100
$LRA+'("System.Windows.Forms")'
Start-Sleep -m 100
$SK+'("%F")'
$SK+'("o")'
$SK+'("{ENTER}")'
$SK+'("%T")'
$SK+'("r")'
Start-Sleep 1
$AA+'("Save As")'
Start-Sleep 1
$SK+'("Diff.txt")'
$SK+'("{ENTER}")'

To encapsulate this in a command window, save it to a file PowerShellScript.ps1: Note, changed the command syntax a bit, should work if you use the & {.\dot\source\path}

start /b /wait powershell.exe  -nologo -WindowStyle Hidden -sta -Command "& {.\PowerShellScript.ps1}"


Related Topics



Leave a reply



Submit