New to Telerik UI for WinForms? Download free 30-day trial

Assigning Global RadShortcuts

RadItem allows you to add shortcuts which will generate a Click event for that item, allowing you to perform specific action, associated with that item. This approach however has some requirements such as you need a RadItem instance and some restrictions such as each shortcut, registered with RadItem is processed only if the item resides on the currently active form within the application. In order to create a custom shortcuts behavior, you may provide your own IShortcutProvider implementation and to handle its callback methods to provide completely customized shortcut support for your application, including “Global” shortcuts – that is a shortcut which is not bound to the currently active form. The following code snippet demonstrates how this can be done:

1. Implement a custom IShortcutProvider:


public class MyShortcutProvider : IShortcutProvider
{
    private RadShortcutCollection shortcuts;
    private bool registered;

    public MyShortcutProvider()
    {
        this.shortcuts = new RadShortcutCollection(this);
    }

    public void OnPartialShortcut(PartialShortcutEventArgs e)
    {
        //This callback is called when a key that partially matches a registered shortcut is pressed
        //For example if we have CTRL + A, S pressing CTRL + A will execute this callback.
        //You will need to set the PartialShortcutEventArgs.Handled to true if you want to wait for the complete keyboard combination
        e.Handled = true;
    }

    public void OnShortcut(ShortcutEventArgs e)
    {
        //A keyboard combination for a specific shortcut is pressed.
        MessageBox.Show("Shortcut [" + e.Shortcut.GetDisplayText() + "] is executed.");
        //Mark the event arguments as "Handled" so that this shortcut is no further processed.
        e.Handled = true;
    }

    public void OnShortcutsChanged()
    {
        //Called by the Shortcuts collection when a shortcut is either added or removed from the collection
        //This is used for optimization purposes - e.g. is we do not have shortcuts registered,
        //we do not need to be registered with RadShortcutManager
        if (this.shortcuts.Count > 0)
        {
            if (!this.registered)
            {
                RadShortcutManager.Instance.AddShortcutProvider(this);
                this.registered = true;
            }
        }
        else
        {
            if (this.registered)
            {
                RadShortcutManager.Instance.RemoveShortcutProvider(this);
                this.registered = false;
            }
        }
    }

    public RadShortcutCollection Shortcuts
    {
        get
        {
            return this.shortcuts;
        }
    }

}

2. And here is how we utilize the custom shortcut provider:

MyShortcutProvider provider;
public Form2()
{
    InitializeComponent();
    provider = new MyShortcutProvider();
    provider.Shortcuts.Add(new RadShortcut(Keys.Control, Keys.A, Keys.S));
}

The shortcut providers that implement IShortcutProvider interface are registered as WeakReferences. So, if you declare the MyShortcutProvider variable in the scope of the constructor or in another method (for example the Form.Load event) the Garbage collection may collect the shortcut provider reference at a certain moment. To prevent this from happening, you need to declare the MyShortcutProvider variable globally.