I would like to build a calculator like Windows Calculator.
I have done the following coding but I dont know how use code e.KeyChar for Equal(=) when the user presses the Enter Button.
if(e.KeyChar == 40)
{
MessageBox.Show("you press enter");
}
but this code not run in program
Use the KeyUp or KeyDown events and query
if (e.KeyCode == Keys.Enter)
Try this
if (e.KeyChar == 13)
{
// Enter key pressed
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
MessageBox.Show("Enter Key Pressed!");
}
Related
I made a method that detects when a key is pressed, but its not working! Heres my code
void KeyDetect(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W && firstload == true)
{
MessageBox.Show("Good, now move to that box over to your left");
firstload = false;
}
}
I also tried to make a keyeventhandler but, it sais "cannot assign to key detect because it is a method group"
public Gwindow()
{
this.KeyDetect += new KeyEventHandler(KeyDetect);
InitializeComponent();
}
Use keypress event like this:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.F1 && e.Alt)
{
//do something
}
}
1) Go to your form's Properties
2) Look for the "Misc" section and make sure "KeyPreview" is set to "True"
3) Go to your form's Events
4) Look for the "Key" section and double click "KeyDown" to generate a function to handle key down events
Here is some example code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("You pressed " + e.KeyCode);
if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
{
//Do Something if the 0 key is pressed (includes Num Pad 0)
}
}
You are looking for this.KeyPress. See How to Handle Keypress Events on MSDN.
Try to use the KeyDown event.
Just see KeyDown in MSDN
Just do
if (Input.GetKeyDown("/* KEYCODE HERE */"))
{
/* CODE HERE */
}
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
}
}
How can I determine in KeyDown that ⇧ + Tab was pressed.
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Shift)
{
//do stuff
}
}
can't work, because never both keys are pressed exactly in the same second. You always to at first the Shift and then the other one..
It can't work, because never both keys are pressed exactly in the same second.
You're right that your code doesn't work, but your reason is wrong. The problem is that the Tab key has a special meaning - it causes the focus to change. Your event handler is not called.
If you use a different key instead of Tab, then your code will work fine.
If you really want to change the behaviour of Shift + Tab for one specific control, it can be done by overriding ProcessCmdKey but remember that many users use the Tab key to navigate around the form and changing the behaviour of this key may annoy those users.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (DateTimePicker_BirthDate.Focused && keyData == (Keys.Tab | Keys.Shift))
{
MessageBox.Show("shift + tab pressed");
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
If you are looking for a key press combination (Tab, then Shift) like Ctrl K + D you will have to use this modified example which was taken from MSDN social.
private StringBuilder _pressedKeys = new StringBuilder();
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
_pressedKeys.Append("Tab");
return;
}
if (e.Modifiers == Keys.Shift)
{
_pressedKeys.Append("Shift");
return;
}
if (_pressedKeys.ToString()."TabShift")
{
MessageBox.Show("It works!");
_pressedKeys.Clear();
}
else
{
_pressedKeys.Clear();
}
base.OnKeyDown(e);
}
First hook the Tab keypress event, then during the event, check the state of the Shift key. Keep in mind that there are two shift keys; make sure you check both of them.
This very related post shows how to check the state of modifier keys:
How to detect the currently pressed key?
Edit: an insight provided by another answerer who justly deserves an upvote is that the default behavior of the tab key (to change control focus) must be suppressed.
You can find your answer in
this post
It's Simple.
You can do that using KeyUp Event in the TextBox
private void txtBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
Or
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
It's Simple.
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
I use these following codes to work with numpad keys.
if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
{
MessageBox.Show("You have pressed numpad0");
}
if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.D1)
{
MessageBox.Show("You have pressed numpad1");
}
And also for the other numpad keys. But I want to know how I can to this for "+" , "*" , "/" , " -" , " . " which located next to the numpad keys.
Thanks in advance
Check out the entire Keys enum . You have Keys.Mutiply, Keys.Add, and so forth.
Note that Keys.D0 is not the numpad 0, it's the non-numpad 0.
For "+" , "*" , "/" , we can use KeyDown event and for "-" , "." we can use KeyPress event.
Here are the codes :
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Add)
{
MessageBox.Show("You have Pressed '+'");
}
else if (e.KeyCode == Keys.Divide)
{
MessageBox.Show("You have Pressed '/'");
}
else if (e.KeyCode == Keys.Multiply)
{
MessageBox.Show("You have Pressed '*'");
}
}
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '.')
{
MessageBox.Show("You have pressed '.'");
}
else if (e.KeyChar == '-')
{
MessageBox.Show("You have pressed '-'");
}
}
I used switch to make mine work.
I am making a calculator and created a KeyDown even on the target textbox. I then used:
switch (e.KeyCode)
{
case Keys.NumPad1:
tbxDisplay.Text = tbxDisplay.Text + "1";
break;
case Keys.NumPad2:
tbxDisplay.Text = tbxDisplay.Text + "2";
break;
case Keys.NumPad3:
tbxDisplay.Text = tbxDisplay.Text + "3";
break;
}
etc.
The other thing to consider is that if the user then clicked an on screen button, the focus would be lost from the textbox and the key entries would no longer work. But thats easy fixed with a .focus() on the buttons.
There is a simple way to learn all the keycodes without checking the manuals.
Create a form and then go to KeyDown event of the form then add this
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}
Then you will get the name of the any key you have pressed on your keyboard
How do you handle a KeyDown event when the ALT key is pressed simultaneously with another key in .NET?
The KeyEventArgs class defines several properties for key modifiers - Alt is one of them and will evaluate to true if the alt key is pressed.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyData != (Keys.RButton | Keys.ShiftKey | Keys.Alt))
{
// ...
}
}
Something like:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt)
{
e.Handled = true;
// ,,,
}
}
This is the code that finally Works
if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z && e.Alt){
//Do SomeThing
}
I capture the alt and down or up arrow key to increment the value of a numericUpDown control. (I use the alt key + down/up key because this form also has a datagridview and I want down/up keys to act normally on that control.)
private void frmAlzCalEdit_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.Down)
{
if (nudAlz.Value > nudAlz.Minimum) nudAlz.Value--;
}
if (e.Alt && e.KeyCode == Keys.Up)
{
if (nudAlz.Value < nudAlz.Maximum) nudAlz.Value++;
}
}
Create a KeyUp event for your Form or use a library like I did to get a GlobalHook so you can press these keys outside the form.
Example:
private void m_KeyboardHooks_KeyUp(object sender, KeyEventArgs e)
{
if ( e.KeyCode == Keys.Alt || e.KeyCode == Keys.X)
{
}
}