Usually when running a UWP app on Xbox the B button on the controller is handled automatically and will return you to the previous page.
I have a page which contains a WebView, when you use the directional buttons to place the focus box around that control, the B button no longer responds. You can use the A button to take control of the WebView and display the pointer and the B button then will return focus back as above but I cannot navigate back using the B button until you move the focus box to a different control. This also happens using AdControl since this uses WebView.
I have tried to capture KeyDown:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
Debug.WriteLine(args.VirtualKey.ToString());
}
This responds with GamePadB, GamePadX etc but not when the focus box is around the WebView.
Is there anyway I can find out when the GamePad buttons (specifically B) are pressed when the focus box is around the WebView (or AdControl) and the control isn't engaged so I can manually invoke the backstack navigation?
Since this issue happens when using the XY focus mode for the app, if your OS version is 14393 or higher, one workaround for this issue is to use the mouse mode for this page which contains the webview by setting RequiresPointer="WhenFocused" as following:
<Page RequiresPointer="WhenFocused">
...
</Page>
And set another page to XY focus mode by using the following code in the app.xaml.cs:
this.RequiresPointerMode =
Windows.UI.Xaml.ApplicationRequiresPointerMode.WhenRequested;
For more information, please try to refer to the following article:
https://msdn.microsoft.com/en-us/windows/uwp/input-and-devices/designing-for-tv#mouse-mode
Related
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 .
In my windows phone app I am doing a scanning process, wherever I am in the app, when the scan button is pressed on my bottom appbar, I navigate to my scan page, known as ScanView.xaml.
Problem:
I have overridden onnavigatedTo method of scanView to initiate scanning screen.
When I press back button while I am on the scanning screen It goes back to ScanView.xaml and hence onnavigatedTo method is again called and my scanning screen again appears.
What I want to do is, when I press back button while on scanning screen it should navigate directly back to my mainpage.xaml
P.S : I have tried overriding the backbutton handler, but it is still not working.
Here is my code.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//this method is invoked as soon as we are navited to the ScanView
Scanning();
}
Thanks in advance
Just remove ScanView.xaml from the navigation stack (it is the item at the top of the stack) when navigated to the scanning page.
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.
I'm fairly new to Visual Studio in C#.
I was wondering how to pop up a message box when the user clicks anywhere within the form window.
Basically, I don't want them accessing and interacting with the program unless they have a password.
Code for the Form1() is really simple right now:
public Form1()
{
InitializeComponent();
}
The actual interface has a bunch of buttons and settings (buttons and settings I don't want the user to be able to interact with unless they have verified themselves).
regarding the 1st questions.
You usually create a second model form , that is transparent and is on top of your form.
Handle the on-click event of the transparent form .
Regarding the second questions- you should rethink your design , maybe do not show the sensitive form at all untill a user has typed the password.
By the way , you have not specified what tech do you use?
is it desktop (winforms / wpf , other)/ web (web forms, asp mvc, other) ?
recommended reading for client side windows programming.
http://msdn.microsoft.com/en-us/library/dd492132.aspx
Edit:
In order to put a form on top of another form , you use a model dialog.
http://msdn.microsoft.com/en-us/library/39wcs2dh(v=vs.110).aspx
In order to create a form transparent
http://msdn.microsoft.com/en-us/library/czke9azk(v=vs.110).aspx
Also , it still depends on your UI technology.
Provided links are form winforms, other technology may require a different approach.
Edit:
As another answer pointed out , you could also bind to the original forms click event , but you will also have to bind to every child control click event recursively.
Click on form and press F4 key that show property window then click on event button there you click on click event that show in .cs file of form .
Make function like
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("Click");
}
I am having a strange problem with the .NET TabControl in C# (Visual Studio 2010). Start a Windows Forms Application. Add a tab control and a button. Add two different labels to the two tab pages so you can differentiate them. The purpose of the button is just to act as a next button; subscribe to the its Click event with the code:
tabControl1.SelectTab(1);
Let's assume the user entered something wrong on the first tab, so when they try to go to the second tab we want to send them back, so subscribe to the tab control's SelectedIndexChanged event with the code:
if(tabControl1.SelectedIndex == 1)
{
tabControl1.SelectTab(0);
}
Now run the program and click the button. You will notice that as judged by the highlighted tab at the top, the first tab page is the one that appears to be selected, as you'd expect. However, as judged by the tab page that actually appears in the body of the tab control, it's still the second tab page that shows up! Calls to various controls' Focus(), Update(), and Refresh() functions don't seem to help. What is going on here?
I repro. This is a generic problem with event handlers, you can confuse the stuffing out the native Windows control by jerking the floor mat like that. TreeView is another control that's very prone to this kind of trouble.
There's an elegant and general solution for a problem like this, you can use Control.BeginInvoke() to delay the command. It will execute later after the native control is done with the event generation and all side-effects have been completed. Which solves this problem as well, like this:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
if (tabControl1.SelectedIndex == 1) {
this.BeginInvoke(new Action(() => tabControl1.SelectTab(0)));
}
}