after many searches on the internet, I can’t find an optimized solution. Indeed, I would just like to block keys and mouse click at some point, however I would like to avoid putting all the keys and clicking in my condition. Is this possible?
There is the code :
private void cmdVision_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pnlNote.Enabled = true;
}
}
private void cmdVision_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pnlNote.Enabled = false;
}
//MAJ4
else if (e.Button == MouseButtons.Right || e.Button == MouseButtons.Middle)
{
pnlNote.Enabled = false;
}
//MAJ4
}
As you can see my condition consists of e.Button == MouseButtons.Right || e.Button == MouseButtons.Middle.
So I would like to avoid having to do that for every key on the keyboard.
Thank you in advance ;)
Try to use PreviewKeyDown event and its PreviewKeyDownEventArgs. There are a set of helpful properties, like IsInputKey and KeyCode (you can compare pressed key with the range of key codes).
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){}
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 read that I can suppress this noise by defining a form accept button, which is something I am trying to avoid (I can point it at a hidden or inactive button I suppose, but since it's not explicitly what I'm trying to do, I'm concerned about side effects)
I use the following snippet to trap the return key and it works just fine, the noise does not occur if I click the button manually.
private void urlTextBox_KeyDown(object sender, KeyEventArgs e) {
if ( e.KeyCode == Keys.Return )
//if ( e.KeyValue.Equals(13) )
{
e.SuppressKeyPress = true;
//e.Handled = true;
goButton.PerformClick();
}
I am targetting .NET 4.0 so I should be able to implement most ideas.
Give this a shot:
private void urlTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
e.SuppressKeyPress = true;
}
}
private void urlTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
goButton.PerformClick();
}
}
Source
It may also work with the KeyDown event but I haven't tested it.
I am trying to make a minesweeper type game in visual c# and I want to have different things happen when I right click and left click a button, how do I do this?
I have tried this code but it only registers left clicks:
private void button1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MessageBox.Show("Left");
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("Right");
}
}
You will have to use the MouseUp or MouseDown event instead of the Click event to capture right click.
Just try with button1_MouseDown event instead of button1_MouseClick Event.It will solve your problem.
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//do something
}
if (e.Button == MouseButtons.Right)
{
//do something
}
}
Button is reacting only for MouseButtons.Left not for MouseButton.Right and not even for middle.
void Select(object sender, MouseEventArgs e)
{
/* var btn = sender as CardButton;*/
if (e.Button == MouseButtons.Left)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
if (e.Button == MouseButtons.Right)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
Draw();
}