Click here to Skip to main content
15,886,689 members
Articles / Desktop Programming / WPF

Full Screen WPF Application with a Low-Level Keyboard Hook

Rate me:
Please Sign up or sign in to vote.
4.75/5 (29 votes)
26 Nov 2008CPOL7 min read 121.2K   3.1K   48  
This application displays shapes of random sizes and colors. It's perfect for babies and toddlers who love to sit and press away at the keyboard.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ShapeShow.Library;

namespace ShapeShow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Private Members

        private const Int32 MaxShapeCount = 25;

        private bool IsRequestingOptions
        {
            get
            {
                return (Keyboard.Modifiers == (ModifierKeys.Alt | ModifierKeys.Control) && Keyboard.IsKeyDown(Key.O));
            }
        }

        #endregion

        public MainWindow()
        {
            InitializeComponent();

            // Route all events in the options control to the main window.
            Options.ButtonPanel.AddHandler(Button.ClickEvent, new RoutedEventHandler(OptionsButton_Click));
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (IsRequestingOptions)
            {
                ShowOptions();
            }
            else if (CanDraw(e))
            {
                DrawShape(ShapeFactory.CreateShape());
            }

            base.OnKeyDown(e);
        }

        private void DrawShape(IShape shape)
        {
            if (DrawingSurface.Children.Count == MaxShapeCount)
            {
                DrawingSurface.Children.RemoveAt(0);
            }

            DrawingSurface.Children.Add(shape.UIElement);
        }

        private void ShowOptions()
        {
            DrawingSurface.Opacity = 0.2;
            Options.Visibility = Visibility.Visible;
        }

        private void HideOptions()
        {
            DrawingSurface.Opacity = 1.0;
            Options.Visibility = Visibility.Collapsed;
        }

        private void CloseApplication()
        {
            Application.Current.Shutdown();
        }

        private bool CanDraw(KeyEventArgs e)
        {
            return (!Options.IsVisible && e.Key != Key.System &&
                e.Key != Key.LeftAlt && e.Key != Key.RightAlt &&
                e.Key != Key.LeftCtrl && e.Key != Key.RightCtrl);
        }

        private void OptionsButton_Click(object sender, RoutedEventArgs e)
        {
            if (!e.Handled)
            {
                FrameworkElement feSource = e.Source as FrameworkElement;

                switch (feSource.Name)
                {
                    case "CloseButton":
                        CloseApplication();
                        break;

                    case "ReturnButton":
                        HideOptions();
                        break;

                    case "ClearScreen":
                        DrawingSurface.Children.Clear();
                        break;

                    default:
                        return;
                }

                e.Handled = true;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
United States United States
Barry Dorman is a software engineer from the Birmingham, AL area. His primary focus is ASP.NET. Barry graduated with a BS in Electrical Engineering from The University of Alabama at Birmingham in 2006. Since then, he has kept himself busy working in the healthcare industry.

Comments and Discussions