Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>

Code C#
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;
            }
        }
            
    }
}
Posted

1 solution

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
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 2-Aug-15 8:27am    
I was also looking for a solution to this question. 5ed.
Sergey Alexandrovich Kryukov 2-Aug-15 8:34am    
Thank you, Afzaal.
—SA
doduc812 2-Aug-15 11:08am    
Hi Sergey Alexandrovich Kryukov,

Thank you so much. I got it
Sergey Alexandrovich Kryukov 2-Aug-15 15:13pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900