14

I have the following code:

public void tbSpeed_KeyDown(object sender, KeyRoutedEventArgs e)
{
    e.Handled = !((e.Key >= 48 && e.Key <= 57) || (e.Key >= 96 && e.Key <= 105) || (e.Key == 109) || (e.Key == 189));
}

Is there any way to detect if any modifier key like shift is being pressed ?

4

4 回答 4

21

使用GetKeyState。例如

var state = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
return (state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;

注意:对于 Alt,您将使用 VirtualKey.Menu。

于 2012-10-22T11:22:55.573 回答
7

对于 Win10 UWP,我注意到 CTRL 和 SHIFT 键设置为锁定状态。所以我做了以下事情:

var shiftState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
var ctrlState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Control);

var isShiftDown = shiftState != CoreVirtualKeyStates.None;
var isCtrlDown = ctrlState != CoreVirtualKeyStates.None;
于 2016-04-26T15:11:24.423 回答
0

你可以试试下面的代码

CoreVirtualKeyStates controlKeyState = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
var  ctrl = (controlKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;

CoreVirtualKeyStates shiftKeyState = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift);
var shift = (shiftKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
于 2012-10-23T09:34:31.953 回答
-1

Bitwise ANDModifiersKeyboard with ShiftKey的属性 -

bool isShiftKeyPressed = (Keyboard.Modifiers & ModifierKeys.Shift)
                         == ModifierKeys.Shift;

也试试这个-

bool isShiftKeyPressed = (ModifierKeys & Keys.Shift) == Keys.Shift;

或者

Control.ModifierKeys == Keys.Shift
于 2012-10-21T19:16:43.707 回答