WPF之功能鍵檢測

  <StackPanel Margin="5">
    <TextBox KeyDown="KeyEvent"></TextBox>
    <TextBlock Name="lblInfo"></TextBlock>
    <Button Click="CheckShift">Check Current Shift State</Button>
  </StackPanel>
        private void KeyEvent(object sender, KeyEventArgs e)
        {
            
            lblInfo.Text = "Modifiers: " +
                e.KeyboardDevice.Modifiers.ToString();

            if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                lblInfo.Text += "\r\nYou held the Control key.";                   
            }
        }

        private void CheckShift(object sender, RoutedEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftShift))
            {
                lblInfo.Text = "The left Shift is held down.";                   
            }
            else
            {
                lblInfo.Text = "The left Shift is not held down.";                   
            }
            
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章