I want to handle the ctrl + c keys in keydown event but it is not working. I am trying this code but not working. when I print e.keycode, I see it as "Controlkey" but I am pressing Ctrl + C.
I tried for ALT + A. It is working and e.keycode is coming as "A" key. And I tried to code in this link: Link is here. But didn't work again.
My code (if key is Ctrl+ C, e.keycode = Controlkey ):
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
{
Console.WriteLine("work please");
}
}
I tried this code for another project, and it works but now I am writing again and it doesn't. How can solve it?
Edit: It is working for this code (if key is Alt + C, e.keycode = A ) :
if (Control.ModifierKeys == Keys.Alt && e.KeyCode == Keys.C)
{
Console.WriteLine("work please");
}
Related
Ok guys so I've been working on this control for close to a month and one of the issues that I'm having is that if I press the CTRL key by it's self it registers and if I press the Space key by it's self it registers. I've tried to separate the two and I've tried to use them in the same if statement. Both are unsuccessful.
My first attempt was like this
protected override void OnKeyDown(KeyEventArgs e)
{
// base.OnKeyDown(e);
if (_isEditing)
{
if (e.KeyData == Keys.Delete)
{
if (_selectedObj != null)
{
DeleteSelectedObject();
}
}
}
if (e.Control && e.KeyData == Keys.Space)
{
_isEditing = !_isEditing;
Invalidate();
}
}
Now if I remove the Ctrl or the 'Space' key from the equation it works fine. So I tried to separate them and came up with
protected override void OnKeyDown(KeyEventArgs e)
{
// base.OnKeyDown(e);
if (_isEditing)
{
if (e.KeyData == Keys.Delete)
{
if (_selectedObj != null)
{
DeleteSelectedObject();
}
}
}
if (e.Control)
{
Console.WriteLine(DateTime.Now.ToShortTimeString());
if (e.KeyData.Equals(Keys.Space))
{
_isEditing = !_isEditing;
Console.WriteLine(DateTime.Now.Ticks.ToString());
}
Invalidate();
}
}
using the Console.WriteLine() as a cheater to tell me when the key is pressed and the Ticks doesn't get displayed unless I Comment out the CTRL clause. Where am I going wrong here?
Try something like
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Space)
{
}
You won't get modifier in a KeyDown event. Rather try one of the following ways to know if modifiers (Ctrl, Shift, Alt) are pressed or not:
Keyboard.IsKeyDown
if ((Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
&& e.KeyData == Keys.Space){}
Check Keyboard.Modifiers
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
&& e.KeyData == Keys.Space){}
I need to capture when the user presses CTRL-Z (press CTRL and Z at the same time) in a RichTextBox control.
I've turned off the ShortCutsEnabled property. I've tried every combination I can think of using KeyCode and KeyData with the KeyDown and KeyPress events.
I can capture EITHER a CTRL OR a Z, but never both together. Is RichTextBox capturing this keystroke before I can see it, even if shortcuts are disabled?
Does anyone have a solution that works for this?
you could simply use CTRL-Z
textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Z && (e.Control)) {
MessageBox.Show("Ctrl + Z Pressed!");
}
}
Check KeyCode and Modifiers in the KeyDown event:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Z)
MessageBox.Show("Ctrl-Z Pressed");
}
void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.Z)
{
MessageBox.Show("Ctrl + Z is Pressed");
}
}
try this.
In C#, I am trying to see if the user presses the right key so that a player moves right, but when I try it, it does not register the keypress:
private void KeyPressed(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == Convert.ToChar(Keys.Right))
{
MessageBox.Show("Right Key");
}
}
It does not display the MessageBox
This doesn't work for Left/Up/Down either
However, if I replace it with
if(e.KeyChar == Convert.ToChar(Keys.Space))
It works.
Am I doing something wrong?
You should use KeyDown event, e.g. for arrows:
private void KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
}
}
Arrow keys are not characters, so KeyPressed is not used for them.
Arrow Keys are in keyUp event they are:
Keys.Up, Keys.Down, Keys.Left, Keys.right
They are not triggered by KeyPressEventArgs.
i thought i solved this problem by myself but it came back to haunt my application so here it goes:
i have the following keydown event handler registered in a form with a couple of disabled and readonly textboxes and they are only simple shortcuts for the buttons:
private void AccountViewForm_KeyDown(object sender, KeyEventArgs e)
{
//e.SuppressKeyPress = true;
//e.Handled = true;
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.E && !isInEditMode)
btnEditMode_Click(sender, e);
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.S && isInEditMode) btnEditMode_Click(sender, e);
if (e.KeyCode == Keys.Escape) btnCancel_Click(sender, e);
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.W) Close();
}
the form has KeyPreview set to true but whenever a readonly textbox has focus and i press Ctrl + E i can't get "Control.ModifierKeys == Keys.Control" and "e.KeyCode == Keys.E" to be both true at the same time. What is really strange is that Ctrl + W works. Anyone has any idea what the hell is going on? :(
According to this question and this one, It looks like a more general way to handle keyboard shortcuts is to override the ProcessCmdKey() method:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.F)) {
MessageBox.Show("What the Ctrl+F?");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Have you considered using Alt + E and Alt + S and just setting the mnemonic property for your buttons? That seems to work well for me, and it's easier to set up.
I had the same problem
In my application the shortcuts did not work if the form was opened with an invoked show(). They did work if the form was opened with ShowDialog(). I also found that the keydown event for the textbox was not fired by CTRL+C etc but strangely was fired by CTRL+B.
The workaround fix involved using the keyup event instead of the keydown.
Here's my code :
public void ShortCut(object sender, KeyEventArgs e, TextBox box )
{
string s, tmp1, tmp2;
int selectionIndex;
switch (e.KeyCode)
{
case Keys.V: // paste
if (Clipboard.ContainsText())
{
s = Clipboard.GetText(TextDataFormat.Text);
selectionIndex = box.SelectionStart;
tmp1 = box.Text.Substring(0, selectionIndex);
tmp2 = box.Text.Substring(selectionIndex + box.SelectionLength);
box.Text = tmp1 + s + tmp2;
}
break;
case Keys.C: // copy
if (box.SelectionLength > 0)
{
selectionIndex = box.SelectionStart;
s = box.Text.Substring(selectionIndex, box.SelectionLength);
Clipboard.SetText(s);
}
break;
case Keys.X: // cut
if (box.SelectionLength > 0)
{
selectionIndex = box.SelectionStart;
s = box.Text.Substring(selectionIndex, box.SelectionLength);
Clipboard.SetText(s);
tmp1 = box.Text.Substring(0, selectionIndex);
tmp2 = box.Text.Substring(selectionIndex + box.SelectionLength);
box.Text = tmp1 + tmp2;
}
break;
case Keys.A: // all
box.SelectAll();
break;
}
}
//and here's an example calling it :
private void textBoxExpression_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
m_Host.ShortCut(sender, e, textBoxExpression);
}
else
{
....
}
}
I have this code for window form application and I have been attempting to convert it to a Silverlight application but it does not work!. There is a Textbox and I attached KeyDown event handler to it. when the user press the arrow key ( left or right) while the focus on the textbox, it will write . or -. When it is window form i used e.KeyCode and Keys.Right and its works great but when it is silverlight I used e.Key and key.Right and the program doesn't work good because the arrows do the 2 functions moving and write ./-. How I can work this out in Silverlight?
(My English not good)
The code ( window form):
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (sender is TextBox)
{
TextBox textBox = (TextBox)sender;
if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
e.Handled = true;
char insert;
if (e.KeyCode == Keys.Left)
{ insert = '.'; }
else
{ insert = '-'; }
int i = textBox.SelectionStart;
textBox.Text = textBox.Text.Insert(i, insert.ToString());
textBox.Select(i + 1, 0);
}
}
}
(and Silverlight):
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (sender is TextBox)
{
TextBox textBox = (TextBox)sender;
if (e.Key == Key.Left || e.Key == Key.Right)
{
e.Handled = true;
char insert;
if (e.Key == Key.Left)
{ insert = '.'; }
else
{ insert = '-'; }
int i = textBox.SelectionStart;
textBox.Text = textBox.Text.Insert(i, insert.ToString());
textBox.Select(i + 1, 0);
}
}
}
I don't understand, is there huge different effect between using Keycode/Keys and Key/Key or because something else?
The KeyDown event will not proportionate for several keys in TextBox, as TextBox uses those keys internally and marks those as e.Handled before they get to custom user code.
Here is the MSDN quote that explains the issue further:
Another example is TextBox. Some keys
such as the ARROW keys are not
considered text by TextBox and are
instead considered specific to the
control UI behavior, and the TextBox
marks these event cases as handled.
If I were you, I'd just use the KeyUp event as your custom code does appear to work fine in that event.
Sincerely,
-- Justin Angel
You have to set
e.Handled = true;
so the event won't be consumed further down the route.