jump to navigation

Capture All Keystrokes aka Accelerators aka Shortcuts June 14, 2007

Posted by codinglifestyle in C#, Winform.
Tags: , , , ,
add a comment

I have a custom ListView control which supports editing items. The WinForm application has lots of accelerators (shortcuts) such as DEL to delete an item as well as CTRL-C, CTRL-X, and CTRL-P for copy and paste operations. The problem I encountered was while editing the TextBox passed these keystrokes on to the application. This meant that copy and paste didn’t work within the TextBox because they were handled by the application. Worse, when using the delete key in the TextBox the user was prompted if they wanted to delete the item (again, an application shortcut).


The solution was simple, consume those keystrokes. Many examples test for WM_KEYDOWN and consume some messages and pass on others. I simply wanted all keystrokes to go to my TextBox while it was active.


    public class ListViewTextBox : TextBox


        //While editing, we want to capture all keystrokes

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)


            return false;