事件在WPF中同时按下多个键 [英] Event pressing multi keys at the same time in WPF

查看:91
本文介绍了事件在WPF中同时按下多个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF项目中,C#编程语言



当按W键时,我有label1.Context = 1

当按下A键时,我有label1.Context = 2

现在,我想同时按下A键和W键以获得label.Context = 5因此,我该怎么办?



代码:XAML

In WPF project, C# programming languages

When pressing "W" key, I have "label1.Context = 1"
When pressing "A" key, I have "label1.Context = 2"
Now, I want to press "A" key and "W" key at the same time to get "label.Context = 5" as a result, so how can I do?

Code: XAML

<pre lang="xml"><Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" KeyDown="windown_KeyDown">
    <Grid>
        <Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Height="60" Margin="190,90,0,0" VerticalAlignment="Top" Width="175"/>

    </Grid>
</Window>



代码C#


Code C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public void windown_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.W)
            {
                label1.Content = 1;
            }
            if (e.Key == Key.A)
            {
                label1.Content = 2;
            }
        }
            
    }
}

推荐答案

你永远不会真的同时按下多个键。您只能同时按住两个或更多键。你能看到区别么?在低级别,键盘发出序列化的事件序列,没有任何同时,但事件不是按键。这种主要的低级硬件事件是按下按键或释放按键的事件。 (当然,根本没有字符,没有状态,切换和普通键,它们都是相同的;例如,字符出现在更高级别的OS上,具体取决于活动输入法,语言,等等。在WPF事件处理级别,您只处理转换到高级键定义系统的键,具体取决于许多因素,但您仍然可以处理 KeyDown KeyUp 分开。你不必亲自跟踪关键状态。



除了通常的键盘事件处理之外,你需要的是使用<检查关键状态 System.Windows.Input.Keyboard.IsKeyDown

https://msdn.microsoft.com/en-us/library/ms604273%28v=vs.110%29.aspx [ ^ ]。br />


-SA
You never really press multiple keys down at the same time. You only can hold two or more keys down at the same time. Can you see the difference? At low level, keyboard gives out serialized sequence of events, without any "at the same time", but the events are not key presses. Such primary low-level hardware event is the event of pressing a key down or releasing the key. (And of course, there are no characters at all, there are no status, toggle and "normal" keys, they are all equal; the characters, for example, appear on higher level of OS, depending on active input method, language, etc.) At the level of WPF event handling, you only handle the keys translated to the high-level key definition system, depending on many factors, but you still can handle KeyDown and KeyUp separately. You don't really have to track the key status yourself.

All you need, in addition to usual keyboard event handling, is to check up the key state using System.Windows.Input.Keyboard.IsKeyDown:
https://msdn.microsoft.com/en-us/library/ms604273%28v=vs.110%29.aspx[^].

—SA


这篇关于事件在WPF中同时按下多个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆