执行从C#到EuroTruck的键输出不起作用(PostMessage,user32.dll)

问题描述 投票:0回答:2

我试图让我的C#脚本输出到ets2,以便它将驱动我(wasd)。为了测试我正在使用空格键。我已经在chrome和notepad中测试了代码,它在哪里工作并放下一个空间。谁会知道出了什么问题?

更新:我使用键盘模块为python编写了一些测试代码,我让它工作。是否有可能将“空间”变成我可以从C#改变的变量?

Python代码:

import keyboard, time
time.sleep(5)
keyboard.press_and_release("space")

Spy ++中的线程和Windows:

enter image description here enter image description here

我使用以下代码:

    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();
}
c# keyboard postmessage user32
2个回答
0
投票

您的代码立即退出。这是故意的吗?在main的末尾调用Console.Readline()来阻止,直到你给控制台输入。


0
投票

我自己找到了(临时)修复程序。但是,此修复程序不是非常友好。任何有更好建议的人都欢迎。

使用我在我的问题中描述的键盘功能,我制作了一个使用参数的python脚本。这个python脚本由C#脚本使用以下代码启动:

static public string run_python(string cmd, string args)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:\Users\Stijn\AppData\Local\Programs\Python\Python36\python.exe";
        start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd,args);
        start.UseShellExecute = false;// Do not use OS shell
        start.CreateNoWindow = true; // We don't need new window
        start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
        start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
                string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
                return result + stderr;
            }
        }
    }

python代码包括:

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])

使用键盘模块可以在https://pypi.org/project/keyboard/找到

© www.soinside.com 2019 - 2024. All rights reserved.