Answered by:
KeyDown Input is Disabled after AppBar Displayed

Question
-
I added KeyDown in Grid element following codes for Keyboard input in URL
http://msdn.microsoft.com/en-us/library/windows/apps/hh868246.aspx
It worked fine when app was launched. However when I showed the AppBar or switch to Fullscreen model, the keyboard was disabled. I could not input with keyboard anymore. What is wrong?
Tuesday, July 30, 2013 7:56 PM
Answers
-
When asking for help please be clear about what your code is doing (a minimal code snippet or uploading a minimal sample project on your skydrive is ideal) and the exact repro steps. Clearly explain the actual behavior and how it is different from the desired behavior.
Taking a guess at what you are seeing: the Xaml KeyDown and KeyUp events will go to the focused control. If it doesn't handle the event the event will bubble up through its parents. If no control has the focus then nobody will receive the key events.
You can either make sure that somebody has the focus or you can get key events for the entire window by using the CoreWindow KeyDown and KeyUp events instead of the Xaml events.
--Rob
- Marked as answer by Charlie C. Li Thursday, August 1, 2013 3:59 PM
Tuesday, July 30, 2013 8:36 PMModerator
All replies
-
When asking for help please be clear about what your code is doing (a minimal code snippet or uploading a minimal sample project on your skydrive is ideal) and the exact repro steps. Clearly explain the actual behavior and how it is different from the desired behavior.
Taking a guess at what you are seeing: the Xaml KeyDown and KeyUp events will go to the focused control. If it doesn't handle the event the event will bubble up through its parents. If no control has the focus then nobody will receive the key events.
You can either make sure that somebody has the focus or you can get key events for the entire window by using the CoreWindow KeyDown and KeyUp events instead of the Xaml events.
--Rob
- Marked as answer by Charlie C. Li Thursday, August 1, 2013 3:59 PM
Tuesday, July 30, 2013 8:36 PMModerator -
I had a similar problem with the openfiledialog in WPF.
I had to use the this.Focus(); command to get the focus back to the main form.
n.Wright
- Proposed as answer by nigelwright7557 Tuesday, July 30, 2013 8:40 PM
Tuesday, July 30, 2013 8:40 PM -
The code clip is
In XAML
<common:LayoutAwarePage> <Viewbox x:Name="abc" ...> <Grid KeyDown="Grid_KeyDown" KeyUp="Grid_KeyUp"> ... </Grid> <VisualStateManager.VisualStateGroups> ... </VisaulStateManager.VisualStateGroups> </Viewbox> </common:LayoutAwarePage>
In CPP
void MainPage::OnNavigatedTo(NavigationEventArgs^ e) { ... this->Loaded+=ref new RoutedEventHandler(this,&MainPage::ProgrammaticFocus); } void MainPage::ProgrammaticFocus(Object^ sender, RoutedEventArgs^ e) { this->Focus(Windows::UI::Xaml::FocusState::Programmatic); } void MainPage::Grid_KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e) { switch (e->Key) { case VirtualKey::Control: isCtrlKeyPressed = true; break; case VirtualKey::Delete: DeleteItem(); break; } e->Handled = true; } void MainPage::Grid_KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e) { if (e->Key == VirtualKey::Control) isCtrlKeyPressed = true; else if (isCtrlKeyPressed) { if (e->Key==VirtualKey::Enter) { Fullscreen(); } } e->Handed = true; }
- Edited by Charlie C. Li Wednesday, July 31, 2013 1:26 AM
Wednesday, July 31, 2013 1:26 AM -
My testing PC is a Ultrabook with touchscreen and keyboard. In my further testing, I found that when I tap or swipe touch screen first, the keyboard will be disabled. I want to use touch screen and keyboard anytime.
Wednesday, July 31, 2013 2:44 PM -
Following Rob's suggestion, by using CoreWindow KeyDown I made the keyboard enabled.Thursday, August 1, 2013 3:58 PM