从C#向Java app(Minecraft)发送输入

时间:2011-05-31 14:00:38

标签: c# java

我试图制作一个程序,将一些文本发送到我的游戏,这是一个java应用程序 - 但我在C#编写代码

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Minecraft1
{
public partial class Form1
{ 
    [DllImportAttribute( "User32.dll" )]

    private static extern int FindWindow( String ClassName, String
    WindowName );

    [DllImportAttribute( "User32.dll" )]
    private static extern int SetForegroundWindow( int hWnd );

    private void button1_Click(object sender, EventArgs e)
    {
        //to activate an application
        int hWnd = FindWindow(null, "Minecraft");
        if (hWnd > 0)
        {
            SetForegroundWindow(hWnd);
            SendKeys.Send("/Give metrius 64 64");
        }
    }
}
}

但是当我运行它,并单击按钮时,它会激活窗口并发送类似的内容 “meeeetrrrius”

  • 任何线索? :)

谢谢!

1 个答案:

答案 0 :(得分:0)

在发送密钥之前,窗口可能不在前台 你能尝试一下:

    if (hWnd > 0)
    {
        SetForegroundWindow(hWnd);
        Thread.Sleep(50); // intentional delay to ensure the window is in the foreground
        SendKeys.Send("/Give metrius 64 64");
    }