我正在尝试将我的C#脚本输出到ets2中,以便它将为我驱动(wasd)。为了测试,我使用空格键。我已经在chrome和记事本中测试了代码,可以在其中工作并放置一个空格。有人知道怎么了吗?
更新: 我使用键盘模块为python编写了一些测试代码,然后它开始工作。是否可以将“空格”变成一个我可以从C#更改的变量?
Python代码:
import keyboard, time
time.sleep(5)
keyboard.press_and_release("space")
Spy ++中的线程和Windows:
我使用以下代码:
public const int WM_KEYDOWN = 0x0100;
const int VK_SPACE = 0x20;
static void Main(string[] args)
{
System.Threading.Thread.Sleep(2000); // gives user time to switch tabs
IntPtr programloc = WindowHelper.GetForegroundWindow();
// I also tried using (from Spy++) FindWindow("Euro Truck Simulator 2", "prism3d");
if (programloc == IntPtr.Zero) throw new SystemException();
WindowHelper.PostMessage(programloc, WM_KEYDOWN, VK_SPACE, 0);
}
和以下模块WindowHelper(多个Stackoverflow和docs.microsoft页面的组合):
class WindowHelper
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName);
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(
IntPtr hwndParent,
IntPtr hwndChildAfter,
string lpszClass,
string lpszWindos);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
public static extern IntPtr GetForegroundWindow();
}
答案 0 :(得分:0)
您的代码将立即退出。这是故意的吗?调用main末尾的Console.Readline()进行阻止,直到您输入控制台输入为止。
答案 1 :(得分:0)
我自己已经找到了(临时)修复程序。但是,此修复对性能的要求不是很高。欢迎有更好建议的人。
使用我在问题中描述的键盘功能,我制作了一个使用参数的python脚本。此python脚本由C#脚本使用以下代码启动:
from keyboard import press_and_release
from sys import argv as args
for i in range(len(args)-1):
press_and_release(args[i+1])
python代码包括:
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,//<--------- When I change this I have two different problem
body: ListView(
children: <Widget>[
TextField(),
SizedBox(height: 100.0,),
TextField(),
SizedBox(height: 100.0,),
TextField(),
SizedBox(height: 100.0,),
TextField(),
SizedBox(height: 100.0,),
TextField(),
SizedBox(height: 100.0,),
TextField(),
SizedBox(height: 100.0,),
TextField(),
],
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add), onPressed: () {},),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
color: Colors.red,
child: new Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(icon: Icon(Icons.menu), onPressed: () {},),
IconButton(icon: Icon(Icons.search), onPressed: () {},),
],
),
),
);
}