Detect combined keys pressed - c#

I want to detect if the user clicked few keys together (example: ctrl+c , alt +F4 etc..)
How can I do it??
I googled it but I got confused... :/
I tried this:
if (Control.ModifierKeys == (Keys.C) &&Control.ModifierKeys == Keys.Control)
{
MessageBox.Show("Test");
}
But it didn't worked,any ideas?

I just understood what I was should to do:
I was needed to type this:
void detectCopy(object sender, KeyEventArgs e)
{
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
{
//work here
}
}
and this on the constructor:
public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(detectCopy);
}

Your if statement is incorrent use this:
if (e.KeyCode == Keys.C && Control.ModifierKeys == Keys.Control)
{
}

On the constructor do this:
public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(detectCopy);
KeyPreview = true;
}
And in the code do this:
void detectCopy(object sender, KeyEventArgs e)
{
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
{
//work here
}
}
In Windows Form, the Propertie KeyPreview need be true to KeyDown Event works!!!

Related

how to detect two hot key in win form app like CTRL +C , CTRL+ W

my question is how to detect two hot keys in win form application
CTRL +C,CTRL,K like visual studio commenting command
I need to simulate VS hot key For commenting a line of code
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
{
}
}
Simple way is... There is a Windows API Function called ProcessCmdKey, by overriding this function we can achieve what we want
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.C)) {
MessageBox.Show("You have pressed the shortcut Ctrl+C");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Microsoft Documentation can be found here
source
private bool _isFirstKeyPressedW = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control & e.KeyCode == Keys.W)
{
_isFirstKeyPressedW = true;
}
if (_isFirstKeyPressedW)
{
if (e.Control & e.KeyCode == Keys.S)
{
//write your code
}
else
{
_isFirstKeyPressedW = e.KeyCode == Keys.W;
}
}
}
if you want to handle Ctrl + C followed by Ctrl+ K you need to maintain a state variable.
Set the Form KeyPreview property to true and handle the Form KeyDown event.
Try This:
this.KeyPreview=true;
private bool isFirstKeyPressed= false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
isFirstKeyPressed = true;
}
if (isFirstKeyPressed)
{
if (e.Control && e.KeyCode == Keys.K)
{
MessageBox.Show("Ctrl+C and Ctrl+K pressed Sequentially");
/*write your code here*/
isFirstKeyPressed= false;
}
}
}

Detect navigation key press in datagridview

I tried with keypress event of datagridview, but it was not working. How can I detect the up-down-left-right arrow key presses in datagridview?
Try with KeyDown event:
private void dgv1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
//do stuff
}
}
It is possible with PreviewKeyDown:
public Form1()
{
InitializeComponent();
dgv.Columns.Add(new DataGridViewTextBoxColumn());
dgv.Rows.Add("text");
dgv.PreviewKeyDown += (sender, args) =>
{
Debug.Print(args.KeyCode.ToString());
};
}

How to Refactor Common These Shortcut Keys

During the initial development of my .NET application (using WinForms), I had to go in and create common editing shortcuts such as CTRL-A, CTRL-C, and CTRL-V because they are not enabled by default. Now that my application has grown to quite a few text boxes, I am trying to figure out how to refactor the following code. Can someone help please?
private void textBox1_results_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
textBox1_results.SelectAll();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.C)
{
textBox1_results.Copy();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.V)
{
textBox1_results.Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
If you have a BaseForm that all your forms inherit from try moving the above code to it and call it from all the text box event handlers.
public partial class Form2 : Form1
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
base.TextKeyDown(sender, e);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void TextKeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
((TextBox) sender).SelectAll();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.C)
{
((TextBox)sender).Copy();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.V)
{
((TextBox)sender).Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
}
Well, there are two main approaches to this.
Create your own text box control by inheriting from System.Windows.Forms.TextBox and adding your own implementation of the KeyDown (still calling base.KeyDown()) with the short cuts enabled
Create a helper class that you call in each form's Load even that iterates through all controls on the form and adds a handler to your code.
Option 1 would be something like;
public class ShortcutTextBox : TextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
SelectAll();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.C)
{
Copy();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.V)
{
Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
base.OnKeyDown(e);
}
}
Though this comes with the caveat that you'd need to replace every instance of TextBox with ShortcutTextBox.
Option two still involves refactoring but it's a once per form option. Create a helper class like the following;
public abstract class ControlUtilities
{
public static void AddTextBoxShortcuts(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
if (c is TextBox)
{
TextBox txt = (TextBox)c;
txt.KeyDown += textBox_KeyDown;
}
else if (c.Controls != null && c.Controls.Count > 0)
{
// recursively look for text boxes
AddTextBoxShortcuts(c.Controls);
}
}
}
private static void textBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox txt = (TextBox)sender;
if (e.Control && e.KeyCode == Keys.A)
{
txt.SelectAll();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.C)
{
txt.Copy();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.V)
{
txt.Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
}
and call it in your form's Load event like;
private void Form1_Load(object sender, EventArgs e)
{
ControlUtilities.AddTextBoxShortcuts(this.Controls);
}
Probably something like this:
private static void Shortcut_KeyDown(object sender, KeyEventArgs e)
{
var textBox = (TextBox)sender;
if (e.Control && e.KeyCode == Keys.A)
{
textBox.SelectAll();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.C)
{
textBox.Copy();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.V)
{
textBox.Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
And apply it:
textBox1.KeyDown += Shortcut_KeyDown;
textBox2.KeyDown += Shortcut_KeyDown;
// etc...
Although I'm not sure why you would need to write this at all - this behavior is standard to Winforms TextBox as far as I know.

Register Modifier keys for a CustomControl

I am working on a CustomControl and I want to register ModifierKeys in this control. I have already set the KeyPerview to True in the Form which this control is being added to.
Now I have a Boolean named _ctrl and I want this boolean to be true when the Control key is being held down and it should be false when Control key is being released.
I tried to achive this with the conde belowe in my CustomControl but no success!
private bool _ctrl = false;
private void MyCustomControl_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Control)
{
_ctrl = true;
}
}
private void MyCustomControl_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Control)
{
_ctrl = false;
}
}
Any tips/soloution will be appriciated!
UPDATE
Ok, I decided to do the key down and up event in the form itself:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control)
{
projectBrowser.ControlKeyIsDown = true; //bool in MyCustomControl
MessageBox.Show("CTRL is PRESSED");
}
}
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control)
{
projectBrowser.ControlKeyIsDown = false; //bool in MyCustomControl
MessageBox.Show("CTRL is DEPRESSED");
}
}
Now, The KeyDown event detects the control key and messagebox is being shown. But the KeyUp event does not work and it doesn't show the messagebox. What could be wrong?
It seems that the key up gets detected if I change the KeyUpevent like this:
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.Control)
{
projectBrowser.ControlKeyIsDown = false;
e.Handled = true;
}
}
You can try calling the Control.ModifierKeys property:
protected override void OnKeyDown(KeyEventArgs e) {
if (Control.ModifierKeys == Keys.Control) {
MessageBox.Show("I am pressing control.");
}
base.OnKeyDown(e);
}
If you throw up a MessageBox on the KeyDown event, the KeyUp event won't get called.

How to disable navigation on WinForm with arrows in C#?

I need to disable changing focus with arrows on form. Is there an easy way how to do it?
Thank you
Something along the lines of:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control control in this.Controls)
{
control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
}
}
void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
e.IsInputKey = true;
}
}
I've ended up with the code below which set the feature to EVERY control on form:
(The code is based on the one from andynormancx)
private void Form1_Load(object sender, EventArgs e)
{
SetFeatureToAllControls(this.Controls);
}
private void SetFeatureToAllControls(Control.ControlCollection cc)
{
if (cc != null)
{
foreach (Control control in cc)
{
control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
SetFeatureToAllControls(control.Controls);
}
}
}
void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
e.IsInputKey = true;
}
}
I tried this aproach, where the form handles the preview event once. It generates less code than the other options.
Just add this method to the PreviewKeyDown event of your form, and set the KeyPreview property to true.
private void form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
e.IsInputKey = true;
break;
default:
break;
}
}
You should set KeyPreview to true on the form. Handle the KeyDown/KeyUp/KeyPress event and set the e.Handled in the eventhandler to true for the keys you want to be ignored.

Categories