C#SendKeys到应用程序

时间:2018-12-08 17:12:19

标签: c# .net

public void Press(Keys key)
{
    WindowsAPI.PostMessage(this.Handle, 260u, new UIntPtr((uint)key), UIntPtr.Zero);
}

public void Release(Keys key)
{
    WindowsAPI.PostMessage(this.Handle, 261u, new UIntPtr((uint)key), UIntPtr.Zero);
}

public void gepardkey(Keys key)
{
    this.Press(key);
    Thread.Sleep(10);
    this.Release(key);
}

private void ragnarokgepard_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    bool flag2 = this.currentHP / this.maxHP *100.0<Convert.ToDouble(this.numHPPercent);
    if (flag2)
    {
        this._windowsAPI.gepardkey(Keys.F1);
    }

    bool flag3 = this.currentSP / this.maxSP *100.0<Convert.ToDouble(this.numSPPercent);
    if (flag3)
    {
        this._windowsAPI.gepardkey(Keys.F2);
    }
}

我遇到此错误http://prntscr.com/lsel0z,有人可以帮助我吗? 我已经使用了计时器和private void autopots(),但此错误仍然弹出

这是ragnarok privateserver的培训师

2 个答案:

答案 0 :(得分:1)

我不知道您如何定义PostMessage API 但这里是我的定义:

const UInt32 WM_KEYDOWN = 0x0100;//256
const UInt32 WM_KEYDUP = 0x0101;//257
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);

以及keydown的语法,例如:(密钥是虚拟密钥)

    WindowsAPI.PostMessage(this.Handle, WM_KEYDOWN, (int)key, 0);

您使用260作为Keydown的值,使用261作为Keyup的值..... !! ??

答案 1 :(得分:0)

this._windowsAPI为空。确保已在构造函数中或声明时对其进行了初始化。

我假设您已经从WindowsAPI类复制了gepardkey方法。如果您没有复制它,并且它与ragnarokgepard_ProgressChanged方法在同一类中,那么您可以使用以下方法替换ragnarokgepard_ProgressChanged方法:

private void ragnarokgepard_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    bool flag2 = this.currentHP / this.maxHP *100.0<Convert.ToDouble(this.numHPPercent);
    if (flag2)
    {
        gepardkey(Keys.F1);
    }

    bool flag3 = this.currentSP / this.maxSP *100.0<Convert.ToDouble(this.numSPPercent);
    if (flag3)
    {
        gepardkey(Keys.F2);
    }
}