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.
107 lines
3.0 KiB
107 lines
3.0 KiB
using Newtonsoft.Json; |
|
using System; |
|
using System.Text; |
|
using System.Windows.Forms; |
|
|
|
namespace NumpadMonitor |
|
{ |
|
public class KeyInfo |
|
{ |
|
public Keys Key { get; private set; } |
|
public int FsModifiers { get; private set; } |
|
public IntPtr KeyCode { get; private set; } |
|
|
|
internal void SetKeyCode(IntPtr keyCode) |
|
{ |
|
if (KeyCode == IntPtr.Zero) |
|
KeyCode = keyCode; |
|
else |
|
throw new ArgumentException("Parameter was already set, and cannot be set again"); |
|
} |
|
|
|
public KeyInfo(Keys key, int fsModifiers = 0) |
|
{ |
|
Key = key; |
|
FsModifiers = fsModifiers; |
|
} |
|
|
|
[JsonConstructor] |
|
public KeyInfo(Keys key, int fsModifiers, IntPtr keyCode) |
|
{ |
|
Key = key; |
|
FsModifiers = fsModifiers; |
|
KeyCode = keyCode; |
|
} |
|
|
|
public static int GetModifierInt(Modifier[] modifiers) |
|
{ |
|
int modifierCode = 0; |
|
foreach (Modifier modifier in modifiers) |
|
{ |
|
switch (modifier) |
|
{ |
|
case Modifier.Alt: |
|
modifierCode |= 0b0001; |
|
break; |
|
|
|
case Modifier.Ctrl: |
|
modifierCode |= 0b0010; |
|
break; |
|
|
|
case Modifier.Shift: |
|
modifierCode |= 0b0100; |
|
break; |
|
|
|
case Modifier.Win: |
|
modifierCode |= 0b1000; |
|
break; |
|
|
|
default: |
|
throw new ArgumentOutOfRangeException("modifier", "Modifier not in enum"); |
|
} |
|
} |
|
return modifierCode; |
|
} |
|
|
|
public override string ToString() |
|
{ |
|
StringBuilder sb = new StringBuilder(); |
|
if (FsModifiers != 0) |
|
{ |
|
if ((FsModifiers & 0b0010) == 0b0010) |
|
sb.Append("Ctrl + "); |
|
if ((FsModifiers & 0b0001) == 0b0001) |
|
sb.Append("Alt + "); |
|
if ((FsModifiers & 0b0100) == 0b0100) |
|
sb.Append("Shift + "); |
|
if ((FsModifiers & 0b1000) == 0b1000) |
|
sb.Append("Win + "); |
|
} |
|
sb.Append(Key.ToString()); |
|
return sb.ToString(); |
|
} |
|
|
|
public override bool Equals(object obj) |
|
{ |
|
return obj is KeyInfo info && |
|
Key == info.Key && |
|
FsModifiers == info.FsModifiers; |
|
} |
|
|
|
public override int GetHashCode() |
|
{ |
|
int hash = 17; |
|
hash = hash * 23 + (int)Key; |
|
hash = hash * 23 + FsModifiers; |
|
return hash; |
|
} |
|
} |
|
|
|
public enum Modifier |
|
{ |
|
Alt, |
|
Ctrl, |
|
Shift, |
|
Win |
|
} |
|
} |