473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

key combinations help

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,
Nov 17 '05 #1
3 10335
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/
Nov 17 '05 #2
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.

"Jani Järvinen [MVP]" wrote:
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/

Nov 17 '05 #3
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.Te xt);

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

return false;
}

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

Regards,
Omer kamal
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: drs | last post by:
Hi, I am trying to find all lists of length x with elements a, b, and c. To this end, I have created the class below, but it is not quite working and I am having trouble figuring out what to...
3
by: Ryan | last post by:
I've been trying to find an algorithm that will output all of the possible combinations of items in an array. I know how to find the number of combinations for each set using nCr=n!/(r!(n-r)!) ...
5
by: ech0 | last post by:
I would appreciate any help I can get in finding a solution to the following problem: ================================================================== Below is a diagram:...
4
by: Norman Fritag | last post by:
Hi there, >>>__ 1020.83, 2305.22, 1176.86, 755.12, 123.41 __ 1976.1, 1325.99, 947, 718.03, 414.32 __ 1020.83, 1976.1, 352.5, 947, 718.03, 366.98 Their IDs were as...
1
by: syl | last post by:
Hi I have a huge loto data and i am trying to come up with the total count of all total combination...the manual process to tiring in excel. e.g the possible combinations range from 1 up to 49. so...
4
by: Suzie1986 | last post by:
Hiya, I am a newcomer to programming and really stuck!!! Any help would be gratefully received!! I have written a program that gives me all possible combinations for 1 and 2 for a length of...
5
by: Phil | last post by:
Thank you for reading my plea for help. What I'm trying to achieve is to get the subsets from a given set using the Standard Template Library. Say, for example, I have created an empty set and...
5
by: Bails | last post by:
Hi all I have a theory for a lotto system and need help on how to code it. I want to create 1 massive database with EVERY combination of numbers possible in a given lotto system, then remove all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.