Keyboard Services
- 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:
- IKeyboardHandlerService.OnKeyDown is called when a key is pressed. KeyDown events repeats if the key is held down.
- IKeyboardHandlerService.OnKeyUp is called when a key is released.
- IKeyboardHandlerService.OnKeyPress is called after the key is pressed.
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:
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);
}
}
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.
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);
}
Key events occur in the following order:
- KeyDown
- KeyPress
- 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.