Keyboard shortcut for a button - c#

In C# (Microsoft Visual Studio 2010), how can I assign a keyboard shortcut to a button such as the following?
private void closeButton_Click(object sender, EventArgs e)
{
// Close the program
this.Close();
}
I know I can use the "&" character in the button's Text and create an Alt - n shortcut, but I'd like to create a single keypress shortcut, such as c to execute the above.

KeyDown is your friend ;) For example, if you want the shortcut key A while Shift is pressed, try this:
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A && e.Shift)
// Do something
}
If you want a "real" keyboard shortcut you can use hooks.
Look at Stack Overflow question RegisterHotKeys and global keyboard hooks?.

Here's a different approach:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch(keyData)
{
case Keys.F2:
// do something...
return true;
case Keys.F3:
// do something...
return true;
case Keys.F4:
// do something...
return true;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
}
You don't need to change KeyPreview value.

if you want add Ctrl + S in switch statment so you can also try below code.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Control | Keys.S:
// do something...
return true;
case Keys.Control |Keys.Alt | Keys.S:
// do something...
return true;
case Keys.F2:
// do something...
return true;
case Keys.F3:
// do something...
return true;
case Keys.F4:
// do something...
return true;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
}

Related

Focus changes even after setting e.Handled = true

I've set KeyPreview = true; for my Form. I basically want to use the arrow keys to go to the next and previous images instead of changing focus to different controls. I've set the Handled property to true but still the focus changes on arrow key press.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
// Do stuff
}
else if (e.KeyCode == Keys.Left)
{
// Do stuff
e.Handled = true;
}
else if (e.KeyCode == Keys.Right)
{
// Do stuff
e.Handled = true;
}
}
EDIT
The behavior I want to achieve is as follows.
Left Arrow Key -> Previous Image
Right Arrow Key -> Next Image
Now, I also have a few TextBoxes on my Form and I therefore do not want to go to next and previous images if those Textboxes are in focus because then it should navigate through the text instead.
This worked for me.
Do not set KeyPreview = true; for the Form.
Override ProcessCmdKey and process as needed if any of the TextBoxes do not have focus.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (textBox1.ContainsFocus || textBox2.ContainsFocus || textBox3.ContainsFocus)
{
return base.ProcessCmdKey(ref msg, keyData);
}
if (keyData == Keys.Delete)
{
removeRect();
return true;
}
else if (keyData == Keys.Left)
{
previousImg();
return true;
}
else if (keyData == Keys.Right)
{
nextImg();
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}

C# - Implementing Hotkeys in WinForms

I tried following code:
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Control)
{
// Show the hotkey on the buttons with ctrl hotkeys
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Control | Keys.S:
// Execute the Ctrl + S button action
btnSettingsSave_Click(this, null);
return true;
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
to save my setting in the GUI by pressing CTRL+S. Now that works fine but I wanted to put in more hot keys (for example CTRL+F). How can I do that?

Avoiding alert on key board shortcut of C# winform

I am making key board shortcuts to a Winform application in C# using Visual Studio 2012. My shortcuts work perfect. But it gives a annoying beep sound.
I added e.Handled = true; and e.SuppressKeyPress = true; according to many threads. But it does not work and my winform stuck.
How can I avoid this?
private void textBoxSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
do stuff
}
else if (e.KeyCode == Keys.Enter)
{
//do stuff
}
e.Handled = true;
e.SuppressKeyPress = true;
}
and I need a solution for this too.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.F))
{
//do stuff
}
else if (keyData == (Keys.Control | Keys.G)) {
//do stuff
}
return base.ProcessCmdKey(ref msg, keyData);
}
What you have in the KeyDown event should work. The SupressKeyPress = true stopped the ding for me when I copied your code.
In the ProcessCmdKey event you would need this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.F))
{
//do stuff
return;
}
else if (keyData == (Keys.Control | Keys.G)) {
//do stuff
return;
}
return base.ProcessCmdKey(ref msg, keyData);
}

Stop a key from firing an event in C# using ProcessCmdKey?

So I'm making a form, and I want the left and right keys to ONLY correspond to a numericUpDown box that I have on the form. So the code I wrote is the following :
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Right)
{
numericUpDown1.Value = Convert.ToDecimal(numericUpDown1.Value + 1);
}
if (keyData == Keys.Left)
{
try
{
numericUpDown1.Value = Convert.ToDecimal(numericUpDown1.Value - 1);
}
catch { }
}
return base.ProcessCmdKey(ref msg, keyData);
}
However it seems to still do the default action of moving between different objects on the form if that's what the selected view is currently. How do I stop the default action?
You need to return true when you don't want the default action to be carried out.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Right)
{
numericUpDown1.Value = Convert.ToDecimal(numericUpDown1.Value + 1);
return true;
}
if (keyData == Keys.Left)
{
try
{
numericUpDown1.Value = Convert.ToDecimal(numericUpDown1.Value - 1);
return true;
}
catch { }
}
}
Maybe you should return true to indicate you have processed the key stroke message so that no other controls get it.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Right){
numericUpDown1.Value = Convert.ToDecimal(numericUpDown1.Value + 1);
return true;
}
else if (keyData == Keys.Left){
try {
numericUpDown1.Value = Convert.ToDecimal(numericUpDown1.Value - 1);
}
catch { }
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
NOTE: It looks like that you didn't post the code you run? I highly recommend you to post the actual code of yours, your code doesn't even compile because lacking of return. And your code lacks the return base.ProcessCmdKey(ref msg, keyData); which is required to process other keys.
you can add an event hanlder and do this:
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Left)
{
e.Handled = true; //this line will do the trick
//add the rest of your code here.
}
}

How can I stop a button from stealing my keypress event?

I have a button that keeps stealing my keypress event. How do I override this? I want to have a method run when enter is clicked when the focus is on the form, but the button steals it.
Handle the KeyDown event in the Form:
private void Form1_KeyDown(Object sender, KeyEventArgs e)
{
if ((e.KeyCode & Keys.Enter) == Keys.Enter)
{
//do something
e.Handled = true;
}
else
base.OnKeyDown(e);
}
Buttons do not steal the KeyPress event, they 'only' steal the Enter key. So you can do something like this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
processYourKey( keyData);
return false; // do not steal!
}
processYourKey( keyData);
return base.ProcessCmdKey(ref msg, keyData);
}

Categories

Resources