I have a standard WinForms-application. I want to implement such functionality:
user can press and hold only one keyboard button at a time. If he tried to press a button, while another button pressed, then it gets no result.
PS: this behavior spreads only to a form that I want, not to all forms of my application.
C#, 2.0 - 3.5, VS 2008
I got something similar than Khadaji
private Keys CurrentKey = Keys.None;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (CurrentKey == Keys.None)
{
CurrentKey = e.KeyData;
// TODO: put your key trigger here
}
else
{
e.SuppressKeyPress = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == CurrentKey)
{
// TODO: put you key end trigger here
CurrentKey = Keys.None;
}
else
{
e.SuppressKeyPress = true;
}
}
I banged this out pretty quickly, so you might have to tinker with it to make it work, but it should get you started.
Set your form's KeyPreview to true. Put the in a KeyDown event and KeyUp Event.
Keys MyKey;
bool KeyIsDown = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
e.Handled = true;
}
else
{
MyKey = e.KeyData;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
if (e.KeyData == MyKey)
{
KeyIsDown = false;
}
}
}
Related
Is an easy way to cancel click event when user hit enter on button (instead of mouse click on button?)
i have tried with:
private void button3_Click(object sender, EventArgs e)
{
KeyEventArgs ke = e as KeyEventArgs;
if (ke != null)
{
if (ke.KeyCode == Keys.Enter)
{
return;
}
}
}
But ke is null
public void btnClick(object sender, EventArgs e)
{
bool IsMouse = (e is System.Windows.Forms.MouseEventArgs);
// If not mouse, they hit spacebar or enter
}
Yes it will be null. because EventArgs is not KeyEventArgs
KeyEventArgs will be passed as a parameter to KeyDown or KeyUp events. You're messing up things.
You can do something like this
private bool flag = false;
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
flag = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (flag)
{
flag = false;
return;
}
//else do original task
}
You can handle KeyPress event for the button and disable or ignore enter key there instead of returning in button click
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData & Keys.KeyCode) == Keys.Enter)
{
SendKeys.Send("{Tab}");
return true;
}
return false;
}
Let's say I've got this:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Non-numeric key pressed => prevent this from being input into the Textbox
e.SuppressKeyPress = true;
}
}
and this:
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
try
{
UpdateState(double.Parse(((TextBox)sender).Text));
}
catch (Exception ex)
{
((TextBox)sender).Text = ioElement.StateVal.ToString("0.00");
}
}
}
I know this code doesn't make a lot of sense, it's just test.
The question is: Will the e.SuppressKeyPress = true in KeyDown event have affect on KeyUp event, so the Enter key will not be accepted?
No, e.SuppressKeyPress = true will just ignore the Enter key (it won't go to the next line and Text property of the textbox won't be changed) and e.Keycode will be visible in the KeyUp. Therefore suppressing the key in the KeyDown doesn't affect the KeyUp event and your code should work. The UpdateState will be called when you hit Enter button in the TextBox. You can try this code to check it:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
e.SuppressKeyPress = true;
}
}
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show("Up");
}
}
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.
If you press and hold the 5 key on the numpad it will continue to execute a statement in the KeyDown event handler. How can i ensure the statement is executed only once, even if i hold the key down?
Thanks for your attention.
private void form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.NumPad5)
{
dados.enviar("f"); //I want this to run only once!
}
}
You can set flag on key down and reset it on key up.
private bool isPressed = false;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.B && !isPressed )
{
isPressed = true;
// do work
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (isPressed )
isPressed = false;
}
bool alreadyPressed = false;
...
if (e.KeyCode == Keys.NumPad5 && ! alreadyPressed)
{
alreadyPressed = true;
...
You should use IsRepeat flag to check if it's the first time key is pressed.
if (e.KeyCode == Keys.NumPad5 && !e.IsRepeat)
I have no idea how do this.
I know only how do detect one key:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
{
MessageBox.Show("C key");
}
}
You have to keep track of keydown/keyup events, and keep a list of all the keys that are currently "down". The keyboard handler can only trigger on individual keys, and it's up to your code to detect/keep track of which ones are down, and if those individual keydown events are close enough to each other to be counted as "together".
put a break point in your key down event and press your two keys together. examine the KeyData of the KeyEventArgs. it will show you what you have to use to detect two keys pressed together. Use some dummy code like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("KeyData is: " + e.KeyData.Tostring());
}
like I have done for shift and r pressed together
As you can see, you can use a timer event with booleans to detect if two keys are pressed:
bool keyup = false;
bool keyleft = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = true;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (keyleft && keyup)
{
Console.Beep(234, 589);
}
}
Use this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
string keysPressed = keyData.ToString();
// your code goes here
}
This is what I get for Up + Shift: "Up, Shift"