private static bool IsPressedState(List <Keys> pressedKeys, Keys key, bool state) { foreach (Keys pressedKey in pressedKeys) { if (pressedKey == key) { return(state); } } return(!state); }
private static Keys XnaKeyToKey(XnaKeys key) { Keys kv = Keys.None; switch (key) { case XnaKeys.OemComma: kv = Keys.Oemcomma; break; case XnaKeys.OemTilde: kv = Keys.Oemtilde; break; case XnaKeys.OemPlus: kv = Keys.Oemplus; break; default: if (Enum.IsDefined(typeof(Keys), key.ToString()) | key.ToString().Contains(",")) { kv |= (Keys)Enum.Parse(typeof(Keys), key.ToString()); } break; } if (key == XnaKeys.LeftShift) { kv = Keys.LShiftKey | Keys.Shift; } if (key == XnaKeys.RightShift) { kv = Keys.RShiftKey | Keys.Shift; } return(kv); }
/// <summary> /// Gets the <code>char</code> value represented by the <see cref="Keys"/> that was sent. /// </summary> /// <param name="key">The <see cref="Keys"/> enumeration value to examine.</param> /// <returns>A <code>char</code> that represents the ASCII value of the key.</returns> public static char GetKeyChar(Keys key) { if ((key & Keys.Shift) == Keys.Shift) { key ^= Keys.Shift; if ((int)key >= 0x41 && (int)key <= 0x5A) { return (char)key; } // We made this a massive `switch` statement for the speed it provides. switch (key) { case Keys.D1: return '!'; case Keys.D2: return '@'; case Keys.D3: return '#'; case Keys.D4: return '$'; case Keys.D5: return '%'; case Keys.D6: return '^'; case Keys.D7: return '&'; case Keys.D8: return '*'; case Keys.D9: return '('; case Keys.D0: return ')'; case Keys.OemPeriod: return '>'; case Keys.Oemcomma: return '<'; case Keys.OemQuestion: return '?'; case Keys.OemOpenBrackets: return '{'; case Keys.OemCloseBrackets: return '}'; case Keys.OemSemicolon: return ':'; case Keys.OemQuotes: return '"'; case Keys.Oemtilde: return '~'; case Keys.Oemplus: return '+'; case Keys.Separator: return '_'; case Keys.OemPipe: return '|'; case Keys.Divide: return '/'; case Keys.Multiply: return '*'; case Keys.Subtract: return '-'; case Keys.Add: return '+'; } return (char)key; } else { if ((int)key >= 'A' && (int)key <= 'Z') { return (char)((int)key + 32); } if ((int)key >= '0' && (int)key <= '9') { return (char)key; } switch (key) { case Keys.Separator: return '-'; case Keys.Oemplus: return '='; case Keys.Divide: return '/'; case Keys.Multiply: return '*'; case Keys.Subtract: return '-'; case Keys.Add: return '+'; case Keys.NumPad0: case Keys.NumPad1: case Keys.NumPad2: case Keys.NumPad3: case Keys.NumPad4: case Keys.NumPad5: case Keys.NumPad6: case Keys.NumPad7: case Keys.NumPad8: case Keys.NumPad9: return key.ToString().Substring(6, 1)[0]; case Keys.OemPeriod: return '.'; case Keys.Oemcomma: return ','; case Keys.OemQuestion: return '/'; case Keys.OemPipe: return '\\'; case Keys.OemOpenBrackets: return '['; case Keys.OemCloseBrackets: return ']'; case Keys.OemSemicolon: return ';'; case Keys.OemQuotes: return '\''; case Keys.Oemtilde: return '`'; case Keys.Decimal: return '.'; default: return (char)key; } } }
private static bool IsKeyUp(List<Keys> pressedKeys, Keys key) => IsPressedState(pressedKeys, key, false);
private static bool IsKeyDown(List<Keys> pressedKeys, Keys key) => IsPressedState(pressedKeys, key, true);
private static bool IsPressedState(List<Keys> pressedKeys, Keys key, bool state) { foreach (Keys pressedKey in pressedKeys) { if (pressedKey == key) { return state; } } return !state; }
/// <summary> /// Gets the <code>char</code> value represented by the <see cref="Keys"/> that was sent. /// </summary> /// <param name="key">The <see cref="Keys"/> enumeration value to examine.</param> /// <returns>A <code>char</code> that represents the ASCII value of the key.</returns> public static char GetKeyChar(Keys key) { if ((key & Keys.Shift) == Keys.Shift) { key ^= Keys.Shift; if ((int)key >= 0x41 && (int)key <= 0x5A) { return((char)key); } // We made this a massive `switch` statement for the speed it provides. switch (key) { case Keys.D1: return('!'); case Keys.D2: return('@'); case Keys.D3: return('#'); case Keys.D4: return('$'); case Keys.D5: return('%'); case Keys.D6: return('^'); case Keys.D7: return('&'); case Keys.D8: return('*'); case Keys.D9: return('('); case Keys.D0: return(')'); case Keys.OemPeriod: return('>'); case Keys.Oemcomma: return('<'); case Keys.OemQuestion: return('?'); case Keys.OemOpenBrackets: return('{'); case Keys.OemCloseBrackets: return('}'); case Keys.OemSemicolon: return(':'); case Keys.OemQuotes: return('"'); case Keys.Oemtilde: return('~'); case Keys.Oemplus: return('+'); case Keys.Separator: return('_'); case Keys.OemPipe: return('|'); case Keys.Divide: return('/'); case Keys.Multiply: return('*'); case Keys.Subtract: return('-'); case Keys.Add: return('+'); } return((char)key); } else { if ((int)key >= 'A' && (int)key <= 'Z') { return((char)((int)key + 32)); } if ((int)key >= '0' && (int)key <= '9') { return((char)key); } switch (key) { case Keys.Separator: return('-'); case Keys.Oemplus: return('='); case Keys.Divide: return('/'); case Keys.Multiply: return('*'); case Keys.Subtract: return('-'); case Keys.Add: return('+'); case Keys.NumPad0: case Keys.NumPad1: case Keys.NumPad2: case Keys.NumPad3: case Keys.NumPad4: case Keys.NumPad5: case Keys.NumPad6: case Keys.NumPad7: case Keys.NumPad8: case Keys.NumPad9: return(key.ToString().Substring(6, 1)[0]); case Keys.OemPeriod: return('.'); case Keys.Oemcomma: return(','); case Keys.OemQuestion: return('/'); case Keys.OemPipe: return('\\'); case Keys.OemOpenBrackets: return('['); case Keys.OemCloseBrackets: return(']'); case Keys.OemSemicolon: return(';'); case Keys.OemQuotes: return('\''); case Keys.Oemtilde: return('`'); case Keys.Decimal: return('.'); default: return((char)key); } } }
private static bool IsKeyUp(List <Keys> pressedKeys, Keys key) => IsPressedState(pressedKeys, key, false);
private static bool IsKeyDown(List <Keys> pressedKeys, Keys key) => IsPressedState(pressedKeys, key, true);