Keyboard Events Example for WPF

Nevron NOV UserInterface for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF UserInterface examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Keyboard Events Example for WPF Description

Demonstrates the keyboard support in NOV. Click on the canvas and start pressing the keyboard keys to see the keyboard events that NOV sends to the application. Keyboard support is available to most desktop enviroments.


Keyboard Events Example

C# Keyboard Events Example Code for Nevron UserInterface for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Dom;
using Nevron.Nov.Editors;
using Nevron.Nov.Globalization;
using Nevron.Nov.Graphics;
using Nevron.Nov.IO;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.IO;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NKeyboardEventsExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

        #region Example

        public NWidget CreateExampleContent()
        {
            m_Canvas = new NCanvas();
            m_Canvas.PrePaint += OnCanvasPrePaint;

            // subscribe for keyboard events
            m_Canvas.KeyDown += OnCanvasKeyDown;
            m_Canvas.KeyUp += OnCanvasKeyUp;
            m_Canvas.InputChar += OnCanvasInputChar;
            m_Canvas.GotFocus += OnCanvasGotFocus;
            m_Canvas.LostFocus += OnCanvasLostFocus;

            // subscribe for mouse events
            m_Canvas.MouseDown += OnCanvasMouseDown;

            return m_Canvas;
        }

        public NWidget CreateExampleControls()
        {
            NStackPanel stack = new NStackPanel();
            stack.FitMode = ENStackFitMode.Last;
            stack.FillMode = ENStackFillMode.Last;

            // handle key up
            m_HandleKeyDown = new NCheckBox("Handle Key Down");
            stack.Add(m_HandleKeyDown);

            // handle key up
            m_HandleKeyUp = new NCheckBox("Handle Key Up");
            stack.Add(m_HandleKeyUp);

            // handle key up
            m_HandleInputChar = new NCheckBox("Handle Input Char");
            stack.Add(m_HandleInputChar);

            // Create the events log
            m_EventsLog = new NExampleEventsLog();
            stack.Add(m_EventsLog);

            return stack;
        }
        public string GetExampleDescription()
        {
            return @"

Demonstrates the keyboard support in NOV. Click on the canvas and start pressing the keyboard keys to see the keyboard events that NOV sends to the application. Keyboard support is available to most desktop enviroments.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Keyboard Input", "https://www.nevron.com/docs/openvision/ui-keyboard-input.html"), ("User Input", "https://www.nevron.com/docs/openvision/ui-user-input.html"), }; } #endregion #region Implementation #endregion #region Event Handlers #endregion #region Fields NCheckBox m_HandleKeyDown; NCheckBox m_HandleKeyUp; NCheckBox m_HandleInputChar; NCanvas m_Canvas; NExampleEventsLog m_EventsLog; string m_sInputString = ""; #endregion #region Static Methods #endregion #region Constants private const string EnglishLanguageName = "English (US)"; private const string BulgarianLanguageName = "Bulgarian"; private const string GermanLanguageName = "German"; #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Keyboard Events Example for WPF Solution

VB.NET Keyboard Events Example Code for Nevron UserInterface for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.DataStructures
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Editors
Imports Nevron.Nov.Globalization
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.IO
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System
Imports System.IO

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NKeyboardEventsExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As Nevron.Nov.UI.NWidget
            Me.m_Canvas = New Nevron.Nov.UI.NCanvas()
            Me.m_Canvas.PrePaint += AddressOf OnCanvasPrePaint

            ' subscribe for keyboard events
            Me.m_Canvas.KeyDown += AddressOf OnCanvasKeyDown
            Me.m_Canvas.KeyUp += AddressOf OnCanvasKeyUp
            Me.m_Canvas.InputChar += AddressOf OnCanvasInputChar
            Me.m_Canvas.GotFocus += AddressOf OnCanvasGotFocus
            Me.m_Canvas.LostFocus += AddressOf OnCanvasLostFocus

            ' subscribe for mouse events
            Me.m_Canvas.MouseDown += AddressOf OnCanvasMouseDown

            Return Me.m_Canvas
        End Function

        Public Function CreateExampleControls() As Nevron.Nov.UI.NWidget
            Dim stack As Nevron.Nov.UI.NStackPanel = New Nevron.Nov.UI.NStackPanel()
            stack.FitMode = Nevron.Nov.Layout.ENStackFitMode.Last
            stack.FillMode = Nevron.Nov.Layout.ENStackFillMode.Last

            ' handle key up
            Me.m_HandleKeyDown = New Nevron.Nov.UI.NCheckBox("Handle Key Down")
            stack.Add(Me.m_HandleKeyDown)

            ' handle key up
            Me.m_HandleKeyUp = New Nevron.Nov.UI.NCheckBox("Handle Key Up")
            stack.Add(Me.m_HandleKeyUp)

            ' handle key up
            Me.m_HandleInputChar = New Nevron.Nov.UI.NCheckBox("Handle Input Char")
            stack.Add(Me.m_HandleInputChar)

            ' Create the events log
            Me.m_EventsLog = New NExampleEventsLog()
            stack.Add(Me.m_EventsLog)

            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

Demonstrates the keyboard support in NOV. Click on the canvas and start pressing the keyboard keys to see the keyboard events that NOV sends to the application. Keyboard support is available to most desktop enviroments.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Keyboard Input", "https://www.nevron.com/docs/openvision/ui-keyboard-input.html"), ("User Input", "https://www.nevron.com/docs/openvision/ui-user-input.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_HandleKeyDown As Nevron.Nov.UI.NCheckBox Private m_HandleKeyUp As Nevron.Nov.UI.NCheckBox Private m_HandleInputChar As Nevron.Nov.UI.NCheckBox Private m_Canvas As Nevron.Nov.UI.NCanvas Private m_EventsLog As NExampleEventsLog Private m_sInputString As String = "" #End Region #Region "Static Methods" #End Region #Region "Constants" Private Const EnglishLanguageName As String = "English (US)" Private Const BulgarianLanguageName As String = "Bulgarian" Private Const GermanLanguageName As String = "German" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Keyboard Events Example for WPF Solution

Related Documentation Links