I have got some controls on the Panel and I am trying to delete them using "Delete" button. I handled KeyPress Event as mentioned in How to get Keypress event in Windows Panel control
Your issue is that the event MainForm_KeyUp does not even get fired on your key up, because you have focues another control. But you can fix that with KeyPreview.
A Form object has the property KeyPreview. According to the MSDN:
Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
So when you set:
this.KeyPreview = true;
You enable that your MainForm gets notified about those key events always. Even when any other Control is focused. So you enable that those key events will invoke MainForm_KeyUp().
Now set a breakpoint:
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
//set a breakpoint here, so you get confirmation, that the event will get fired
//on key up of the *delete* button
//...now do what you desire
}
Related
what I have Tried is ?
Window.Current.CoreWindow.KeyDown += keydown;
private void keydown(CoreWindow sender, KeyEventArgs args)
{
//Printing entered key
}
I'm developing UWP application.In my app Window.Current.CoreWindow
keydown event fired for all the keys in keyboard except "Tab" key.
I don't know why the event don't fire for that specific key?I want to do some actions while "Tab" key is pressed. Anyone know, how to fire the event when "Tab" key is pressed ?
By testing, sometimes pressing Tab key will not trigger CoreWindow.KeyDown event handler when there are some controls such as Button can get focus in a page. You could try to add a UIElement.KeyDown event or a UIElement.PreviewKeyDown event to a page(such as MainPage) in xaml file.
Update:
When you use CoreWindow.KeyDown event and there are controls which could get focus in your page, pressing Tab key will let the focus step into a tab sequence instead of triggering the CoreWindow.KeyDown event. The CoreWindow.KeyDown event could be triggered when Tab key is pressed and the focus locates at the last control which could get focus.
If you want CoreWindow.KeyDown event to be triggered when Tab key is pressed, you could set TabNavigation as Once in your page. If you want save the Tab key’s feature that stepping into tab sequence, we still suggest you use UIElement.KeyDown event or UIElement.PreviewKeyDown event.
For example:
Window.Current.Content.KeyDown += Content_KeyDown;
Update:
Window.Current.Content.KeyDown event is a routed event. About routed event, you could refer to the document. A routed event is an event that is potentially passed on (routed) from a child object to each of its successive parent objects in an object tree.
In your scenario, you could monitor the value of e.OriginalSource and you could view that when you step into the last control which could get focus by pressing Tab key the KeyDown event will be triggered twice. In the second trigger, the value of e.OriginalSource could be Windows.UI.Xaml.Controls.Frame( may be different, subject to your observation in the second trigger). That’s because the routed KeyDown event need to bubble to its parent object at this time. You could add some code to identify the second trigger.
For example:
private void Content_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalSource.ToString()!= "Windows.UI.Xaml.Controls.Frame")
{
//Just be fired once here
}
}
Note, you could try to use code e.Handled = true; to stop the routed behavior referring to here. And, if you use Page.KeyDown event, there is no situation that KeyDown event be triggered twice.
I have a windows form application , In which i created two tab pages. In one of the tab page i have a button to send email notification.
In the tabpage leave event i have some code to perform some other actions.
When i click on this button to send email. First it fires tabpage leave event , as ithe button contains button1.enabled=false; in the first line as below,
private void btnTestEmail_Click(object sender, EventArgs e)
{
btnTestEmail.Enabled = false;
bool sent = Support.SendEmail("Test Email", "This is a test email, Please ignore.", null);
--
}
But when i remove btnTestEmail.Enabled = false; code it is not firing tabpage leave event.
What could be the reason that it fires the leave event of tab page. As it is vbery strange behaviour. As i dont want to fire any event of tab page .
Regards
Changing btnTestEmail.Enabled to false will change the ActiveControl, which fires the Leave event.
According to MSDN:
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and
so on), by calling the Select or SelectNextControl methods, or by
setting the ContainerControl.ActiveControl property to the current
form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
What you can do:
What I would do to eliminate this behavior is unsubscribing the Leave event and re-subscribing it after setting the Enabled property to false.
Like this:
this.tabPage1.Leave -= new System.EventHandler(this.tabPage1_Leave);
btnTestEmail.Enabled = false;
this.tabPage1.Leave += new System.EventHandler(this.tabPage1_Leave);
The Problem is, that the Leave event fires if the control isnt the active control. If you click the Button the TabPage changes from active to inactive because the Button is active control now.
In my Windows application, I made a multiline textbox by setting AcceptsReturn property to True. It lets the user enter multiple lines of text into the textbox. Also, I'd like to do something every time, the Return/Enter key is pressed in the textbox. The event handler code is as follows...
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
// do something here
}
It appears that, if AcceptsReturn is set to True and the Return key is pressed, this event handler is not called at all. Any other key press is detected properly. If AcceptsReturn is not set to True and the Return key is pressed, the event handler is called and Return key press is detected just fine. The problem with this is that pressing Return key doesn't advance the user to the new line in the textbox (as expected).
So, I'd like the Return key press to properly advance the user to the new line in the textbox as well as I'd like to be able to detect that Return key press. Is my approach wrong? Is there a better way to do this?
KeyDown is bubbling event, which means it is first raised on the source control (the TextBox), then on the parent, then on the parent's parent, and so on, until it is handled. When you set AcceptsReturn to true, the control handles the Return key, so the event is not bubbled. In this case you can use the tunneling version of the event: PreviewKeyDown, which is raised on each ancestor of the control from the top to the bottom before it reaches the source control.
See Routing Strategies on MSDN
I have a TextBox that only allows certain characters to be typed into it; I'm handling this logic in the PreviewTextInput event. If it's a character that's allowed, then the TextChanged event is fired, otherwise it cancels the TextChanged event.
I have another application that is opened on a second screen while this is running, however even if there is keyboard input while the other application is active, it should still update the currently focused TextBox on the main application. To do this, I added a listener to the OnKeyPress() event on the second application, which calls the PreviewTextInput event on the focused TextBox in the main application.
Here is the code:
private void ImageEventController_OnKeyPress(char c)
{
object focusedElement = this.CurrentKeyboardFocus;
if (focusedElement != null)
{
if (focusedElement is TextBox)
{
TextBox target = (TextBox)focusedElement;
if (target.IsEnabled)
{
string text = c.ToString();
var routedEvent = TextCompositionManager.PreviewTextInputEvent;
target.RaiseEvent(new TextCompositionEventArgs(
InputManager.Current.PrimaryKeyboardDevice,
new TextComposition(InputManager.Current, target, text)) { RoutedEvent = routedEvent });
}
}
}
}
When this is called, it goes through the PreviewTextInput event, however the TextChanged event never gets fired even if it's a valid character. Is there any reason why TextChanged is not getting fired when PreviewTextInput is invoked programmatically?
UPDATE:
I added in this code to the bottom of my PreviewTextInput event listener:
if (!e.Handled)
{
textbox.Text = e.Text;
}
This forces the TextChanged event to fire and fixes the functionality when the second application has focus, however if the main application has focus it causes two TextBoxes to get updated when only pressing one button.
I was unable to figure out how to invoke the TextChanged event from PreviewTextInput, but I did manage to accomplish what I needed to do:
Instead of performing the logic to validate the key pressed is valid inside of the PreviewTextInput event, I pulled all of the logic out and put it into a public function. Then in my ImageEventController_OnKeyPress event I am using the LogicalTreeHelper.GetParent() method to find the necessary control. From there I call the public function to validate a valid key is pressed and if it is, I call the TextInput directly.
Issue
Weird problem. We've got two forms: the main application window and a settings form. The main form has its KeyPreview set to true, and a method attached to its KeyUp event. This is so that the settings window can be opened with a key shortcut (ctrl-m):
private void MyShortcuts(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.M)
{
e.Handled = true;
e.SuppressKeyPress = true;
MySettings sett = new MySettings();
sett.Show();
}
}
Now, that bit works just fine. However, the problem is that despite setting the Handled and SuppressKeyPress properties to true, the KeyUp event is still passed on to the MySettings form. I've traced this to ControlNativeWindow.OnMessage receiving what seems to be a different event (its Handled and SuppressKeyPress properties are set to false), and passing that on to the form and its focused control.
Questions
First of all, why is the event passed on despite instructing .Net not to do so?
Secondly, how do I prevent the event from firing?
Any ideas will be much appreciated, I run out of them myself.
What's happening here is that the M and the CTRL key are raising two separate KeyUp events (which is normal behavior). When you press CTRL and then M and then lift your finger off of the M key, a KeyUp event is raised, which your handler on the main form catches and uses to show the settings form. You then take your finger off of the CTRL key, which raises another KeyUp event (this time on the settings form, which is now the active form).
On the settings form, you can just check e.Control and ignore the event if it's true.
Instead of the KeyUp Event you should use the KeyDown event.
If you take a look at the documentation you'll see, that before the KeyUp event a KeyPress event will be thrown and this will be catched by your settings form.