如何使用 sendmessage(C#) 向 cmd.exe 发送退格键
我正在尝试将击键发送到从我的应用程序启动的 cmd.exe。这样做,我能够发送所有键盘字符,但如果我尝试发送退格键,它似乎不会生效。以下是向 cmd.exe 发送消息的代码片段:
SendMessage((int)shell.MainWindowHandle, WM_KEYDOWN, ((int)e.KeyCode), 0);
SendMessage((int)shell.MainWindowHandle, WM_KEYUP, ((int)e.KeyCode), 0);
知道为什么这不起作用吗?从 C# 应用程序发送到 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须 P/Invoke SendInput() 因为退格键是由键盘驱动程序直接处理的。
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);
上述方法可以帮助您输入任意字符串中的字符还考虑大写和空格。
唯一的问题是我面临的是,如果您之前发送消息并且它仍在处理,那么一个字符串具有两次相同的字符,如“channel”,那么输入的字符串是chanel而不是channel。
我目前正在寻找解决这个问题的方法。
Murali 你可以与我分享你的 KEYDOWN 和 KEYUP 代码吗?
BR,
拉胡尔
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
使用ascii代码:
退格键 --> OCT 0x10,字符“BS”
use the ascii code:
backspace --> OCT 0x10, char 'BS'