You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.2 KiB
127 lines
4.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Apewer.WinForm
|
|
{
|
|
|
|
/// <summary>修饰键。</summary>
|
|
public struct ModifierKey : IEquatable<ModifierKey>
|
|
{
|
|
|
|
byte _value;
|
|
bool _alt;
|
|
bool _ctrl;
|
|
bool _shift;
|
|
bool _win;
|
|
|
|
/// <summary>包含 ALT 键。</summary>
|
|
public bool WithALT { get => _alt; }
|
|
|
|
/// <summary>包含 CTRL 键。</summary>
|
|
public bool WithCTRL { get => _ctrl; }
|
|
|
|
/// <summary>包含 SHIFT 键。</summary>
|
|
public bool WithSHIFT { get => _shift; }
|
|
|
|
/// <summary>包含 WIN 键。</summary>
|
|
public bool WithWIN { get => _win; }
|
|
|
|
/// <summary>修饰键组合的值。</summary>
|
|
public byte Value { get => _value; }
|
|
|
|
/// <exception cref="OverflowException"></exception>
|
|
private ModifierKey(byte value)
|
|
{
|
|
_alt = value << 7 >> 7 == 1;
|
|
_ctrl = value << 6 >> 7 == 1;
|
|
_shift = value << 5 >> 7 == 1;
|
|
_win = value << 4 >> 7 == 1;
|
|
_value = Convert.ToByte(value);
|
|
if (_value != value) throw new OverflowException($"参数【{value}】含有无效的键。");
|
|
}
|
|
|
|
private ModifierKey(bool alt, bool ctrl, bool shift, bool win)
|
|
{
|
|
_alt = alt;
|
|
_ctrl = ctrl;
|
|
_shift = shift;
|
|
_win = win;
|
|
|
|
var value = 0;
|
|
if (alt) value += 0x01;
|
|
if (ctrl) value += 0x02;
|
|
if (shift) value += 0x04;
|
|
if (win) value += 0x05;
|
|
_value = Convert.ToByte(value);
|
|
}
|
|
|
|
/// <summary></summary>
|
|
public override int GetHashCode() => _value;
|
|
|
|
/// <summary></summary>
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj is ModifierKey b) return _value == b._value;
|
|
return false;
|
|
}
|
|
|
|
/// <summary></summary>
|
|
public bool Equals(ModifierKey another) => _value == another._value;
|
|
|
|
/// <summary></summary>
|
|
public static implicit operator byte(ModifierKey instance) => instance._value;
|
|
|
|
/// <summary></summary>
|
|
/// <exception cref="OverflowException"></exception>
|
|
public static implicit operator ModifierKey(byte value) => new ModifierKey(value);
|
|
|
|
/// <summary></summary>
|
|
/// <exception cref="ArgumentException" />
|
|
public static ModifierKey operator +(ModifierKey a, ModifierKey b)
|
|
{
|
|
if (a._alt && b._alt) throw new ArgumentException("相加的两个组合键共同包含了 ALT 键。");
|
|
if (a._ctrl && b._ctrl) throw new ArgumentException("相加的两个组合键共同包含了 CTRL 键。");
|
|
if (a._shift && b._shift) throw new ArgumentException("相加的两个组合键共同包含了 SHIFT 键。");
|
|
if (a._win && b._win) throw new ArgumentException("相加的两个组合键共同包含了 WIN 键。");
|
|
|
|
var alt = a._alt || b._alt;
|
|
var ctrl = a._ctrl || b._ctrl;
|
|
var shift = a._shift || b._shift;
|
|
var win = a._win || b._win;
|
|
return new ModifierKey(alt, ctrl, shift, win);
|
|
}
|
|
|
|
/// <summary></summary>
|
|
public static ModifierKey operator -(ModifierKey a, ModifierKey b)
|
|
{
|
|
var alt = a._alt;
|
|
var ctrl = a._ctrl;
|
|
var shift = a._shift;
|
|
var win = a._win;
|
|
|
|
if (b._alt) alt = false;
|
|
if (b._ctrl) ctrl = false;
|
|
if (b._shift) shift = false;
|
|
if (b._win) win = false;
|
|
|
|
return new ModifierKey(alt, ctrl, shift, win);
|
|
}
|
|
|
|
/// <summary>表示 ALT 修饰键。</summary>
|
|
public static ModifierKey ALT { get; } = new ModifierKey(0x01);
|
|
|
|
/// <summary>表示 CTRL 修饰键。</summary>
|
|
public static ModifierKey CTRL { get; } = new ModifierKey(0x02);
|
|
|
|
/// <summary>表示 SHIFT 修饰键。</summary>
|
|
public static ModifierKey SHIFT { get; } = new ModifierKey(0x04);
|
|
|
|
/// <summary>表示 WIN 修饰键。</summary>
|
|
public static ModifierKey WIN { get; } = new ModifierKey(0x08);
|
|
|
|
}
|
|
|
|
}
|
|
|