key combinations help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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. Please help.

Regards,
 
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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Sure you got the question right... I knew about the first trick but it is not
working on VS 2005 Beta 2.

Thanks for the second one. I will try that.

Thanks again.
 
The best way to handle keys events is to override the ProcessDialogKey()
function.
You can use KeyDown , KeyUp methods but you wont be able to control the
trigring of your events.

In following code I created short/hot key for Tab-Key when tab key is
pressed and comboBox1 is Focoused
focous to perticular Control (checkbox1 in this case).If you press Ctrl+V
then data from clipboard will e copied
to TextBox(Tb_Script in the this case).

Becarefull about returning true , false and base.ProcessDialogKey(keyData).

code;

--------------------------------------------------------------------------------

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData ==System.Windows.Forms.Keys.Tab){
if(this.checkBox1.Visible &&
!(this.checkBox1.Focused) && this.comboBox1.Focused)
{
this.checkBox1.Focus();
return true;
}else{
return base.ProcessDialogKey(keyData);
}
}else if(keyData == Keys.ControlKey && keyData== Keys.V &&
Tb_Script.Focused){
// Declares an IDataObject to hold the data returned from the
clipboard.
// Retrieves the data from the clipboard.
IDataObject iData = Clipboard.GetDataObject();

// Determines whether the data is in a format you can use.
if(iData.GetDataPresent(DataFormats.Text))
{
// Yes it is, so save in a tmp string and do concatanation
Tb_Script.Text=String)iData.GetData(DataFormats.Text);

return true;
}else
return base.ProcessDialogKey(keyData);
}

return false;
}
 
Back
Top