c#..Space KeyDown and Space in a textBox [duplicate] - c#

This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I am making a program to record the position of a mouse when someone presses the Space button.
This works fine, however when I put the cursor in any textBox in the form the code becomes useless of course because the space gets typed in the textBox. I tried to change the focus() or try other keys like LeftWin ... but none worked!
Any advice on how can I detect the Space button (or any other key) all the time in a form?
private void lebel1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
//bla bla
}
}

The event is not firing because the Textbox is handled the event, with that being said, the event will not be propagated to the parent controls (Bubbling events).
You could use a KeyPress, which means that the event will be fired from the most outer control and will be propagated to their children. (Tunnel events)
You could learn more about here.

Related

Canceling the SpaceBar Click/Tap on uwp controls throughout the app

When a UI element ( control ) is focused in a uwp app it can be triggered with Spacebar or the Enter keys, this is not limited to Desktop but also helps in Xbox so user can navigate through the controls and press on any focused control to active its Command.
Use case
But in My use case I want only Enter key to trigger that behaviour and Spacebar should not do anything at all no matter which control is pressed on the screen.
The reason to this requirement is that I am building a MediaPlayer application and no matter which control or button is focused within the app when I press Spacebar I want to simply link it to the Play/Pause Behaviour of my media element.
Not a Duplicate
This question is not a duplicate of : UWP - Don't fire Click event when pressing space (like in Movies & TV app)
Because in the question linked above, the answer was only relevant if any of the AppBarButtons were focused so they will not do anything on pressing Space but only will be invoked with Enter. But in my use case I want to apply the same behavior even outside the MediaPlayerElement control. I have a NavigationView and MediaPlayerElement resides in one of the pages, so I want this behavior to work even when a NavigationViewItem is focused or any other control which can be focused and invoked should only be invoked with Enter and not Space.
Is there a app level solution where I can apply this behaviour at the very root control and it descends to all of its children i.e : whole app?
What I have tried
I have tried with the already answered question (linked above) and that works fine for its limited scenario. And I have also tried setting AllowFocusOnInteraction=false to every app bar button and also other extra controls I have in the style of my CustomMediaTransportControls. But this is also limited to MediaPlayerElement only and also it prevents tab navigation which is not good for accessibility.
You can do this by handling the PreviewKeyDown event higher in the visual tree hierarchy, for example in the Page.
Subscribe to the event in the Page constructor:
public MainPage()
{
this.InitializeComponent();
this.PreviewKeyDown += MainPage_PreviewKeyDown;
}
Or in XAML:
<Page ... PreviewKeyDown="MainPage_PreviewKeyDown">
And in the event handler set the KeyRoutedEventArgs to handled when the Space key was pressed:
private void MainPage_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Space)
{
e.Handled = true;
}
}
This way the key down event will never reach any control below in the hierarchy because the PreviewKeyDown event propagates the tree before the event takes place.
There are many ways could approach, You could listen the current Content PreviewKeyDown event to detect Space press.
public Scenario1()
{
this.InitializeComponent();
Window.Current.Content.PreviewKeyDown += Content_PreviewKeyDown;
}
private void Content_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
e.Handled = e.Key == VirtualKey.Space ? true : false;
}
You could also GLOBAL HOTKEY for your uwp app that could be used when your app's window not in foreground. For more derail you could check this blog .

How distinguish two different buttons in same event click? [duplicate]

This question already has answers here:
How do I handle click events on multiple similar buttons in WPF?
(3 answers)
Click event for button in loop C# WPF
(3 answers)
How would you know which element of an ItemsControl sends an event in MVVM?
(5 answers)
How to find out which button I pressed?
(1 answer)
Closed 5 years ago.
On WPF, if I have only one button click event shared for two or more (52 being more precise) is there a way to distinguish which button the event come from?
private void Button_Card_Click(object sender, RoutedEventArgs e)
{
// for testing
// it works for each button, but which one has been clicked?
MessageBox.Show("Clicked");
}
First button object with event set up
Second button object with event set up
sender should be the clicked button, but also look at RoutedEventArgs.Source and .OriginalSource
I would also look into using Commands and CommandParameter to indicate which was clicked.

How to add a right click event to an object like button,label,listbox etc [duplicate]

This question already has answers here:
How to create a right click event handler in c#
(4 answers)
Closed 7 years ago.
I'm a beginner, so this may be easy. But i could not find it. I want to add a right click event to a windows form. but when i look properties/events menu of the form, I could not find any right click event. Is there a menu or code example for that? i prefer to use it auto constructed like double clicking for click event, so it is better to me if you can show me how can i find that property if it exists.
Thanks for helping.
its a mousedown event,
private void btn_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
// do your stuff
}
}

Block Events from a Form/UserControl [duplicate]

This question already has answers here:
Disable All Events on a Windows Form
(5 answers)
Closed 10 years ago.
My Application sometimes comminucate with a Server in another Thread, while the form should be locked in this time(that means, the user cant fire any Click, KeyDown .... event). In this time, a waitoverlay with a loading circle is showing which blocks any mouse events like click or mousehover.
But the problem is, that the user can fire KeyDown events by pressing keys and the user can also make mouse clicks by pressing enter after navigating to the button with the TAB Key.
I know that Form.Enabled = true; can do the trick, but then the form is looking very ugly. I know also that I could make a bool inaction; and then look at this var in the event handler, but I think theres a better way.
Short Form: How can I block GUI Events like Click or KeyDown from a Form without .Enabled = True?
Any help will be greatly appreciated.
Edit: Solved it by adding this to the overlay control:
protected override bool ProcessKeyMessage(ref Message m)
{
return true;
}
protected override bool ProcessTabKey(bool forward)
{
return true;
}
You may set the KeyPreview property of the form to false.
When the KeyPreview is false, the form will not receive key events before the event is passed to the control that has focus. And if you change your focus to some unused control which does not interact with keys, form or any controls waiting for key events will not receive any events.

Mouse Leave event on Windows Form [duplicate]

This question already has answers here:
How to detect when the mouse leaves the form?
(4 answers)
Closed 6 years ago.
I've set the 'mouse leave' event on a windows form and I want to hide the form when the mouse leaves the visible area.
But here is where I am facing a problem. Even when I move the mouse to a button on the same form, it calls the 'mouse leave' event, which makes this form invisible.
It means I've got to prevent the event triggering when moving the mouse to the button. But how?
Any other approach?
There is no simple way to do it. One way could be to check all the controls inside the Form and if mouse is not over any of them this means mouse is outside the Form
Another way could be to check inside mouse leave event whether mouse is inside the window boundary or not
it's very simple...add this:
protected override void OnMouseLeave(EventArgs e)
{
if(this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return;
else
{
base.OnMouseLeave(e);
}
}
// On the form's MouseLeave event, please checking for mouse position is not in each control's client area.
private void TheForm_MouseLeave(object sender, EventArgs e)
{
bool mouse_on_control = false;
foreach (var c in Controls)
mouse_on_control |= c.RectangleToScreen(c.ClientRectangle).Contains(Cursor.Position);
if (!mouse_on_control)
Close();
}
// And in addition, if any control inside has its client area overlapping the form's client area at any border,
// please also checking if mouse position is outside the form's client area on the control's MouseLeave event.
private void ControlOverlappedTheFormsBorder_MouseLeave(object sender, EventArgs e)
{
if (!RectangleToScreen(ClientRectangle).Contains(Cursor.Position))
Close();
}

Categories

Resources