Skip to main content Skip to footer

Customize Key Behavior in Spread

Processing keyboard behavior is common in application developing, generally we use KeyDown event, and add code in the event processor. Sometime we have to process some keys which are handled by control inside, as we know MS provide a way to monitor handled event by AddHandler method on WPF/Silverlight/WinRT platforms. Code may like this:


        public MainPage()  
        {  
            InitializeComponent();  
            this.gcSpreadSheet1.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(GcSpreadSheet_KeyDown), true);  
        }  

        private void GcSpreadSheet_KeyDown(object sender, KeyEventArgs e)  
        {  
            if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)  
            {  
                // business logic code   
            }  
        }  

However by this way, the action of our control is still performed. How to replace the action of control for some specific keys? Spread provides a flexible way to customize mapping of keys and actions, use this function you can easily replace control build-in actions by your own business logic. Code may like this:


         public MainPage()  
        {  
            InitializeComponent();  
            this.gcSpreadSheet1.View.KeyMap.Remove(new KeyStroke(Key.C, ModifierKeys.Control)); // remove build-in key mapping  
            this.gcSpreadSheet1.View.KeyMap.Add(new KeyStroke(Key.C, ModifierKeys.Control), new SpreadAction(OnMyAction)); // map the key to your own action  
        }  
        private void OnMyAction(object sender, ActionEventArgs e)  
        {  
            // business logic code   
        }  

These code can be used in Spread WPF, Spread Silverlight, and Spread WinRT. For more information of products, please visit Spread WPF-Silverlight and Spread WinRT on our web site.

MESCIUS inc.

comments powered by Disqus