Reading keyboard modifiers

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyboardInput" Height="300" Width="300">
    <Grid>
        
    </Grid>
</Window>



//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;
using System.Windows.Threading;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        DispatcherTimer dt = new DispatcherTimer();
        public Window1()
        {
            InitializeComponent();
            dt.Interval = TimeSpan.FromSeconds(0.5);
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();
        }

        void dt_Tick(object sender, EventArgs e)
        {
         
            if ((Keyboard.Modifiers &amp; ModifierKeys.Control) != 0)
            {
                Console.WriteLine("ModifierKeys.Control");
            }
            bool homeKeyPressed = Keyboard.IsKeyDown(Key.Home);
            Debug.WriteLine("Home pressed: " + homeKeyPressed);
        }

    }
}

   
    
     


Reading individual key state with Keyboard.IsKeyDown

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyboardInput" Height="300" Width="300">
    <Grid>
        
    </Grid>
</Window>



//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;
using System.Windows.Threading;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        DispatcherTimer dt = new DispatcherTimer();
        public Window1()
        {
            InitializeComponent();
            dt.Interval = TimeSpan.FromSeconds(0.5);
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();
        }

        void dt_Tick(object sender, EventArgs e)
        {
         
            if ((Keyboard.Modifiers &amp; ModifierKeys.Control) != 0)
            {
                Console.WriteLine("ModifierKeys.Control");
            }
            bool homeKeyPressed = Keyboard.IsKeyDown(Key.Home);
            Debug.WriteLine("Home pressed: " + homeKeyPressed);
        }

    }
}

   
    
     


Suppress Keyboard and Mouse Events

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF " Height="100" Width="200">
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Horizontal" PreviewMouseDown="StackPanel_PreviewMouseDown">
            <Button Content="Blocked" Click="Button_Click" Height="25" Margin="10" Width="70"/>
        </StackPanel>
        <Button Content="Not Blocked" Click="Button_Click" Height="25" Margin="10" Width="70"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Button Clicked", "Button");
        }

        private void StackPanel_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }
}

   
    
     


Use KeyBinding to bind Key event to TextBox.InputBindings

image_pdfimage_print


   
  
<Window x:Class="Commands.NoCommandTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NoCommandTextBox" Height="300" Width="300">
    <Grid>
      <TextBox Name="txt"/>
    </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;

namespace Commands
{
    public partial class NoCommandTextBox : System.Windows.Window
    {
        public NoCommandTextBox()
        {
            InitializeComponent();           
        
            txt.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.C, ModifierKeys.Control));
        }
     
        private void SuppressCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
            e.Handled = true;
        }
    }
}

   
    
     


StackPanel PreviewTextInput and PreviewKeyDown

image_pdfimage_print


   
  


<Window x:Class="RoutedEvents.OnlyNumbers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Only Numbers" Height="300" Width="300">
    <StackPanel Margin="5" PreviewTextInput="pnl_PreviewTextInput" PreviewKeyDown="pnl_PreviewKeyDown">
        <TextBox Margin="3" AcceptsTab="False"></TextBox>
        <TextBox Margin="3"></TextBox>
        <TextBox Margin="3"></TextBox>
    </StackPanel>
    
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;

namespace RoutedEvents
{
    public partial class OnlyNumbers : System.Windows.Window
    {
        public OnlyNumbers()
        {
            InitializeComponent();
        }
        private void pnl_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
            {
                e.Handled = true;
            }            
        }

        private void pnl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }
    }
}

   
    
     


If input is not a number do not handle the key event

image_pdfimage_print


   
  


<Window x:Class="RoutedEvents.OnlyNumbers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Only Numbers" Height="300" Width="300">
    <StackPanel Margin="5" PreviewTextInput="pnl_PreviewTextInput" PreviewKeyDown="pnl_PreviewKeyDown">
        <TextBox Margin="3" AcceptsTab="False"></TextBox>
        <TextBox Margin="3"></TextBox>
        <TextBox Margin="3"></TextBox>
    </StackPanel>
    
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;

namespace RoutedEvents
{
    public partial class OnlyNumbers : System.Windows.Window
    {
        public OnlyNumbers()
        {
            InitializeComponent();
        }
        private void pnl_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
            {
                e.Handled = true;
            }            
        }

        private void pnl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }
    }
}

   
    
     


On Key Down Handler

image_pdfimage_print


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyDown Sample" Height="400" Width="500">
  <StackPanel>
    <TextBlock Width="300" Height="20">
      Type some text into the TextBox and press the Enter key.
    </TextBlock>
    <TextBox Width="300" Height="30" Name="textBox1"
             KeyDown="OnKeyDownHandler"/>
    <TextBlock Width="300" Height="100" Name="textBlock1"/>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace WpfApplication1
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }
        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                textBlock1.Text = "You Entered: " + textBox1.Text;
            }
        }
    }
}