When the user presses the Shift + UP keys, I want my form to respond by calling up a message box.
How do I do this in Windows Forms?
Handle the KeyDown event and have something like:
if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.Up)
{
MessageBox.Show("My message");
}
The event handler has to be on the Main Form and you need to set the KeyPreview property to true. This can be done in design mode from the properties dialog.
In case you want to use multiple modifiers KeyEventArgs also has boolean values to indicate if CTRL, ALT or SHIFT is pressed.
Example:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.Alt && e.Shift && e.KeyCode == Keys.F12)
MessageBox.Show("My message");
}
In this example the messagebox is show if CTRL, ALT, SHIFT and F12 are pressed at the same time.
To handle multiple modifiers keys ( In KeyDown event)
if (e.Control && e.Shift)
{
if (e.KeyCode == Keys.F1)
{
// Your code goes here
}
}
Related
I have to catch the event when I pressed on Shift Tab in TextBox to write some code. It is possible to do that? I tried with that test on KeyUp event :
private void txtJustifTampon_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && Control.ModifierKeys == Keys.ShiftKey)
{
//do stuff
}
}
One of the possible ways out is to use PreviewKeyDown instead of KeyUp since
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are
typically ignored by some controls because they are not considered
input key presses
private void txtJustifTampon_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
// If Shift + Tab pressed (i.e. Tab with Shift modifier)
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Shift) {
//TODO: put relevant code here (do stuff)
}
}
Please, notice that we should use Keys.Shift (not Keys.ShiftKey) as the modifier and we should apply modifier to the event argument (e.Modifiers)
I am developing an windows application.
In that application i have a list box control on one form.Now i need to detect if user presses tab key or Shift+tab key.how can i detect this on list box leave event.
You need to get the KeyEventArgs to detect what key has been pressed. But assuming that when a user presses Tab or Shift+Tab the control loses focus you can just listen on the OnKeyDown or OnKeyPress and check if the pressed items are Tab or Shift+Tab
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
//Check for Tab key
if (e.KeyCode == Keys.Tab)
{
//Do something
}
//Check for the Shift Key as well
if (Control.ModifierKeys == Keys.Shift && e.KeyCode == Keys.Tab) {
//Other stuff to do
}
}
I have a textbox and below it i have a listbox.
While the user is typing in the textbox if he presses the up or down arrow he should make a selection in the listbox. The textbox detects all the characters (except space) but it seems that it can't detect the arrow presses.
Any solution for this? This is a WPF project btw.
EDIT, Here's the working code thanks to T.Kiley:
private void searchBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
{
e.Handled = true;
//do your action here
}
if (e.IsDown && e.Key == Key.Up)
{
e.Handled = true;
//do another action here
}
}
I just tried this and it works. Add a preview key down event to the textbox
private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
MessageBox.Show("It works");
}
You can listen to they KeyDown event of the TextBox. In the handler, check whether the arrow key was pressed (you might need to listen to key up to avoid triggering your code multiple times if the user holds down the button for too long).
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
// Do some code...
}
}
Hi have an windows application that should focus to previous text box with change in back color when shift+tab is pressed.
e.Modifiers == Keys.Shift && e.KeyCode == Keys.Tab
in an event handler method, using an if condition maybe.
Try this,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift)
{
}
}
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