如何将键盘输入从单位(C#)发送到.exe? [英] How can I send keyboard input from unity (C#) to an .exe?

查看:162
本文介绍了如何将键盘输入从单位(C#)发送到.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我有一个基本的exe文件,需要按一个字母键,然后按返回键输入该选择。我试图通过我在Unity中构建的游戏控制这个输入,特别是通过我用按键调用的功能。



目前,当我按' space',调用以下函数打开exe:

Hi all.

I have a basic exe file which requires the pressing of a letter key, followed by the return key to input that selection. I am trying to control this input via a game I've built in Unity, specifically, via functions I call with key-presses.

Currently, when I press 'space', the following function is called which opens the exe:

public static void runStim ()

{
    ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe", "NO_STIMULATOR 1000000 REMOTE");
    startInfo.RedirectStandardOutput = false;
    startInfo.RedirectStandardError = false;
    startInfo.RedirectStandardInput = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = false;

    Process stim = new Process();
    stim.StartInfo = startInfo;    
    stim.Start();
}





虽然这是在游戏中打开并返回,但返回键调用'enterCommand'函数。我需要这个写一个字母给程序,就像你自己打开exe一样。我没有成功。



我被告知我尝试过的方法(下面)尝试通过写入exe文件本身将数据发送到进程而不是流程的输入流 - 任何人都可以帮我实现这个目标吗?



我应该提一下,我的方法可以将字符串写入txt文件。



非常感谢任何帮助。谢谢。



我尝试了什么:





While this is open and back in the game, the return key calls a 'enterCommand' function. I need this to write a letter to the program in the same way you can when the exe is opened on it's own. I haven't been successful in this.

I have been advised that my tried methods (below) try to send data to the process by writing to the exe file itself and not the input stream of the process - can anyone help me achieve this?

I should mention that my method does work with writing a string to a txt file.

Any help is much appreciated. Thank you.

What I have tried:

public static void enterCommand()

{
    using (StreamWriter outputfile = new StreamWriter("C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe", true))
    {
        outputfile.Write("%/Hello");
    }
}




public static void enterCommand()

{
    StreamWriter w;
    w = new StreamWriter(@"C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe");
    w.WriteLine("hello");
}

推荐答案

您需要将命令写入 StandardInput 进程实例的属性:

You need to write the command to the StandardInput property of the Process instance:
ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Users\\Nathan\\Desktop\\stim_data_receiver.exe", "NO_STIMULATOR 1000000 REMOTE");
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;

Process stim = Process.Start(startInfo);
stim.StandardInput.WriteLine("hello");



如果您需要从其他方法发送输入,那么您需要在字段中存储 Process 实例:


If you need to send input from a different method, then you'll need to store the Process instance in a field:

private static Process _stim;

public static void runStim()
{
    ...
    _stim = Process.Start(startInfo);
}

public static void enterCommand()
{
    if (_stim != null && !_stim.HasExited)
    {
        _stim.StandardInput.WriteLine("hello");
    }
}


这篇关于如何将键盘输入从单位(C#)发送到.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆