I want my button to have a pressed and released effect(simulating a mouse press) when i press my default key(enter).
I have tried doing this
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
VisualStateManager.GoToState(btnLogin, "Pressed", true);
System.Threading.Thread.Sleep(300);
VisualStateManager.GoToState(btnLogin, "Normal", true);
btnLogin_Click(sender, e);
}
}
Cant get it to work.
Did you make your button Property IsDefault=True
Try the following
'
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
VisualStateManager.GoToState(btnLogin, "Pressed", true);
this.Refresh();
System.Threading.Thread.Sleep(300);
VisualStateManager.GoToState(btnLogin, "Normal", true);
this.Refresh();
btnLogin_Click(sender, e);
}
}
'
Related
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"
I have a Menu like this:
I´d like that if the cursor is on ValorInsTextBox (Valor Textbox) and I press Enter, the app call the button InserirBtn_ClickAsync (Inserir Button) and after the process, the cursor come back to PosicaoInsTextBox (Posição Textbox).
I made some methods using Key_Down, but something weird happens. Look the code:
private void PosicaoInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
}
private void ValorInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
if (PosicaoInsTxtBox.IsEnabled)
{
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
else
{
ValorInsTxtBox.Focus(FocusState.Programmatic);
}
}
}
When I debug the code, I press Enter when the ValorInsTextBox is on focus, and the method ValorInsTextBox_KeyDown starts and everything goes well. And when it arrives on line:
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
it goes to execute the method PosicaoTextBox_KeyDown and starts to execute it. I don´t know why! Anyone can help me?
You could set the Handled property of the KeyRoutedEventArgs to true in the ValorInsTxtBox_KeyDown event handler to prevent the PosicaoInsTxtBox_KeyDown event handler from being invoked:
private void ValorInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
if (PosicaoInsTxtBox.IsEnabled)
{
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
else
{
ValorInsTxtBox.Focus(FocusState.Programmatic);
}
}
e.Handled = true;
}
And do the same in the PosicaoInsTxtBox_KeyDown event handler to prevent it from being invoked again when you press ENTER in the Posicao" TextBox:
private void PosicaoInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
e.Handled = true;
}
Code run when I use with key A.
But code not run when I use with key return.
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txt = e.Control as TextBox;
txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
}
void txt_KeyPress(object sender, KeyPressEventArgs e)
{
/* Code run. */
if(e.KeyChar == (char)Keys.A)
{
MessageBox.Show("Hello");
}
/* Code not run! */
if (e.KeyChar == (char)Keys.Return)
{
MessageBox.Show("Hello");
}
}
But it run if I try with a text box.
Thank you TaW!
I solved it like this...
void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
}
}
void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
/* Your code here! */
}
}
I am working on a CustomControl and I want to register ModifierKeys in this control. I have already set the KeyPerview to True in the Form which this control is being added to.
Now I have a Boolean named _ctrl and I want this boolean to be true when the Control key is being held down and it should be false when Control key is being released.
I tried to achive this with the conde belowe in my CustomControl but no success!
private bool _ctrl = false;
private void MyCustomControl_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Control)
{
_ctrl = true;
}
}
private void MyCustomControl_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Control)
{
_ctrl = false;
}
}
Any tips/soloution will be appriciated!
UPDATE
Ok, I decided to do the key down and up event in the form itself:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control)
{
projectBrowser.ControlKeyIsDown = true; //bool in MyCustomControl
MessageBox.Show("CTRL is PRESSED");
}
}
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control)
{
projectBrowser.ControlKeyIsDown = false; //bool in MyCustomControl
MessageBox.Show("CTRL is DEPRESSED");
}
}
Now, The KeyDown event detects the control key and messagebox is being shown. But the KeyUp event does not work and it doesn't show the messagebox. What could be wrong?
It seems that the key up gets detected if I change the KeyUpevent like this:
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.Control)
{
projectBrowser.ControlKeyIsDown = false;
e.Handled = true;
}
}
You can try calling the Control.ModifierKeys property:
protected override void OnKeyDown(KeyEventArgs e) {
if (Control.ModifierKeys == Keys.Control) {
MessageBox.Show("I am pressing control.");
}
base.OnKeyDown(e);
}
If you throw up a MessageBox on the KeyDown event, the KeyUp event won't get called.
I have the following code which does not show the MessageBox when enter/return is pressed.
For any other key(i.e. letters/numbers) the MessageBox shows False.
private void cbServer_TextChanged(object sender, EventArgs e)
{
if (enterPressed)
{
MessageBox.Show("Enter pressed");
}
else
MessageBox.Show("False");
}
private void cbServer_Keydown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
enterPressed = true;
MessageBox.Show("Enter presssed: " + enterPressed);
}
else
enterPressed = false;
}
Any ideas?
EDIT: Above code, I thought the issue was with the _Keydown even so I only posted that.
in your form designer class (formname.designer.cs) add this :
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Login_KeyPress);
and add this code to backbone code (formname.cs):
void Login_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
MessageBox.Show("ENTER has been pressed!");
else if (e.KeyChar == (char)27)
this.Close();
}
This is because when you press Enter TextChanged event won't fire.
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter key pressed");
}
else if (e.Key == Key.Space)
{
MessageBox.Show("Space key pressed");
}
}
Use PreviewKeyDown event to detect any key before shown in textbox or input
void cbServer_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
{
// Check if User Presses Enter
if (e.KeyCode == System.Windows.Forms.Keys.Return)
{
// Here: user pressed ENTER key
}
else
{
// Here: user did not press ENTER key
}
}