Does the KeyDown KeySuppress cancel KeyUp event? - c#

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");
}
}

Related

C# dateTimePicker Focus does not work

In my C# Windows Forms application I want to navigate between two dateTimePicker (dateTimePicker1, dateTimePicker2) by pressing the Enter key.
When the Form open focus on dateTimePicker1 and press Enter key then focus dateTimePicker2 and press Enter key focus dateTimePicker1.
I'm trying the below code but it doesn't work:
private void dateTimePicker2_Enter(object sender, EventArgs e)
{
dateTimePicker1.Focus();
}
The Enter Event is triggered when you enter a control by pressing tab to change focus or clicking into it. If you want to listen for the enter key you need to use the KeyDown Event.
An implementation of the event handling would look like this:
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker2.Focus();
}
}
private void dateTimePicker2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker1.Focus();
}
}
Try this
private void dateTimePicker1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
dateTimePicker2.Focus();
}
}
private void dateTimePicker2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
dateTimePicker1.Focus();
}
}
The Enter event does not represent the key press of the Enter key, it represents the user changing focus to the control.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter(v=vs.110).aspx
You should use KeyDown event
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
dateTimePicker2.Focus();
}

If Enter key hit - show message. Else - type as normal

If the user hits the enter key, I want to show a message saying the enter key was hit. Else, I want to type in the textbox as normal.
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter Was Clicked");
}
}
If enter is hit the message is displayed correctly. However, if I try to type a sentence, nothing appears in the textbox.
This will work...
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter Was Clicked");
}
else
{
MessageBox.Show(e.KeyCode.ToString());
}
}
However I don't want a messagebox showing every letter that was typed.
Try this:
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(e.KeyCode.ToString());
}
else
{
e.Handled = true;
}
}
Please work! This is the most confusing I've ever encountered!
Use the KeyPress event:
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (e.KeyChar == (char)Keys.Return)
{
// Stop the character from being entered into the control since it is non-numerical.
MessageBox.Show("Enter Was Pressed");
e.Handled = true;
}
}

Cannot disable beep sound on textbox keydown event

Below is my code for disabling beep sound when I press "Enter" on textbox KeyDown() event:
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
SaveData();
e.Handled = true;
}
But it keeps beeping when I press "Enter" on textbox. What am I doing wrong?
From your comments, showing a MessageBox will interfere with your setting of the SuppressKeyPress property.
A work-around is to delay the showing of the MessageBox until after the method is completed:
void TextBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
e.SuppressKeyPress = true;
this.BeginInvoke(new Action(() => SaveData()));
}
}
EDIT
Please note that the answer provided by LarsTech (below) is a far better approach.
Sorry, I just realised that you have a MessageBox displaying.
What you can do is have a Timer and have it fire off the SaveData() method.
private void Timer1_Tick(System.Object sender, System.EventArgs e)
{
Timer1.Enabled = false;
SaveData();
}
Then in your TextBox keypress event, do this:
if (e.KeyCode == Keys.Enter) {
e.SuppressKeyPress = true;
Timer1.Enabled = true;
}
That seems to work...
You could try creating your own textbox and handle the keydown event like so:
public class MyTextBox : TextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.KeyCode)
{
case (Keys.Return):
/*
* Your Code to handle the event
*
*/
return; //Not calling base method, to stop 'ding'
}
base.OnKeyDown(e);
}
}

Detect when two keys are pressed at the same time

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"

User can press and hold only one keyboard button at a time

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;
}
}
}

Categories

Resources