Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send backspace using sendmessage(C#) to cmd.exe

I am trying to send keystrokes to cmd.exe that I launch from my app. In doing so, I am able to send all the keyboard characters, but if I try to send Backspace, it doesnt seem to take effect. The following is the code snippet to send message to cmd.exe:

SendMessage((int)shell.MainWindowHandle, WM_KEYDOWN, ((int)e.KeyCode), 0);
SendMessage((int)shell.MainWindowHandle, WM_KEYUP, ((int)e.KeyCode), 0);

Any idea why this wouldnt work? What is the best way to send to the stdin of cmd.exe from a C# app?

thanks in advance

like image 953
Murali Avatar asked Sep 03 '25 02:09

Murali


2 Answers

You will have to P/Invoke SendInput() because Backspace is processed by the keyboard driver directly.

string msg = "Hello Cmd";

int i=0; for (i=0; i < msg.Length ; i++)

SendMessage(cmdHwnd, WM_CHAR, (int)msg[i], 0);

the above method helps you input any character in a string also CAPS and Space are considered.

The only problem is what I am facing is if u send message previously and it is still processing then a string having same character twice like "channel" then string entered is chanel and not channel. I am currently finding a solution for this problem.

Murali can u share you code with me for KEYDOWN and KEYUP with KeyCode.

BR,

Rahul

like image 25
rahoolm Avatar answered Sep 05 '25 23:09

rahoolm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!