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 .
I am a fairly new to Visual C# but have coded in Delphi for a long time. I have created a form that has a simple panel that is hidden unless you type in a particular key on the keyboard (I do have "KeyPreview" set for true for the form and I am using the KeyDown event to handle determining if the correct key was pressed and to make the panel visible/invisible). Just beneath the panel is a webBrowser component.
What is happening is as follows:
When my form initially starts, I have code for the "Shown" event that makes sure the form has focus initially:
private void Form1_Shown(object sender, EventArgs e)
{
if (!Focused)
{
Focus();
}
}
When the form is focused at this point, my code for detecting the proper keystroke to get the panel to appear or disappear works fine.
Here is where things get strange and I'm not sure of what to do. There are two parts I am dealing with for what is wrong:
If I click on another form and then on the caption bar of my form again to get focus on my form and try a keystroke, the keystroke detection does not work. However, if I click on another form and then back on my form one more time, the keystroke detection for the form does work. What can I do to make sure that this works each time my from has focus again?
If I click on the web browser component within my own form, the KeyDown code for the form no longer gets enacted. Even if I click on the caption bar for the form, the KeyDown event does not work. What do I need to do to assure that, if a component within my form is clicked, my form will still respond for the KeyDown event?
Thanks in advance for any advice.
I can't say why your caption bar seems to be intercepting key events. It may be that various components on the form can have focus and thus capture keyboard events. I know the web browser control works this way. You may consider capturing keyboard events globally.
I saw something on Codeproject that shows how to do this. I hope this helps.
They use UserActivityHooks.
UserActivityHook actHook;
void MainFormLoad(object sender, System.EventArgs e)
{
actHook= new UserActivityHook(); // crate an instance
// hang on events
actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
actHook.KeyDown+=new KeyEventHandler(MyKeyDown);
actHook.KeyPress+=new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp+=new KeyEventHandler(MyKeyUp);
}
As for the webbrowser control, of course it is going to interecept keyboard events. Users often have to enter text in forms and developers often code javascript on webpages to specifically hook into keyboard events. So the webbrowser control must be able to capture those.
first of I'm working with WPF and i'm new to it.
I added a KeyDownEvent to a content control in xaml:
<ContentControl Grid.Row="1" Name="dockingContentControl" KeyDown="dockingContentControl_KeyDown"/>
I know, probably there is a better way to add a KeyDown event.
(Would also help)
But the event is working. So I used a simple Console.WriteLine command like so:
private void dockingContentControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
Console.WriteLine(e.Key);
}
and it showed me the buttons I pressed.
But somehow it did not trigger with the Arrowkeys.
I don't know why all other (I think all, didn't test all) buttons are working, but the Arrowkeys not.
Did I miss something or did I have to add some kind of extra code.
Commend: All other buttons are recognized, with the exception of the fn-button.
The background chart is moving, could it be that the chart has the focus and doesn't "forward" the input?
It depends what Controls you have placed inside your ContentControl and what control has the focus. Some keys like TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown (for a Button as an example)
You can try to use the PreviewKeyDown-Event for this case.
If I use your provided code in a minimal solution I receive the KeyDown-Events for all keys, but it is required that the ContentControl has the focus.
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
dockingContentControl.Focus();
}
private void dockingContentControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
Console.WriteLine(e.Key);
}
}
I'm creating a custom dropdown box, and I want to register when the mouse is clicked outside the dropdown box, in order to hide it. Is it possible to detect a click outside a control? or should I make some mechanism on the containing form and check for mouseclick when any dropdownbox is open?
So I finally understand that you only want it to close when the user clicks outside of it. In that case, the Leave event should work just fine... For some reason, I got the impression you wanted it to close whenever they moved the mouse outside of your custom dropdown. The Leave event is raised whenever your control loses the focus, and if the user clicks on something else, it will certainly lose focus as the thing they clicked on gains the focus.
The documentation also says that this event cascades up and down the control chain as necessary:
The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.
Overriding your UserControl's OnLeave method is the best way to handle this:
protected override void OnLeave(EventArgs e)
{
// Call the base class
base.OnLeave(e);
// When this control loses the focus, close it
this.Hide();
}
And then for testing purposes, I created a form that shows the drop-down UserControl on command:
public partial class Form1 : Form
{
private UserControl1 customDropDown;
public Form1()
{
InitializeComponent();
// Create the user control
customDropDown = new UserControl1();
// Add it to the form's Controls collection
Controls.Add(customDropDown);
customDropDown.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
// Display the user control
customDropDown.Show();
customDropDown.BringToFront(); // display in front of other controls
customDropDown.Select(); // make sure it gets the focus
}
}
Everything works perfectly with the above code, except for one thing: if the user clicks on a blank area of the form, the UserControl doesn't close. Hmm, why not? Well, because the form itself doesn't want the focus. Only controls can get the focus, and we didn't click on a control. And because nothing else stole the focus, the Leave event never got raised, meaning that the UserControl didn't know it was supposed to close itself.
If you need the UserControl to close itself when the user clicks on a blank area in the form, you need some special case handling for that. Since you say that you're only concerned about clicks, you can just handle the Click event for the form, and set the focus to a different control:
protected override void OnClick(EventArgs e)
{
// Call the base class
base.OnClick(e);
// See if our custom drop-down is visible
if (customDropDown.Visible)
{
// Set the focus to a different control on the form,
// which will force the drop-down to close
this.SelectNextControl(customDropDown, true, true, true, true);
}
}
Yes, this last part feels like a hack. The better solution, as others have mentioned, is to use the SetCapture function to instruct Windows to capture the mouse over your UserControl's window. The control's Capture property provides an even simpler way to do the same thing.
Technically, you'll need to p/invoke SetCapture() in order to receive click events that happen outside of your control.
But in your case, handling the Leave event, as #Martin suggests, should be sufficient.
EDIT: While looking for an usage example for SetCapture(), I came across the Control.Capture property, of which I was not aware. Using that property means you won't have to p/invoke anything, which is always a good thing in my book.
So, you'll have to set Capture to true when showing the dropdown, then determine if the mouse pointer lies inside the control in your click event handler and, if it doesn't, set Capture to false and close the dropdown.
UPDATE:
You can also use the Control.Focused property to determine if the control has got or lost focus when using a keyboard or mouse instead of using the Capture with the same example provided in the MSDN Capture page.
Handle the Form's MouseDown event, or override the Form's OnMouseDown
method:
enter code here
And then:
protected override void OnMouseDown(MouseEventArgs e)
{
if (!theListBox.Bounds.Contains(e.Location))
{
theListBox.Visible = false;
}
}
The Contains method old System.Drawing.Rectangle can be used to indicate if
a point is contained inside a rectangle. The Bounds property of a Control is
the outer Rectangle defined by the edges of the Control. The Location
property of the MouseEventArgs is the Point relative to the Control which
received the MouseDown event. The Bounds property of a Control in a Form is
relative to the Form.
You are probably looking for the leave event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave.aspx
Leave occurs when the input focus leaves the control.
I just wanted to share this. It is probably not a good way of doing it that way, but looks like it works for drop down panel that closes on fake "MouseLeave", I tried to hide it on Panel MouseLeave but it does not work because moving from panel to button leaves the panel because the button is not the panel itself. Probably there is better way of doing this but I am sharing this because I used about 7 hours figuring out how to get it to work. Thanks to #FTheGodfather
But it works only if the mouse moves on the form. If there is a panel this will not work.
private void click_to_show_Panel_button_MouseDown(object sender, MouseEventArgs e)
{
item_panel1.Visible = true; //Menu Panel
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!item_panel1.Bounds.Contains(e.Location))
{
item_panel1.Visible = false; // Menu panel
}
}
I've done this myself, and this is how I did it.
When the drop down is opened, register a click event on the control's parent form:
this.Form.Click += new EventHandler(CloseDropDown);
But this only takes you half the way. You probably want your drop down to close also when the current window gets deactivated. The most reliable way of detecting this has for me been through a timer that checks which window is currently active:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
and
var timer = new Timer();
timer.Interval = 100;
timer.Tick += (sender, args) =>
{
IntPtr f = GetForegroundWindow();
if (this.Form == null || f != this.Form.Handle)
{
CloseDropDown();
}
};
You should of course only let the timer run when the drop down is visible. Also, there's probably a few other events on the parent form you'd want to register when the drop down is opened:
this.Form.LocationChanged += new EventHandler(CloseDropDown);
this.Form.SizeChanged += new EventHandler(CloseDropDown);
Just don't forget to unregister all these events in the CloseDropDown method :)
EDIT:
I forgot, you should also register the Leave event on you control to see if another control gets activated/clicked:
this.Leave += new EventHandler(CloseDropDown);
I think I've got it now, this should cover all bases. Let me know if I'm missing something.
If you have Form, you can simply use Deactivate event just like this :
protected override void OnDeactivate(EventArgs e)
{
this.Dispose();
}
Let's say I have internet explorer embedded in a windows form and I then navigate to a random page on the web. How can I determine when a textbox (or any other control that can accept text input for that matter) becomes the item in focus? Basically, every time the mouse is clicked I can check to see if the item in focus is a textbox or some other control for text input and then act appropriately.
Thanks in advance,
Bob
You still haven't explained the roll of the WebBrowser but the problem seems to be tracking the Input Focus. I don't know of a Form level event but you can hook an eventhandler to the Enter or GotFocus event of all relevant Controls.
// in Form_Load
foreach (var control in this.Controls) control.Enter += OnEnterControl;
private void OnEnterControl(object sender, EventArgs e)
{
focusControl = (sender as Control);
}
I believe what you want to do is sink the HTMLTextContainerEvents2 dispinterface and respond to onFocus (and potentially onBlur).
You're right that you'll have to do the interop via pinvoke yourself. I assume you can get IHTMLElement pointers to all of the objects you want to track (by using getElementsByTagName or some such thing). Once you have that,
Query the IHTMLElemnt for IConnectionPointContainer.
Call IConnectionPointContainer::FindConnectionPoint(DIID_DHTMLTextContainerEvents2)
Call IConnectionPoint::Advise() on the IConnectionPoint obtained in step 2.
And of course you need to implement IDispatch::Invoke() which will be your callback.
Handle DISPID_HTMLELEMENTEVENTS2_ONFOCUS, etc.