- 修改键
/// <summary>
/// 获取一个表示哪个修改键(Shift、Ctrl 和 Alt)处于按下状态的值。
/// </summary>
///
/// <returns>
/// <see cref="T:System.Windows.Forms.Keys"/> 值的按位组合。默认值为 <see cref="F:System.Windows.Forms.Keys.None"/>。
/// </returns>
/// <filterpriority>2</filterpriority>
public static Keys ModifierKeys
{
get
{
Keys keys = Keys.None;
if ((int) UnsafeNativeMethods.GetKeyState(16) < 0)
keys |= Keys.Shift;
if ((int) UnsafeNativeMethods.GetKeyState(17) < 0)
keys |= Keys.Control;
if ((int) UnsafeNativeMethods.GetKeyState(18) < 0)
keys |= Keys.Alt;
return keys;
}
}
- 判断按下状态:
var isControlDown = (Control.ModifierKeys & Keys.Control) == Keys.Control;
var isAltDown = (Control.ModifierKeys & Keys.Alt) == Keys.Alt;
var isShiftDown = (Control.ModifierKeys & Keys.Shift) == Keys.Shift;