Rahul,
I want to have certian key combinations which are hidden from user but if
required i can use them to add some additional functionality to the
program.
I want to do this in C# 2.0 and i am using VS .Net 2005 Beta 2.
I'm not exactly sure if I understood your question correctly, but if you
want to have certain key combinations to work on your form but don't want to
show to the user that they exists, there are at least two easy ways to do
it.
Firstly, you can associate a ContextMenu component with your form. Then, add
the items to the menu corresponding the commands you want to make available.
Next, assign the proper shortcut key to the menu command using the Shortcut
property of each item. Finally, write your code to the Click event handler
of the said menu command.
Normally, the context menu will be shown on the screen when the user clicks
the right mouse button or presses the menu key on the keyboard. However, the
trick is to set the menu item's Visible property to false. This way, the
menu command won't be shown to the user, but the keyboard shortcut still
works. This is probably the easiest way to have "hidden" commands.
The second option is to write a KeyDown event handler to the form. For this
to work properly, also remember to set the KeyPreview property of the form
to true. Then, you could use code similar to this to check for special keys
(here, Ctrl+F12):
-------------------------------------
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
{
if ((e.Control) && (!e.Alt) && (!e.Shift))
{
// only CTRL key was down
if (e.KeyCode == Keys.F12)
{
// CTRL+F12 was pressed
MessageBox.Show("From KeyDown event: Ctrl+F12 pressed!");
}
}
}
-------------------------------------
Hope this helps.
--
Regards,
Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi http://www.saunalahti.fi/janij/