响应“返回”和“前进” WPF 中的按钮

发布于 2024-08-20 00:40:45 字数 893 浏览 3 评论 0原文

我计划向我的 WPF 应用程序添加对许多键盘上的“后退”和“前进”按钮的支持,但我正在努力让它们正常工作。

我尝试过使用标准的 KeyBinding 到 BrowserBack 和 BrowserForward,但没有任何乐趣。我使用 ESC 键测试了代码,以确保它正常工作,并且该键没有问题。

接下来我处理了 KeyUp 事件,但发送的密钥是“System”,这是无用的,如果我使用 KeyInterop.VirtualKeyFromKey 我只会返回 0。

我开始认为 PInvoke/捕获真实的窗口消息将是唯一的选择,但如果有人有任何好主意,我宁愿避免这种情况?

哦,按键本身肯定可以工作,而且我的键盘已插入;-)

更新:他们建议使用 SystemKey 让我达到了可以使用它的地步:

new KeyBinding(TestCommand, new KeyGesture(Key.Left, ModifierKeys.Alt));

这似乎有效对于键盘按钮,但它不适用于相应的触摸“轻弹”(模拟下一个和后一个)。这些轻弹在浏览器中运行良好,但根据我的 KeyUp 事件,它们发送的只是“LeftAlt”,没有其他内容!

**再次更新**:里奇的评论让我想到了这一点:

this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseBack, BrowseBack_Executed));
this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseForward, BrowseForward_Executed));

这似乎很有效……也很轻弹!

I was planning to add support for the Back and Forward buttons, found on many keyboards, to my WPF app, but I'm struggling to get them to work.

I've tried using a standard KeyBinding to BrowserBack and BrowserForward, no joy. I tested the code with the ESC key to make sure it was working in principal, and that key was fine.

Nextup I handled the KeyUp event, but the key that gets sent is "System", which is useless, and if I use KeyInterop.VirtualKeyFromKey I just get 0 returned.

I'm starting to think that PInvoke/trapping real Window Messages are going to be the only option, but I'd rather avoid that if anyone has any bright ideas?

Oh, and the keys themselves definately work, and my keyboard is plugged in ;-)

Update: They suggest to use SystemKey got me to a point that I can get it working with:

new KeyBinding(TestCommand, new KeyGesture(Key.Left, ModifierKeys.Alt));

And that seems to work for the keyboard button, but it doesn't work for the corresponding touch "flick" (which simulates next and back). Those flicks work fine in the browsers, but according to my KeyUp event all they're sending is "LeftAlt" and not much else!

** Update Again ** : Rich's comment got me to this:

this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseBack, BrowseBack_Executed));
this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseForward, BrowseForward_Executed));

Which seems to work a treat.. flicks too!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋日私语 2024-08-27 00:40:45

您引用的按钮在 WPF 中被处​​理为 MediaCommands、NavigationCommands、ApplicationCommands、EditingCommands 或 ComponentCommands - 您需要为要拦截的每个按钮添加 CommandBinding,例如:-

<Window.CommandBindings>
<CommandBinding Command="MediaCommands.PreviousTrack" 
                Executed="PreviousTrackCommandBinding_Executed"/>
<CommandBinding Command="MediaCommands.NextTrack"             
                Executed="NextTrackCommandBinding_Executed"/>

并在后面的代码中添加相关事件: -

private void PreviousTrackCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Previous track");
}
private void NextTrackCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Next track");
}

我想说在你的情况下它可能是NavigationCommands.BrowseForward和NavigationCommands.BrowseBack。查看... http://msdn.microsoft .com/en-us/library/system.windows.input.navigationcommands.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands_members.aspx

查看我的博客文章以获取更多信息等代码示例。

http://richardhopton.blogspot.com/2009/08/响应媒体演示按钮.html

The buttons you refer to are handled as MediaCommands, NavigationCommands, ApplicationCommands, EditingCommands or ComponentCommands in WPF - you'll need to add a CommandBinding for each of the buttons you want to intercept, for example:-

<Window.CommandBindings>
<CommandBinding Command="MediaCommands.PreviousTrack" 
                Executed="PreviousTrackCommandBinding_Executed"/>
<CommandBinding Command="MediaCommands.NextTrack"             
                Executed="NextTrackCommandBinding_Executed"/>

And add relevant events in code behind:-

private void PreviousTrackCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Previous track");
}
private void NextTrackCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Next track");
}

I would say in your case it's probably NavigationCommands.BrowseForward and NavigationCommands.BrowseBack. Check out... http://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands.aspx and http://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands_members.aspx

Check out my blog post for more info and more code samples.

http://richardhopton.blogspot.com/2009/08/responding-to-mediapresentation-buttons.html

奈何桥上唱咆哮 2024-08-27 00:40:45

在 PreviewKeyUp 事件中,您应该能够执行此操作 -

private void Window_PreviewKeyUp(object sender, KeyEventArgs e){
  if (e.SystemKey == Key.BrowserBack)
    // Do something ...

In PreviewKeyUp event, you should be able to do this -

private void Window_PreviewKeyUp(object sender, KeyEventArgs e){
  if (e.SystemKey == Key.BrowserBack)
    // Do something ...
仲春光 2024-08-27 00:40:45

我不想这么说,但这对我来说效果很好:

<RichTextBox>   
       <RichTextBox.InputBindings>
           <KeyBinding Key="BrowserForward" Command="Paste"/>
       </RichTextBox.InputBindings>
       <FlowDocument>
            <Paragraph>
                Some text here to cut and paste...
                            </Paragraph>
            </FlowDocument>
        </RichTextBox>

当我按键盘上的前进键时,它会进行粘贴。

是否有可能有其他东西拦截了按键?

I hate to say it but this worked fine for me:

<RichTextBox>   
       <RichTextBox.InputBindings>
           <KeyBinding Key="BrowserForward" Command="Paste"/>
       </RichTextBox.InputBindings>
       <FlowDocument>
            <Paragraph>
                Some text here to cut and paste...
                            </Paragraph>
            </FlowDocument>
        </RichTextBox>

When I press the Forward key on my keyboard it does a paste.

Is it possible something else is intercepting the key press?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文