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.
Related
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");
}
I have set KeyPreview property of the form to true in order to call keyboard events of the form before control events.
Both the form and the control in the form have KeyDown event like:
form:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)) {
MessageBox.Show("Control + Enter (Form)");
}
}
control:
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (!e.Control && (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)) {
MessageBox.Show("Control + Enter (TextBox)");
}
}
As you see the difference between these two parts of code is that in the form event code I need to call the KeyDown event when the user presses CTRL and Enter keys at the same time,
In the TextBox event code, I need to call the event when the user presses Enter key without holding CTRL-key.
The problem is that when I press Ctrl and Enter keys at the same time both of the above events will call.
How to prevent call both events?
I suggest you use the textBox1_KeyUp event. You can refer to the following code. My test was successful.
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return))
{
MessageBox.Show("Control + Enter (Form)");
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Control)
{
e.Handled = true;
}
else if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Control + Enter (TextBox)");
}
}
Use the ProcessCmdKey and like this.
protected override bool ProcessCmdKey(ref Message msg, System.Windows.Forms.Keys keyData)
{
int WM_ALRT_DOWN = 0x0104;
int WM_KEYDOWN = 0x0100;
if (msg.Msg == WM_ALRT_DOWN && (int)msg.WParam == (int)Keys.F4) //Alt + F4
{
return true; // The key is manually processed
}
if (msg.Msg == WM_KEYDOWN && (int)msg.WParam == (int)Keys.Escape) //Esc
{
return true; // The key is manually processed
}
if (msg.Msg == WM_KEYDOWN && (int)msg.WParam == (int)Keys.Space) //Space
{
return true; // The key is manually processed
}
}
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 want a c# split container that ignores cursor keys and can only be controlled with the mouse. How can I do this? This is so I can use the keyboard input in one of the side panels without simultaneously moving the split.
Using e.Handled = true or e.SuppressKeyPress = true to prevent the keys from resizing the splitter didn't work for me.
I was able to do it by setting IsSplitterFixed = true on KeyDown, and IsSplitterFixed = false on MouseDown/MouseMove (to allow resizing by mouse).
e.g.
public Form1()
{
InitializeComponent();
splitContainer1.MouseMove += splitContainer1_MouseMove;
splitContainer1.KeyDown += splitContainer1_KeyDown;
splitContainer1.MouseDown += splitContainer1_MouseDown;
}
void splitContainer1_MouseDown(object sender, MouseEventArgs e)
{
splitContainer1.IsSplitterFixed = false;
}
void splitContainer1_MouseMove(object sender, MouseEventArgs e)
{
splitContainer1.IsSplitterFixed = false;
}
void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
splitContainer1.IsSplitterFixed = true;
}
You may disable the keyboard input handling the KeyDown event of the control and if required, you may handle the event if the input matches specific keys.
Example
splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown
private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
// if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
// {
e.Handled = true; //Handle the event
// }
}
Moreover, you may stop the splitter from moving by canceling the SplitterMoving event of the SplitContainer control according to the KeyCode gathered from its KeyDown event
Example
Keys KeyCode; //This is the variable we will use to store the KeyCode gathered from the KeyDown event into. Then, check if it matches any of the arrow keys under SplitterMoving event canceling the movement if the result was true
splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown
splitContainer1.SplitterMoving += new SplitterCancelEventHandler(splitContainer1_SplitterMoving); //Link the SplitterMoving event of splitContainer1 to splitContainer1_SplitterMoving
private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
if (KeyCode == Keys.Up || KeyCode == Keys.Down || KeyCode == Keys.Left || KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
{
KeyCode = Keys.A; //Reset the KeyCode
e.Cancel = true; //Cancel the splitter movement
}
}
private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
KeyCode = e.KeyCode; //Set KeyCode to the KeyCode of the event
// e.Handled = true; //Handle the event
}
Thanks,
I hope you find this helpful :)
I have a simple form by which I take input:
12 Buttons, 1 Textbox (disabled & read-only)
this is what I do to handle input
Login_KeyDown() is common method I call for all the KeyDown of every UI component & the form itself..
private void Login_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
else if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.D9)
{
button3.BackgroundImage = Properties.Resources.button_hover;
button3.ForeColor = Color.White;
pin.Text = pin.Text + "9";
}
else if (e.KeyCode == Keys.Back)
{
button11.BackgroundImage = Properties.Resources.button_hover;
button11.ForeColor = Color.White;
if (pin.Text.Length > 0)
pin.Text = pin.Text.Substring(0, pin.Text.Length - 1);
}
else if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(pin.Text);
}
}
This code works fine when I start the app but after I have clicked on any component, rest of the code works fine but "Enter Key Condition" doesn't work.
My guess is as "Enter Key Condition" is not working for UI components or something like that.
I have also tried using "Key Press Event" which uses KeyPressEventArgs then checking KeyChar == 13 but that is also not working.
What is the problem, and how can I solve it?
p.s.
I have not set any button click events for any button, the app is 100% KBoard based.
Check out PreviewKeyDown. Return raises that event on button controls.
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
MessageBox.Show("I found return");
}
Or alternatively you can force it to raise those special keys in the KeyDown Event by using:
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
e.IsInputKey = true;
}
More information: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx
Have you tried to use
Keys.Return
Instead
Edit:
Just thought of this. Do you have the acceptbutton set for the main form?
This is because your Form has AcceptButton defined. For example, you have a "OK", "Accept" or "Confirm" button with DialogResult set to "OK". This tells its parent form that there is an AcceptButton, and the Enter event on the form would go to this button.
What you should do is to catch the Enter key at form level. Add this code to the form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((this.ActiveControl == myTextBox) && (keyData == Keys.Return))
{
//do something
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}