简体   繁体   English

如何统一模拟键盘按键?

[英]How to simulate keyboard keys in unity?

I am currently working on my grad project, its an autopilot and I am making the plane in unity and all the machine learning stuff in python, the python code will return only what the plane will do "up, down, left, right, enginepower up,...) and the c# code will take care of all the physics and so on, so I want to simulate that if (left) then the computer will press (A). 我目前正在我的研究生项目中工作,它是自动驾驶仪,我正在统一制作飞机,所有机器学习内容都在python中,python代码将仅返回飞机将进行的“上,下,左,右,引擎动力” ,...),而c#代码将处理所有物理问题,依此类推,因此我想模拟一下,如果(向左),计算机将按(A)。

I've tried inputsimulator but that's all. 我已经尝试过inputsimulator,仅此而已。

string t = GUIUtility.systemCopyBuffer; // this will get the input from python

if (t=="left") // if what the neural network choose is left do this
{
    //what I need to convert the "left" to an actual keyboard press to
    //be used by the crossplatformmanager
}
float roll = CrossPlatformInputManager.GetAxis("Horizontal");

I expect that: 我希望:

if (t=="left")
{ 
    keyboardbutton="A" 
}

and that CrossPlatformInputManager.GetAxis will treat it as if i physically pressed the key. 并且那个CrossPlatformInputManager.GetAxis将把它当作我实际按下该键一样。

I think this is a XY problem, usually input simulation is need for program that is out of your control, because you can't access the code directly. 我认为这是一个XY问题,通常对于无法控制的程序需要输入仿真,因为您无法直接访问代码。 But for your question both python and unity project are written by yourself, so you don't need simulate the input, just call the method. 但是对于您的问题,python和unity项目都是由您自己编写的,因此您无需模拟输入,只需调用该方法即可。

For example, if you wanna roll something by horizontal axis input, you may do: 例如,如果您想通过水平轴输入来滚动某物,则可以这样做:

var h = Input.GetAxis("Horizontal");
Roll(h);

Then if the value is from the clipboard: 然后,如果该值来自剪贴板:

float h;
string t = GUIUtility.systemCopyBuffer;
switch(t) {
   case "left": h = -1f; break;
   case "right": h = 1f; break;
   ....
}
Roll(h);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM