Im trying to do like that: If I press the key "P" a messagebox will open in the screen.But I need to do it without a textbox or other tool, I want to do that direct in the form.
I tried:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.P)
{ MessageBox.Show("Key P pressed"); }
}
Try with the KeyPress Event of the form. It just works fine.
Assuming this is Winforms, on the form you're trying to catch the event on, make sure to set
Form1.KeyPreview = true;
KeyPreview ensures that keyboard events anywhere on the particular form (such as to a textbox with focus) will still count as a keyboard event for the form itself.
Related
The if statement is being checked until I hit enter then it goes straight to another method. My guess is there is something else on the form that is getting triggered when I hit enter but I can't find it despite my search.
I want to not have to put a button on the form to call this function, the button I had worked but I just want to be able to hit enter from my textbox input.
Here is my code below:
private void textBox1firstName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
searchAD();
}
}
private void textBox2lastName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
searchAD();
}
}
searchAD() is not getting called despite hitting enter. Any suggestions? Thank you!
The form has a KeyDown event, but also has a "AcceptButton" property, which hooks the [Enter] keypress and can call an event handler. Check if there are event-handlers attached to either of those on the form.
I have a Form1_KeyDown event for form1, but while I am typing some text in the Textbox on the Form, the event gets triggered.
How can I stop that event when I am typing inside the textboxes on the form.
There are two ways:
Set form's KeyPreview to false. Which is the default, so you must have explicitly changed it, presumably for a reason. Otherwise the controls of on the form always get the keyboard events first.
Add an active control check in Form1_KeyDown, like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (this.ActiveControl == textBox1) return;
var k = e.KeyCode;
}
I have a program containing multiple C# Forms TextBoxes. I've set up Hotkeys for the entire form activating certain functions. My problem is that my Hotkeys have been set onto the Form KeyDown event and they activate if I write something on a TextBox.
Example: One Hotkey might be I. Everytime I write the letter onto a textbox the Hotkey activates.
Alterior solutions and problems: I've thought about putting a Key in front of the Hotkey like CTRL+Hotkey, but these also present problems as CTRL+C is Windows Copy command etc. SHIFT is an UpperKey button.
Question: Can I prevent Hotkeys from activating when I am writing onto a TextBox without having to go through all of them in the form?
EDIT: Some code as requested. The button codes come from a stored XML file or the Hotkeys Form+Class (separate) where I've set up a window for them.
public Hotkeys hotkeysForm = new Hotkeys();
void Form1_KeyDown(object sender, KeyEventArgs e)
{
toggleInformation = hotkeysForm.toggleInformation;
if (e.Control && e.KeyCode == toggleInformation)
{
showInfo(true);
}
else if (e.KeyCode == toggleInformation)
{
if (!isInfoActive)
showInfo();
else
hideInfo();
}
}
You can disable hotkeys while texbox is an active control. Add the Enter and Leave events for all textboxes:
private void textBox_Enter(object sender, EventArgs e)
{
KeyPreview = false;
}
private void textBox_Leave(object sender, EventArgs e)
{
KeyPreview = true;
}
You should try this hack, if it could solve your problem,
Create a Extented TextBox and use it in your code. you can handle whether to write the pressed key in textbox or not in hotkeyPressed check.
public class ETextBox : System.Windows.Forms.TextBox
{
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (hotKeyPressed) // this is the condition when you don't want to write in text.
{
//Do whatever you want to do in this case.
}
else
{
base.OnKeyDown(e);
}
}
}
I have this code:
private void button5_Click(object sender, EventArgs e)
{
Magnifier20070401.MagnifierForm mf = new Magnifier20070401.MagnifierForm();
mf.Show();
}
It shows the target form correctly. But instead of using a button click, I want to use Ctrl+M to show this form. If the users types Ctrl+M again, I want to close the the form.
How can I do this?
Edit:
This is what i did wich is working :
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "M")
{
Magnifier20070401.MagnifierForm mf = new Magnifier20070401.MagnifierForm();
mf.Show();
}
}
In the constructor of Form1 i added:
this.KeyPreview = true;
So now when i click on Ctrl+M i see the new Form.
What i need now is how to make that if i click again on Ctrl+M it will close the new Form.
Maybe using a flag ?
Edit:
This is what i did now:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "M")
{
if (mf == null)
{
mf = new Magnifier20070401.MagnifierForm();
mf.Show();
}
else
{
mf.Close();
this.Invalidate();
}
}
}
But even doing this.Invalidate(); i don't see the new Form closed.
But if im using put a breakpoint on the mf.Close(); and step into(F11) i see it close when making continue.
Why it dosen't close without using a breakpoint ?
You can add onKeyPress or onKeyDown
and check if Ctrl+M were pressed
private void OnKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
if (((Control.ModifierKeys & Keys.Control) == Keys.Control)
&& (e.KeyChar == 'M'|| e.KeyChar == 'm'))
{
mf.Show();
}
You would use the InputBindings object. I think in your case, probably best to put that at the Window level (Window.InputBindings). More information here:
http://msdn.microsoft.com/en-us/library/system.windows.input.inputbinding.aspx
You can solve this in 2 ways.
If you are using a GUI interface, add a MenuItem control on your Menu, and put the Shortcut property to Ctrl+M then double click the MenuItem to edit the code, then call your launchMagnifier() function. If you do NOT want your menu to show, just set the visible properties to false. This keeps the menu hidden if you do not want it, yet still holds the functionality.
If you do not want the MenuItem, you can catch keys that are pressed in your form. So in your frmMain.cs form, add an event to capture keys, then when Ctrl+M is pressed, invoke launchMagnifier()
A few ways to do that.
On your form set the KeyPreview Property to true
Then add an OnKeyPress or OnKeyDown event handler to the form.
In that test for Ctrl-M and show / destroy the form and set handled (e.Handled) to true.
Any other keypress will be passed on to the currently focused control as it hasn't been handled.
I'm making a program in C#, And I want to know when the user is pressing\pressed a keyboard button (for now: 1-9 buttons on keyboard).
How can I do it?
The Control.KeyPress event (and related KeyDown and KeyUp) should do what you need. Just define an event handler for the one you need in your form:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("Key pressed: " + e.KeyChar);
}
The MSDN page under the link has a more extensive example that deals with "special" keys (you will need to use KeyPress or KeyDown for those).
If you want to capture keys while the focus is not on your form, that's a different matter entirely, but I don't think that's the case as you want to capture keys 1-9. Not the typical global hotkey :)
Don't forget to set the KeyPreview property to true, else other controls on your form (if you have other controls on your form) will receive the event (if they have focus) before the form gets it.
Hook an function to the OnKeyUp event of a form.
see here
private void form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) || (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9))
{
//Do something
}
}
What you search are the 2 Events KeyDown and KeyUp.
KeyUp is when a Key "was" pressed and the user lift his finger up.
KeyDown is the other event.
Just take a new Form. Go to the Events(Press F4) and you will find KeyDown and Up.
private void maskedTextBox1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
and so on :D