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?
Related
The arrow keys should scroll only pictureBox (placed in panel). It works fine.
But it also scroll through comboBox items though it is closed (droppedUp).
How to disable it?
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.DroppedDown = false;
comboBox1.Items.Add("111");
comboBox1.Items.Add("222");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down)
{
Point current = panel1.AutoScrollPosition;
Point scrolled = new Point(current.X, -current.Y + 50);
panel1.AutoScrollPosition = scrolled;
}
return base.ProcessCmdKey(ref msg, keyData);
}
I had read about Control.PreviewKeyDown event:
preview key event
and also found another example:
preview key event
but I cannot understand how it used in my case.
As Hans Passant commented, you must return true. Also, add some other keys that maybe change the selected item in the combobox:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down ||
keyData == Keys.Up ||
keyData == Keys.PageDown ||
keyData == Keys.PageUp)
{
Point current = panel1.AutoScrollPosition;
Point scrolled = new Point(current.X, -current.Y + 50);
panel1.AutoScrollPosition = scrolled;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
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);
}
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);
}
I have a simple increment on textbox by pressing down arrow key which are as below.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down)
{
int c = int.Parse(textBox1.Text);
c++;
textBox1.Text = c.ToString();
}
}
The above works on pressing double down arrow key instead of single pressing down arrow key.
Note: The above code is on UserControl. And I have tried it on simple winform application on form keydown EventHandller and the same is works fine.
How to overcome?.
You'll need to handle other commands that existed before and return when you handle ones you are looking for. Try changing it to this and see if that helps:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (msg.WParam.ToInt32() == (int)Keys.Down)
{
int c = int.Parse(textBox1.Text);
c++;
textBox1.Text = c.ToString();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
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);
}
}