Pressing a key in ContentDialog
So I have a Button in Xaml:
<Button x:Name="btnCancel"
Click="btnCancel_Click"
Content="Cancel"></Button>
and in a ContentDialog I want to press a Key to trigger that button. My Problem is that no key presses are recognized and I don´t understand why.
.cs:
public sealed partial class MyDialog : ContentDialog
{
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
this.KeyDown += onE_KeyDown;
}
//...
private void onE_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
}
}
I put a breakpoint at the onE_KeyDown
method but it never gets there.
c# uwp keydown
add a comment |
So I have a Button in Xaml:
<Button x:Name="btnCancel"
Click="btnCancel_Click"
Content="Cancel"></Button>
and in a ContentDialog I want to press a Key to trigger that button. My Problem is that no key presses are recognized and I don´t understand why.
.cs:
public sealed partial class MyDialog : ContentDialog
{
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
this.KeyDown += onE_KeyDown;
}
//...
private void onE_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
}
}
I put a breakpoint at the onE_KeyDown
method but it never gets there.
c# uwp keydown
add a comment |
So I have a Button in Xaml:
<Button x:Name="btnCancel"
Click="btnCancel_Click"
Content="Cancel"></Button>
and in a ContentDialog I want to press a Key to trigger that button. My Problem is that no key presses are recognized and I don´t understand why.
.cs:
public sealed partial class MyDialog : ContentDialog
{
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
this.KeyDown += onE_KeyDown;
}
//...
private void onE_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
}
}
I put a breakpoint at the onE_KeyDown
method but it never gets there.
c# uwp keydown
So I have a Button in Xaml:
<Button x:Name="btnCancel"
Click="btnCancel_Click"
Content="Cancel"></Button>
and in a ContentDialog I want to press a Key to trigger that button. My Problem is that no key presses are recognized and I don´t understand why.
.cs:
public sealed partial class MyDialog : ContentDialog
{
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
this.KeyDown += onE_KeyDown;
}
//...
private void onE_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
}
}
I put a breakpoint at the onE_KeyDown
method but it never gets there.
c# uwp keydown
c# uwp keydown
asked Nov 13 '18 at 7:49
axbeitaxbeit
14110
14110
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
If KeyDown event never fires, chances are that the control handles it internally. MSDN says:
Specific Windows Runtime controls may have class-based handling for the KeyDown input event. If so, the control probably has an override for the method OnKeyDown. Typically these class handlers are intended to process a subset of key presses that enable a keyboard-based user interaction with that control, and often this interaction supports a keyboard accessibility feature. If a key press is handled by class-based handling, then the key press is considered to be already handled, and the KeyDown event is not raised for handling by any user code handlers on that control for that key specifically.
If that's the case you can try overwrite OnKeyDown method to change this behaviour. Unfortunately I can't check now if that's really the problem but maybe this will help you.
add a comment |
Try to use CoreWindow.KeyDown Event instead:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
//this.KeyDown += MyDialog_KeyDown;
In addition, if you do not consider the case where the keyboard is not released for a long time, try to use KeyUp event which can be fired normally.
By the way, the Esc key of the keyboard will close the ContentDialog by default, so you need to prevent this feature first in the Closing event.
The compete code:
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
//this.KeyDown += onE_KeyDown;
this.KeyUp += MyDialog_KeyUp;
}
private void MyDialog_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
//args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
if (args.Result == ContentDialogResult.None)
{
args.Cancel = true;
}
}
add a comment |
The event you are looking for is PreviewKeyDown event. Keydown event will not get fired if that particular key is handled by system default. PreviewKeyDown fires in all cases.
Note: make sure you set e.handled = false for remaining keys
https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.previewkeydown
<ContentDialog
x:Class="App1.NewContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE"
PreviewKeyDown="ContentDialog_PreviewKeyDown"
PrimaryButtonText="Button1"
SecondaryButtonText="Button2"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<Grid>
</Grid>
</ContentDialog>
private void ContentDialog_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
btnCancel_Click(this, new RoutedEventArgs());
}
else
{
e.Handled = false;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276175%2fpressing-a-key-in-contentdialog%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If KeyDown event never fires, chances are that the control handles it internally. MSDN says:
Specific Windows Runtime controls may have class-based handling for the KeyDown input event. If so, the control probably has an override for the method OnKeyDown. Typically these class handlers are intended to process a subset of key presses that enable a keyboard-based user interaction with that control, and often this interaction supports a keyboard accessibility feature. If a key press is handled by class-based handling, then the key press is considered to be already handled, and the KeyDown event is not raised for handling by any user code handlers on that control for that key specifically.
If that's the case you can try overwrite OnKeyDown method to change this behaviour. Unfortunately I can't check now if that's really the problem but maybe this will help you.
add a comment |
If KeyDown event never fires, chances are that the control handles it internally. MSDN says:
Specific Windows Runtime controls may have class-based handling for the KeyDown input event. If so, the control probably has an override for the method OnKeyDown. Typically these class handlers are intended to process a subset of key presses that enable a keyboard-based user interaction with that control, and often this interaction supports a keyboard accessibility feature. If a key press is handled by class-based handling, then the key press is considered to be already handled, and the KeyDown event is not raised for handling by any user code handlers on that control for that key specifically.
If that's the case you can try overwrite OnKeyDown method to change this behaviour. Unfortunately I can't check now if that's really the problem but maybe this will help you.
add a comment |
If KeyDown event never fires, chances are that the control handles it internally. MSDN says:
Specific Windows Runtime controls may have class-based handling for the KeyDown input event. If so, the control probably has an override for the method OnKeyDown. Typically these class handlers are intended to process a subset of key presses that enable a keyboard-based user interaction with that control, and often this interaction supports a keyboard accessibility feature. If a key press is handled by class-based handling, then the key press is considered to be already handled, and the KeyDown event is not raised for handling by any user code handlers on that control for that key specifically.
If that's the case you can try overwrite OnKeyDown method to change this behaviour. Unfortunately I can't check now if that's really the problem but maybe this will help you.
If KeyDown event never fires, chances are that the control handles it internally. MSDN says:
Specific Windows Runtime controls may have class-based handling for the KeyDown input event. If so, the control probably has an override for the method OnKeyDown. Typically these class handlers are intended to process a subset of key presses that enable a keyboard-based user interaction with that control, and often this interaction supports a keyboard accessibility feature. If a key press is handled by class-based handling, then the key press is considered to be already handled, and the KeyDown event is not raised for handling by any user code handlers on that control for that key specifically.
If that's the case you can try overwrite OnKeyDown method to change this behaviour. Unfortunately I can't check now if that's really the problem but maybe this will help you.
answered Nov 14 '18 at 14:04
MailoszMailosz
6417
6417
add a comment |
add a comment |
Try to use CoreWindow.KeyDown Event instead:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
//this.KeyDown += MyDialog_KeyDown;
In addition, if you do not consider the case where the keyboard is not released for a long time, try to use KeyUp event which can be fired normally.
By the way, the Esc key of the keyboard will close the ContentDialog by default, so you need to prevent this feature first in the Closing event.
The compete code:
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
//this.KeyDown += onE_KeyDown;
this.KeyUp += MyDialog_KeyUp;
}
private void MyDialog_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
//args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
if (args.Result == ContentDialogResult.None)
{
args.Cancel = true;
}
}
add a comment |
Try to use CoreWindow.KeyDown Event instead:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
//this.KeyDown += MyDialog_KeyDown;
In addition, if you do not consider the case where the keyboard is not released for a long time, try to use KeyUp event which can be fired normally.
By the way, the Esc key of the keyboard will close the ContentDialog by default, so you need to prevent this feature first in the Closing event.
The compete code:
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
//this.KeyDown += onE_KeyDown;
this.KeyUp += MyDialog_KeyUp;
}
private void MyDialog_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
//args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
if (args.Result == ContentDialogResult.None)
{
args.Cancel = true;
}
}
add a comment |
Try to use CoreWindow.KeyDown Event instead:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
//this.KeyDown += MyDialog_KeyDown;
In addition, if you do not consider the case where the keyboard is not released for a long time, try to use KeyUp event which can be fired normally.
By the way, the Esc key of the keyboard will close the ContentDialog by default, so you need to prevent this feature first in the Closing event.
The compete code:
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
//this.KeyDown += onE_KeyDown;
this.KeyUp += MyDialog_KeyUp;
}
private void MyDialog_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
//args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
if (args.Result == ContentDialogResult.None)
{
args.Cancel = true;
}
}
Try to use CoreWindow.KeyDown Event instead:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
//this.KeyDown += MyDialog_KeyDown;
In addition, if you do not consider the case where the keyboard is not released for a long time, try to use KeyUp event which can be fired normally.
By the way, the Esc key of the keyboard will close the ContentDialog by default, so you need to prevent this feature first in the Closing event.
The compete code:
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
//this.KeyDown += onE_KeyDown;
this.KeyUp += MyDialog_KeyUp;
}
private void MyDialog_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
//args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
if (args.Result == ContentDialogResult.None)
{
args.Cancel = true;
}
}
answered Dec 10 '18 at 9:37
Stanly FanStanly Fan
11
11
add a comment |
add a comment |
The event you are looking for is PreviewKeyDown event. Keydown event will not get fired if that particular key is handled by system default. PreviewKeyDown fires in all cases.
Note: make sure you set e.handled = false for remaining keys
https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.previewkeydown
<ContentDialog
x:Class="App1.NewContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE"
PreviewKeyDown="ContentDialog_PreviewKeyDown"
PrimaryButtonText="Button1"
SecondaryButtonText="Button2"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<Grid>
</Grid>
</ContentDialog>
private void ContentDialog_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
btnCancel_Click(this, new RoutedEventArgs());
}
else
{
e.Handled = false;
}
}
add a comment |
The event you are looking for is PreviewKeyDown event. Keydown event will not get fired if that particular key is handled by system default. PreviewKeyDown fires in all cases.
Note: make sure you set e.handled = false for remaining keys
https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.previewkeydown
<ContentDialog
x:Class="App1.NewContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE"
PreviewKeyDown="ContentDialog_PreviewKeyDown"
PrimaryButtonText="Button1"
SecondaryButtonText="Button2"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<Grid>
</Grid>
</ContentDialog>
private void ContentDialog_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
btnCancel_Click(this, new RoutedEventArgs());
}
else
{
e.Handled = false;
}
}
add a comment |
The event you are looking for is PreviewKeyDown event. Keydown event will not get fired if that particular key is handled by system default. PreviewKeyDown fires in all cases.
Note: make sure you set e.handled = false for remaining keys
https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.previewkeydown
<ContentDialog
x:Class="App1.NewContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE"
PreviewKeyDown="ContentDialog_PreviewKeyDown"
PrimaryButtonText="Button1"
SecondaryButtonText="Button2"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<Grid>
</Grid>
</ContentDialog>
private void ContentDialog_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
btnCancel_Click(this, new RoutedEventArgs());
}
else
{
e.Handled = false;
}
}
The event you are looking for is PreviewKeyDown event. Keydown event will not get fired if that particular key is handled by system default. PreviewKeyDown fires in all cases.
Note: make sure you set e.handled = false for remaining keys
https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.previewkeydown
<ContentDialog
x:Class="App1.NewContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE"
PreviewKeyDown="ContentDialog_PreviewKeyDown"
PrimaryButtonText="Button1"
SecondaryButtonText="Button2"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<Grid>
</Grid>
</ContentDialog>
private void ContentDialog_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
btnCancel_Click(this, new RoutedEventArgs());
}
else
{
e.Handled = false;
}
}
answered Dec 10 '18 at 9:54
VigneshVignesh
831316
831316
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276175%2fpressing-a-key-in-contentdialog%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown