Back to Devexpress

Keyboard Services

windowsforms-4107-controls-and-libraries-scheduler-services-keyboard-services.md

latest4.2 KB
Original Source

Keyboard Services

  • Jan 21, 2020
  • 2 minutes to read

The IKeyboardHandlerService in the XtraScheduler provides the capability to determine which key is pressed or released. It has the following delegates:

To handle these events you should first create a new class inherited form the KeyboardHandlerServiceWrapper, and override its methods as needed. The following code snippet illustrates how this can be done:

csharp
using DevExpress.Services;
//...
public class MyKeyboardHandlerService : KeyboardHandlerServiceWrapper  
{ 
    IServiceProvider provider;

    public MyKeyboardHandlerService(IServiceProvider provider, IKeyboardHandlerService service)
        : base(service)
    {
        this.provider = provider;            
    }

    public override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Control)
        {
            // Do something when CTRL key is pressed.
        }
        base.OnKeyDown(e);
    }
}
vb
Imports DevExpress.Services
'...
Public Class MyKeyboardHandlerService
    Inherits KeyboardHandlerServiceWrapper
    Private provider As IServiceProvider

    Public Sub New(ByVal provider As IServiceProvider, ByVal service As IKeyboardHandlerService)
        MyBase.New(service)
        Me.provider = provider
    End Sub

    Public Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        If e.Control Then
            ' Do something when CTRL key is pressed.
        End If
        MyBase.OnKeyDown(e)
    End Sub
End Class

The provider is the SchedulerControl instance that provides the service.

This class will replace the existing service that handles the keyboard events.

The following code example replaces the existing keyboard handling service of the XtraScheduler with a custom one. Use this code in the Form Load event.

csharp
using DevExpress.Services;
// ...
IKeyboardHandlerService oldKeyboardHandler = 
    (IKeyboardHandlerService)schedulerControl1.GetService(
        typeof(IKeyboardHandlerService));
if (oldKeyboardHandler != null)
{
    MyKeyboardHandlerService newKeyboardHandler = 
        new MyKeyboardHandlerService(schedulerControl1, oldKeyboardHandler);
    schedulerControl1.RemoveService(typeof(IKeyboardHandlerService));
    schedulerControl1.AddService(typeof(IKeyboardHandlerService), newKeyboardHandler);
}
vb
Imports DevExpress.Services
' ...
Private oldKeyboardHandler As IKeyboardHandlerService = _
    CType(schedulerControl1.GetService(GetType(IKeyboardHandlerService)), _
        IKeyboardHandlerService)
If Not oldKeyboardHandler Is Nothing Then
    Dim newKeyboardHandler As MyKeyboardHandlerService = _
        New MyKeyboardHandlerService(schedulerControl1, oldKeyboardHandler)
    schedulerControl1.RemoveService(GetType(IKeyboardHandlerService))
    schedulerControl1.AddService(GetType(IKeyboardHandlerService), newKeyboardHandler)
End If

Key events occur in the following order:

  1. KeyDown
  2. KeyPress
  3. KeyUp

The KeyPress event is not raised by keys which do not represent characters; however, the noncharacter keys do raise the KeyDown and KeyUp events.

To handle keyboard events only within a current form, and disable other controls from receiving keyboard events, set the e.Handled property in event-handling method to true.