【问题标题】:Responding to "Back" and "Forward" buttons in WPF响应 WPF 中的“后退”和“前进”按钮
【发布时间】:2011-01-13 10:34:25
【问题描述】:

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

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

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

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

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

更新:他们建议使用 SystemKey 让我可以使用它:

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

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

** 再次更新 ** : Rich 的评论让我明白了这一点:

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

这似乎是一种享受……也可以轻弹!

【问题讨论】:

  • 让我知道您是如何在其他人的计算机上运行的。这有点可疑......我觉得这是一个错误
  • 对不起,是的,我已经在几台机器上试过了,它们的行为似乎都一样。

标签: wpf keyboard back key-bindings next


【解决方案1】:

您引用的按钮在 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/responding-to-mediapresentation-buttons.html

【讨论】:

  • 谢谢,但这是通常控制浏览器的后退/下一个按钮,而不是下一个/上一个曲目。不过,您的代码可以很好地与下一个/上一个曲目按钮配合使用 :-)
  • 看起来 NavigationCommands.BrowseBack 是拼图中缺失的部分!谢谢。
【解决方案2】:

在 PreviewKeyUp 事件中,你应该可以做到这一点 -

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

【讨论】:

  • 这不起作用,但它确实让我看到了它实际上在做什么,它发送 LeftAlt 后跟 Left(或 Right)。
  • 大声笑......所以它是一个狡猾的键盘。大多数 Windows 应用程序中都重新使用了该快捷方式组合...我很确定它与 Back 键不同...它是什么键盘?
  • 一个微软舒适键盘,所以我希望他们能正确地实现按键:-)
  • 从您的描述来看,我会说它 100% 确定它们没有正确实施,尽管它是 MS。您是否尝试过键盘“驱动程序”的 Windows 更新(我知道这很荒谬,但也许)?
【解决方案3】:

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

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

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

是否有其他东西在拦截按键?

【讨论】:

  • 对我不起作用。我刚刚将该代码粘贴到一个新的 WPF 项目和 nada 中。我用的是win7,不知道有没有影响?
  • 我也在 Windows 7...键正确。
  • 不管怎样,现在你知道我的代码 sn-p 对我有用,你可以满意你编写的任何代码可能只需要在你的机器上;)
  • 我在几台机器上试过了,结果一样。我让它在我的编辑中部分使用代码,但如果触摸轻弹也能工作,那就太好了。他们应该模拟相同的命令,但似乎不是:-)
猜你喜欢
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多