按下键时,GetAsyncKeyState未注册

时间:2018-06-29 13:27:30

标签: c# events keyboard key

我在我的小C#脚本上工作了数小时,该脚本应该能够检测何时按下,按下或释放空间。我为此有两个类,一个叫做KeyContainer,另一个叫做KeyHolder。 KeyHolder类是可以设置键状态,可以忽略键或可以将键删除/添加到KeyContainer的类。 KeyContainer基本上是所有键的容器或包装,它每5毫秒循环遍历每个键并设置键的状态。

KeyHolder

public class KeyHolder
{

    public KeyContainer Container;
    public int vkey;
    public bool down = false;
    public bool ignore = false;
    public DateTime time;

    public EventHandler<KeyHolder> onKeyStateChange;
    public EventHandler<KeyHolder> onKeyDown;
    public EventHandler<KeyHolder> onKeyUp;
    public EventHandler<KeyHolder> onKeyPress;
    public EventHandler<KeyHolder> onIgnoreStateChange;
    public EventHandler<KeyHolder> onIgnored;
    public EventHandler<KeyHolder> onUnignored;
    public EventHandler<KeyHolder> onAdded;
    public EventHandler<KeyHolder> onRemoved;


    public void KeyStateChange () { onKeyStateChange?.Invoke(this, this); Container.KeyStateChange(this); }
    public void KeyDown () { onKeyDown?.Invoke(this, this); Container.KeyDown(this); }
    public void KeyUp () { onKeyUp?.Invoke(this, this); Container.KeyUp(this); }
    public void KeyPress () { onKeyPress?.Invoke(this, this); Container.KeyPress(this); }
    public void IgnoreStateChange () { onIgnoreStateChange?.Invoke(this, this); Container.IgnoreStateChange(this); }
    public void Ignored () { onIgnored?.Invoke(this, this); Container.Ignored(this); }
    public void Unignored () { onUnignored?.Invoke(this, this); Container.Unignored(this); }
    public void Added () { onAdded?.Invoke(this, this); Container.Added(this); }
    public void Removed () { onRemoved?.Invoke(this, this); Container.Removed(this); }

    public KeyHolder (int vkey, KeyContainer Container)
    {
        this.vkey = vkey;
        this.Container = Container;
    }


    public bool Has () { return Container.Has(vkey); }
    public KeyHolder Add () { Container.AddKeyHolder(this); Added(); return this; }
    public KeyHolder Remove ()
    {
        if (Has()) Container.Remove(this.vkey);
        Removed();
        return this;
    }

    public KeyHolder Ignore (bool state)
    {
        if (ignore == state) return this;
        ignore = state;
        IgnoreStateChange();
        if (state)
        {
            Ignored();
        } else
        {
            Unignored();
        }
        return this;
    }

    public KeyHolder IgnoreToggle () { Ignore(!ignore); return this; }

    public KeyHolder SetState (bool isDown)
    {
        if (down == isDown) return this;

        if (isDown && !down)
        {
            time = DateTime.Now;
            down = true;
            KeyStateChange();
            KeyDown();
        } else if (!isDown && down)
        {
            DateTime lTime = time;
            bool iDown = down;

            time = new DateTime(0, 0, 0, 0, 0, 0, 0);
            down = false;

            int tDiff = DateTime.Now.Subtract(lTime).Milliseconds;
            bool kPress = tDiff <= Container.consideredKeyPress;

            KeyStateChange();
            KeyUp();
            if (kPress) KeyPress();
        }

        return this;
    }
}

KeyContainer

public class KeyContainer
{
    [DllImport("user32.dll")]
    static extern short GetAsyncKeyState(Int32 vKey);

    public List<KeyHolder> Keys = new List<KeyHolder>();
    public int consideredKeyPress = 0x3E8;

    public EventHandler<KeyHolder> onKeyStateChange;
    public EventHandler<KeyHolder> onKeyDown;
    public EventHandler<KeyHolder> onKeyUp;
    public EventHandler<KeyHolder> onKeyPress;
    public EventHandler<KeyHolder> onIgnoreStateChange;
    public EventHandler<KeyHolder> onIgnored;
    public EventHandler<KeyHolder> onUnignored;
    public EventHandler<KeyHolder> onAdded;
    public EventHandler<KeyHolder> onRemoved;

    public void KeyStateChange (KeyHolder key) { onKeyStateChange?.Invoke(key, key); }
    public void KeyDown (KeyHolder key) { onKeyDown?.Invoke(key, key); }
    public void KeyUp (KeyHolder key) { onKeyUp?.Invoke(key, key); }
    public void KeyPress (KeyHolder key) { onKeyPress?.Invoke(key, key); }
    public void IgnoreStateChange(KeyHolder key) { onIgnoreStateChange?.Invoke(key, key); }
    public void Ignored(KeyHolder key) { onIgnored?.Invoke(key, key); }
    public void Unignored(KeyHolder key) { onUnignored?.Invoke(key, key); }
    public void Added(KeyHolder key) { onAdded?.Invoke(key, key); }
    public void Removed(KeyHolder key) { onRemoved?.Invoke(key, key); }

    public Timer Ticker;

    public bool Down (int vkey)
    {
        return (GetAsyncKeyState(vkey) & 0x1F40) != 0x0;
    }

    public void TimerCallback (Object o)
    {
        KeyHolder[] keys = Keys.ToArray();
        for (int i = 0; i < keys.Length; i++)
        {
            keys[i].SetState(Down(keys[i].vkey));
        }
    }

    public KeyContainer ()
    {
        Ticker = new Timer(TimerCallback, null, 0, 5);
    }

    public int IndexOf (int vkey)
    {
        int found = -1;

        KeyHolder[] keys = Keys.ToArray();

        for (int i = 0; i < Keys.Count; i++)
        {
            KeyHolder key = keys[i];
            if (key.vkey == vkey) found = i;
        }

        return found;
    }

    public bool Has (int vkey)
    {
        return IndexOf(vkey) != -1;
    }

    public KeyHolder GetIndex (int index)
    {
        return index > -1 && index < Keys.Count ? Keys.ToArray()[index] : null;
    }

    public KeyHolder RemoveIndex(int index)
    {
        List<KeyHolder> nKeys = new List<KeyHolder>();
        KeyHolder[] oKeys = Keys.ToArray();
        KeyHolder Rem = null;
        for (int i = 0; i < oKeys.Length; i++)
        {
            if (index != i)
            {
                nKeys.Add(oKeys[i]);
            } else
            {
                Rem = oKeys[i];
            }
        }

        return Rem;
    }

    public KeyHolder Get (int vkey)
    {
        return Has(vkey) ? GetIndex(IndexOf(vkey)) : null;
    }

    public KeyHolder Remove (int vkey)
    {
        return Has(vkey) ? RemoveIndex(IndexOf(vkey)) : null;
    }

    public KeyContainer AddKeyHolder (KeyHolder key)
    {
        if (!Has(key.vkey)) Keys.Add(key);
        return this;
    }

    public KeyContainer Add (int vkey)
    {
        if (!Has(vkey)) new KeyHolder(vkey, this).Add();
        return this;
    }

}

但是,当我使用KeyContainer注册密钥时,添加它并没有真正听完密钥,我不知道我在做什么错。这是我用于测试的代码。

public class Refs
{
    public void SpacePress (object sender, KeyHolder vkey)
    {
        Console.WriteLine("Space Key Pressed");
    }
    public void KeyDown (object sender, KeyHolder vkey)
    {
        Console.WriteLine("Space Key Down");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Refs refs = new Refs();
        Console.WriteLine("Press Space or something a few times.");
        KeyContainer Keys = new KeyContainer();
        Keys.Add(0x20);
        Keys.Get(0x20).onKeyPress += refs.SpacePress;
        Keys.Get(0x20).onKeyDown += refs.KeyDown;
        Console.ReadLine();
    }
}

如果有人可以告诉我我在哪里上班了,或者我会很高兴:)谢谢!

0 个答案:

没有答案