如何使用修饰键状态成功散列 System.Windows.Input.Key 值?

发布于 2024-10-16 04:01:25 字数 321 浏览 9 评论 0 原文

我正在尝试编写一个哈希算法,该算法将成功地使用修饰符键状态对 System.Windows.Input.Key 值进行哈希处理,例如:

ctrl = false
shift = true
alt = false
capslock = true
numlock = false
scroll lock = false
key: A

因此,像这样的键值应该与具有不同状态的其他键值分开,例如 ctrl、shift、alt 等但由于这些只是 true 或 false,我不确定如何区分哈希值?

有什么想法吗?它必须足够独特才能处理所有可能的按键组合。

I am trying to write a hashing algorithm that's gonna successfully hash System.Windows.Input.Key values with modifier key states, for instance:

ctrl = false
shift = true
alt = false
capslock = true
numlock = false
scroll lock = false
key: A

So a key value like this should be separated from others with different states for ctrl, shift, alt, etc. But since these are just true or false, I am not sure how to make it distinct for the hash values?

Any ideas? It just have to be unique enough to handle all possible key combinations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡看悲欢离合 2024-10-23 04:01:25

我将构建一个包含能够计算其自己的哈希码的所有值的类,例如:

    class KeyInfo : IEquatable<KeyInfo>
    {
        public bool Ctrl { get; private set; }
        public bool Shift { get; private set; }
        public bool Alt { get; private set; }
        public bool CapsLock { get; private set; }
        public bool NumLock { get; private set; }
        public bool ScrollLock { get; private set; }
        public Keys Key { get; private set; }

        public KeyInfo(bool ctrl, bool shift, bool alt, bool capsLock, bool numLock, bool scrollLock, Keys key)
        {
            this.Ctrl = ctrl;
            this.Shift = shift;
            this.Alt = alt;
            this.CapsLock = capsLock;
            this.NumLock = numLock;
            this.ScrollLock = scrollLock;
            this.Key = key;
        }

        public override bool Equals(object obj)
        {
            return this.Equals(obj as KeyInfo);
        }

        public bool Equals(KeyInfo other)
        {
            if (other == null)
                return false;
            return this.Ctrl == other.Ctrl && this.Shift == other.Shift &&
                   this.Alt == other.Alt && this.CapsLock == other.CapsLock &&
                   this.NumLock == other.NumLock && this.ScrollLock == other.ScrollLock &&
                   this.Key == other.Key;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int hash = 17;
                hash = hash * 23 + this.Ctrl.GetHashCode();
                hash = hash * 23 + this.Shift.GetHashCode();
                hash = hash * 23 + this.Alt.GetHashCode();
                hash = hash * 23 + this.CapsLock.GetHashCode();
                hash = hash * 23 + this.NumLock.GetHashCode();
                hash = hash * 23 + this.ScrollLock.GetHashCode();
                hash = hash * 23 + this.Key.GetHashCode();
                return hash;
            }
        }
    }

归功于 Jon Skeet 对 GetHashCode() 实现的回答

NB

这个类可以有效地用作 Dictionary 键,用于 HashSet 或 LINQ Distinct() 等LINQ 设置的操作。

编辑:

我想强制执行这样一个事实:您不得使用哈希码作为字典键,而应使用整个类。
您不能依赖哈希代码的唯一性,因为哈希它受到冲突

I would build a class containing all values able to compute it's own hash code, like:

    class KeyInfo : IEquatable<KeyInfo>
    {
        public bool Ctrl { get; private set; }
        public bool Shift { get; private set; }
        public bool Alt { get; private set; }
        public bool CapsLock { get; private set; }
        public bool NumLock { get; private set; }
        public bool ScrollLock { get; private set; }
        public Keys Key { get; private set; }

        public KeyInfo(bool ctrl, bool shift, bool alt, bool capsLock, bool numLock, bool scrollLock, Keys key)
        {
            this.Ctrl = ctrl;
            this.Shift = shift;
            this.Alt = alt;
            this.CapsLock = capsLock;
            this.NumLock = numLock;
            this.ScrollLock = scrollLock;
            this.Key = key;
        }

        public override bool Equals(object obj)
        {
            return this.Equals(obj as KeyInfo);
        }

        public bool Equals(KeyInfo other)
        {
            if (other == null)
                return false;
            return this.Ctrl == other.Ctrl && this.Shift == other.Shift &&
                   this.Alt == other.Alt && this.CapsLock == other.CapsLock &&
                   this.NumLock == other.NumLock && this.ScrollLock == other.ScrollLock &&
                   this.Key == other.Key;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int hash = 17;
                hash = hash * 23 + this.Ctrl.GetHashCode();
                hash = hash * 23 + this.Shift.GetHashCode();
                hash = hash * 23 + this.Alt.GetHashCode();
                hash = hash * 23 + this.CapsLock.GetHashCode();
                hash = hash * 23 + this.NumLock.GetHashCode();
                hash = hash * 23 + this.ScrollLock.GetHashCode();
                hash = hash * 23 + this.Key.GetHashCode();
                return hash;
            }
        }
    }

Credits to this Jon Skeet's answer for the GetHashCode() implementation.

N.B.

this class can be effectively used as a Dictionary key, into HashSet or in LINQ Distinct() and other LINQ sets' operations.

EDIT:

I'd like to enforce the fact that you mustn't use the hash code as dictionary key, but use the whole class instead.
You cannot rely on the uniqueness of the hash-codes because hashing it's subjected to collisions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文