在C#中切换状态 - WPF [英] Toggle state in C# - WPF

查看:174
本文介绍了在C#中切换状态 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

当我按一个键时,我想让一个功能工作在切换(切换)模式,我真的无法想象如何做到这一点。我尝试了很多方法,只有RegisterHotKey方法才能正常工作。但是RegisterHotKey正在覆盖游戏中的映射键,这不是我需要的。所以我正在尝试使用GetKeyState。无论我改变什么,它下面的代码只适用于一个位置......:

Hello,
I want to make a function to work in toggle(switch) mode when i press a key and i really can't figure how to do it. I tried lots of ways and only the "RegisterHotKey" method is working fine. But "RegisterHotKey" is overwriting the mapped key from the game and this is not what i need. So i'm trying to use "GetKeyState". The code below it's only working for one position no matter what i change...:

private void mw_KeyDown(object sender, KeyEventArgs e)
{
    bool toggle = true;

    bool sw = (toggle = !toggle);

    int tog = (GetKeyState(Key.Tab)); 

    if ((tog & 1) == 1)
    {
        if (sw)
        {
            System.Windows.MessageBox.Show("go to second position...!");
        }
    }
    else
    {
        System.Windows.MessageBox.Show("go to first position...!");
    }
}



任何想法或建议我该怎么做?



编辑:在使用MessageBox进行测试时,它运行正常...但是使用我的真实代码,它总是跳到第二部分并退出...:


Any idea or suggestion how can i do this ?

edit: On testing with MessageBox it's working fine...but with my real piece of code it's always jumping to the second part and exit...:

private void mw_KeyDown(object sender, EventArgs e)
{
    if (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; }
        Write(vMemory + 8, byt, 10);
    }
    else
    {
        byte[] byt = { 0xC7, 0x83, 0x1A, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        for (int i = 6; i < 10; ++i) { byt[i] = (byte)(wepSpd & 0xFF); wepSpd = wepSpd >> 8; }
        Write(vMemory + 8, byt, 10);
    }
}



不执行循环的原因是什么?

谢谢,


What could be the reason for not executing the loop ?
Thank you,

推荐答案

你不应该检查关键状态(为什么?),你应该在某个控件或窗口上处理键盘事件。



例如:
You should not check the key state (why?), you should handle keyboard event on some control or window.

For example:
bool toggle;

//...

MyIUElement.KeyDown += (sender, eventArgs) => {
    if (eventArgs.Key == Key.Return) { // you can also check up eventArgs.KeyStates
    // or some other key: if (eventArgs.Key == SomeKeyOfYourChoice) ...
       toggle = !toggle;
       // depending on toggle, show it on UI
    } //if
}; //MyIUElement.KeyDown





您可以使用其他活动,不同的UI元素,不同的处理程序等。



请参阅:

http://msdn.microsoft.com/en-us/library/system.windows。 uielement.keydown.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.input.keyeventargs.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system .windows.uielement_events.aspx [ ^ ]。



-SA



You can use other events, different UI elements, different handlers, etc.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.keyeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement_events.aspx[^].

—SA


这篇关于在C#中切换状态 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆