Handling multiple keys inside a switch block - c#

I am developing a Windows Forms Application where I am trying to Hide a panel on whenever a user presses the combination of F12 and ctrl key but I am getting the error Operator '&&' cannot be applied to operands of type 'Keys' and 'Keys' . Thanks for your time.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//method to assign keys
switch (e.KeyCode)
{
case Keys.Down:
SendKeys.Send("{Tab}");
e.Handled = true;
break;
case (Keys.Control && Keys.F12): **// error here**
this.panel3.Hide();
default:
break;
}
}

If you want to Hide() on Ctrl + F12 combination, you should check e.Modifiers:
...
case (Keys.F12): // On F12
if (e.Modifiers == Keys.Control) { // On Ctrl + F12
this.panel3.Hide();
}
...

Related

Cannot handle Ctrl + S in RichTextBox

I'm coding a text editor for part of my winforms application. It has the usual Bold, Underline, Strikeout, Italic toolbar buttons. For reasons of accessibility and workflow efficiency I want to add shortcuts as well, however.
Here's the relevant code:
private void TextInput_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control)
{
switch(e.KeyCode)
{
case Keys.B: ChangeFontStyle('B');
break;
case Keys.I: e.SuppressKeypress = true;
ChangeFontStyle('I');
break;
// ... the others (U and S) look like the one for B, with the matching letters...
}
}
}
}
private voide ChangeFontStyle(char targetStyle)
{
switch(targetStyle)
{
case 'B':
if(TextInput.Selectionfont.Bold)
{
TextInput.SelectionFont = new Font(TextInput.Selectionfont, TextInput.Selectionfont.Style ^ FontStyle.Bold);
}
else
{
TextInput.SelectionFont = new Font(TextInput.Selectionfont, TextInput.SelectionFont.Style | FontStyle.Bold);
}
}
}
The others look like this, too, just with italic, underline and strikeout respectively. For three of them itorks (though if I don't " e.SuppressKeyPress on ctrl-I, an indent gets set on top of font turning italic). Just strikeout doesn't work with ctrl-S. With ctrl-shift-S it works, and the case 'S' block never executes, so ctrl-S must somehow get caught somewhere and used for something else. But I definitely don't use it elsewhere. Suggestions?
When you have a MenuStrip on the form including a menu item with Ctrl + S as ShortcutKey, then Ctrl + S will be consumed by menu item and your control will not receive the key combination.
KeyDown event of RichTextBox is too late for handling shortcut keys and MenuStrip or parent controls may consume the key combination in their ProcessCmdKey.
To handle shortcut keys for RichTextBox, use one of the following options:
Have a MenuStrip including a ToolStripMenuItem assigned with the shortcut key to its ShortcutKeys propert, then handling Click event of the menu item:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Handled by Save Menu");
}
Override ProcessCmdKey method of the Form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.S))
{
MessageBox.Show("Handled by Form");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
The last option is using PreviewKeyDown event of the RichTextBox:
private void richTextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyData == (Keys.Control | Keys.S))
{
e.IsInputKey = true;
BeginInvoke(new Action(() => {
MessageBox.Show("Handled by RichTextBox");
}));
}
}

How to detect when arrow key down is pressed / C# WPF

I'm working with WPF C# app, and I need to implement some action when arrow key down on keyboard is pressed, for example:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
// Here I gotta check is that arrow key down which invoked this event.. then call a method
DoSomething();
}
I simply can not figure out in wpf how to detect a arrow key down ..
Any kind of help would be great!
Thanks !
The KeyEventArgs hold information about the pressed key in the KeyEventArgs.Key property and so you can check for the down arrow key by checking if e.Key is equal to Key.Down, which is the enumeration value for the arrow down key.
private void Window_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down) // The Arrow-Down key
{
DoSomething();
}
}
switch (e.Key)
{
case Key.Up:
break;
case Key.Down:
break;
case Key.Left:
break;
case Key.Right:
break;
default:
break;
}

C# Detect when key gets pressed and trigger a button

So I'm trying to make a simple calculator. The user can only input the numbers by the buttons on the form or by the numpad. This is the code I have:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string key = "";
switch (e.KeyCode)
{
case (Keys.NumPad1):
key = "1";
break;
case (Keys.NumPad2):
key = "2";
break;
default:
break;
}
txt_string.Text = txt_string.Text + key;
}
If I make a breakpoint on the KeyDown function and press the Numpad keys (and every other keys) the program doesnt even comes to that breakpoint.
Do I have to change something on my Form to detect the Keys?
You'll need to set KeyPreview to true (property on the form). Also, I would advise against trying to debug the behaviour - because you may affect the behaviour you're testing (Debug.WriteLine()) is your friend here.
Just to point out that many keyboard doesnt have numpad. You can check if the key is a integer.
void Form1_KeyDown(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
txt_string.Text += e.KeyChar;
}
}
This is more a Code Review than a solution though.

control key plus S to save

I'm trying to implement a shortcut for Ctrl+S to save in my project.
How can I acomplish this?
Here's my code:
case Keys.ControlKey:
// if control + S then save
Thanks!
I found the answer:
Control is a modifier so I need to go by the letter "G" (or whatever letter I assign to the save function) and then check whether a modifier was selected or not. Here's the code:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.G:
if (e.Modifiers == Keys.Control)
{
//Do something
}
break;
}
}

show button being pressed when keyboard key is pressed

well i'm new to c# and i'm using Visual studio 2012.
i'm trying to make a checkbox with the appearance of a button.
when a keyboard key is pressed i would like for it to show the same way when the mouse clicks a button. If i hit the A key the button/checkbox is pressed down and if A key is hit again the button/checkbox is raised up.
i got this to work with just the button1 but i can't get it to show the pressing of the button by using this code
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on button1
ShowPictureButton.PerformClick();
break;
default:
break;
}
i figured i can use a checkbox so it will stay down when pressed.
If you are saying that you are using a Checkbox with it's Appearance Property set to Button you could do something like this
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on CheckBox's
ShowPictureButton.Checked = !ShowPictureButton.Checked;
break;
default:
break;
}
first set KeyPreview in your form properties to true
add events in your form (keypress and mouseclick) and then write your code, like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
checkBox2.Checked = !checkBox2.Checked;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Visible = !checkBox1.Visible;
}

Categories