I want to find out if Enter was pressed in
private void txtMessage_TextChanged(object sender, EventArgs e)
{
// do something
}
But I can't do this :
private void txtMessage_TextChanged(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Then Enter key was pressed
}
}
Since txtMessage_TextChanged doesn't have this overload .
Any suggestions ? thanks !
You need to subscribe to an event that actually captures that information.
The KeyPress event fires before TextChanged, so you could use a bool to indicate whether the Enter key was pressed.
bool enterKeyPressed = false;
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
enterKeyPressed = (e.KeyChar == (char)Keys.Enter);
}
private void txtMessage_TextChanged(object sender, EventArgs e)
{
if (enterKeyPressed)
{
}
}
register to this:
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Login_KeyPress);
In that method set a boolean or something if keychar == 13
you can use keypress event to capture enter key event
Related
I have registed ctrl+` as a hotkey,and I want to display a window when I press the hotkey and not release the leftctrl key and toggle ` key to do something else just like alt+tab switch the application.Here is the code.
private void ListOnKeyDown(object sender, KeyEventArgs e)
{
if (e.SystemKey==Key.LeftCtrl && e.Key == Key.Oem3)
{
m_host.SelectNext();
}
}
But I found this way only fired one key.So what's the right way to fire the key event?
And here is the debug information.
debug information
Just change your keydown event to detect the modifier this way
private void ListOnKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Oem3)
{
m_host.SelectNext();
e.Handled = true;
}
}
Because onKeyDown event works for ONE key only, in order to use two keys at same time you need little improvise:
bool firstkeyisOn = false;
private void ListOnKeyDown(object sender, KeyEventArgs e)
{
if (e.SystemKey==Key.LeftCtrl/*Or other key by choice*/)
{
firstkeyisOn = true;
e.Handled = true;
return;
}
if(firstkeyisOn && (e.Key == Key.Oem3/*Or other key by choice*/))
{
m_host.SelectNext();
}
}
private void ListOnKeyUp(object sender, KeyEventArgs e)
{
if (e.SystemKey==Key.LeftCtrl/*Key must be same as holding one*/)
{
firstkeyisOn = false;
}
//or
//firstkeyisOn = false;
}
To cancel hotkey mode you just add firstkeyisOn = false under OnKeyUp event and you good to go.
I have a question. Examples that i find is on a "KeyPress" and they are not longer working on WPF
Can you tell me, how to allow only specified keys from keybord to be write on WPF textbox? I know about keyUp and Down functions, but how to define letters that i want to by type into it?
I think it will be easier, if i will post my code and i tell u what i want to do. What to change here?
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}
You can use PreviewKeyDown and use e.Key to filter out what you need.
Or, in any place of your code you can use Keyboard class:
if (Keyboard.IsKeyDown(Key.E)) { /* your code */ }
UPDATE:
To forbid a key, you need to set event as handled:
if (e.Key == Key.E)
{
e.Handled = true;
MessageBox.Show($"{e.Key.ToString()} is forbidden");
}
That thing works pretty fine to me (thanks for #JohnyL):
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
else
e.Handled = true;
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}
I have a textbox and a button.I'm saving the value(keyboard key) entered in the TextBox.I need to give a message when I press the right keyboard key.
private void btn_Click(object sender, EventArgs e)
{
Properties.Settings.Default.text1 = text1.Text;
Properties.Settings.Default.Save();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == text1.Text) //--------->> error
{
MessageBox.Show("success");
}
}
how can I provide this condition?
maybe easier it will be to use KeysConverter
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
KeysConverter convertor = new KeysConverter();
string keyPressed = convertor.ConvertToString(e.KeyValue);
if (keyPressed == text1.Text)
{
//do stuff
}
}
If you compare with one char text. You can try this.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (txt.Text.Length == 1 && e.KeyValue == (int)txt.Text[0]) //--------->> error
{
MessageBox.Show("success");
}
}
This piece of code detect most keys and puts it in a messageBox, but one of the keys it doesn't is the enter key. *Note it is a key_down method
MessageBox.Show(e.KeyData.ToString());
so far I have tried so many methods to fix and searched a lot for an answer
I have tried this
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
e.IsInputKey = true;
}
}
it says 0 references which I know I'm not calling the method but where do I call it?
where ever I do call it, it gives an error.
you delete this line
e.IsInputKey = true;
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
//e.IsInputKey = true;
}
}
or
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
//e.IsInputKey = true;
}
}
In your Form1 Design Properties:
AcceptButton Property set dropdown to button1 or the name of your Button
KeyUp Event of your Button
private void button1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key has been pressed");
}
}
or
private void button1_KeyUp(object sender, KeyEventArgs e)
{
button1_KeyDown(sender, e);
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key has been pressed");
}
}
Put this line soon after: InitializeComponents(); -->
button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown);
.
The event you're trying to use will only fire when you focus on that button. The focus is passed in 2 different ways ( via user interaction ) one of which is an actual mouse click on that element and another is "tabbing" on to the control, and make sure you read about "Events" and "EventsHandlers"
Ok, so I'm in the process of making a Tic-Tac-Toe game to help me learn C#.
I'm attempting to add a bit of functionality to it, so I want people to be able to use the NumPad on a computer to simulate clicking the buttons.
Here is what I have but when I use the NumPad the buttons don't click.
Can any of you see a reason as to why?
//===============================
// start NumPad Simulate Clicks
// NumPad MyButtons
// 7 8 9 1 2 3
// 4 5 6 4 5 6
// 1 2 3 7 8 9
//===============================
public void myControl_NumPad7(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad7)
{
button1_Click(null, null);
}
}
public void myControl_NumPad8(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad8)
{
button2_Click(null, null);
}
}
public void myControl_NumPad9(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad9)
{
button3_Click(null, null);
}
}
public void myControl_NumPad4(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad4)
{
button4_Click(null, null);
}
}
public void myControl_NumPad5(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad5)
{
button5_Click(null, null);
}
}
public void myControl_NumPad6(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad6)
{
button6_Click(null, null);
}
}
public void myControl_NumPad1(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad1)
{
button7_Click(null, null);
}
}
public void myControl_NumPad2(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad2)
{
button8_Click(null, null);
}
}
public void myControl_NumPad3(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.NumPad3)
{
button9_Click(null, null);
}
}
EDIT
Noticed that I have to be clearer about what I mean...
From the code you posted I suspect you have 9 controls you added your key-events to. These controls will only receive key-events when they are focused.
To handle keys globally for the form, you have to set Form.KeyPreview to true. Also, I'd not handle the keys the way you do, but add a Form.KeyDown event and write something like:
switch (e.KeyCode)
{
case Keys.NumPad9:
e.Handled = true;
button3.PerformClick();
break;
case Keys.NumPad8:
e.Handled = true;
button2.PerformClick();
break;
// And so on
}
This will handle the NumPad-Keys within the form - you can then remove all the event handlers you posted in your question.
To programatically "click" a button, you should use the Button.PerformClick() method, as more than one event handler may be added to the click event, which would not be called otherwise.
EDIT 2
Syntax for the switch-statement was invalid. Of course every "case" must start with the case keyword... Now it should work.
You need to use button1.PerformClick(); for every button to invoke event correctly, here's info.