-1

我对来自 c++ 的一段代码有一个新问题。我有一个按键来进行按键切换或类似的操作......也许代码会比我解释得更好:

while (1) { 
    Sleep (1);
if (GetAsyncKeyState(VK_TAB) & 1) { 
    toggle = !toggle; 
if(toggle) 
    {
        code here...();
    }
else 
    {   
        code here...(); 
        }
    }
}

在 c++ 中一切都很好......但是移植到 C# - WPF,让我崩溃了。知道最简单的方法是什么吗?谢谢,

编辑2:为什么不工作?我已经注册了该操作的密钥..但没有开始。这是因为我已经在方法中了吗?

KeyboardHook hook = new KeyboardHook(); // take action on keys pressed. hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(fireUp); // register the event that is fired after the key press. hook.RegisterHotKey(ModKeys.None, Keys.Tab);

void fireUp(object sender, KeyPressedEventArgs e)
{
    bool toggle = false;
    toggle = !toggle;
    byte[] byt = { 0xC7,0x83,0x1A,0x05,0x00,0x00,0x00,0x00,0x00,0x00 };
for (int i = 6; i < 10; ++i)
    {
        byt[i] = (byte)(atkSpd & 0xFF);
        atkSpd = atkSpd >> 8;
    }
if (toggle)
    {
        Write(vMemory + 8, byt, 10);
        Write(vMemory, BitConverter.GetBytes((vMemory + 8)), 8);
        Write(atkBase, new byte[] { 0xFF, 0x24, 0x25 }, 3);
        Write(atkBase + 3, BitConverter.GetBytes((vMemory)), 4);
    }
else
    {
      Write(atkBase, new byte[] { 0x66, 0x89, 0xB3, 0x1A, 0x05, 0x00, 0x00 }, 7);
    }

}

4

2 回答 2

1

有什么理由不能使用KeyDown/KeyPress/KeyUp事件吗?

private void tabPress(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Tab)
    {
        if(toggle) 
        {
            code here...();
        }
        else 
        {   
            code here...(); 
        }
    }
}

在我看来,这是更好的方法,更不用说更有效(不是一直检查)。

如果您想使用您发布的方法,我会说发生崩溃是因为 while 循环是无限的,不允许发生任何其他事情。如果您提供崩溃时遇到的异常或至少提供有关实际发生情况的更多信息,那将会有所帮助。

于 2013-08-14T23:29:49.157 回答
0

最后,我修复它。

public partial class MainWindow : Window
{
private bool toggle = true;
}

private void fireUp(rwKey hotKey)
    {
        byte[] byt ={0xC7,0x83,0x1A,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x8B,0x74,0x24,
        0x40,0x48,0x8B,0x6C,0x24,0x48,0x48,0x8B,0x5C,0x24,0x50,0x48,0x83,0xC4,0x58,0xC3};
        for (int i = 6; i < 10; ++i) { byt[i] = (byte)(atkSpd & 0xFF); atkSpd = atkSpd >> 8; }

        bool kd = (toggle = !toggle);

        if (kd)
        {
            Write(vMemory + 8, byt, 30);
            Write(vMemory, BitConverter.GetBytes((vMemory + 8)), 8);
            Write(atkBase, new byte[] { 0xFF, 0x24, 0x25 }, 3);
            Write(atkBase + 3, BitConverter.GetBytes((vMemory)), 4);
        }
        else
        {
            Write(atkBase, new byte[] { 0x66, 0x89, 0xB3, 0x1A, 0x05, 0x00, 0x00 }, 7);
        }
    }
于 2013-08-17T12:11:09.630 回答