代码之家  ›  专栏  ›  技术社区  ›  Steven Robbins

响应WPF中的“后退”和“前进”按钮

  •  4
  • Steven Robbins  · 技术社区  · 15 年前

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

    我试过用标准的键绑定浏览器和浏览器,没有乐趣。我用Esc键测试了代码,以确保它在原则上工作,并且该键是正常的。

    nextup我处理了keyup事件,但是发送的密钥是“system”,这是没用的,如果我使用keyterop.virtualkeyfromkey,我只得到0的返回。

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

    哦,钥匙本身确实能用,我的键盘也插上了;-)

    更新 :他们建议使用SystemKey,这让我明白了我可以使用它的一点:

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

    这似乎对键盘按钮有效,但对相应的触摸“flick”(模拟下一个和上一个)无效。这些笔势在浏览器中工作得很好,但是根据我的键控事件,它们发送的只是“leftalt”,其他的就不多了!

    **再次更新**:Rich的评论让我想到:

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

    这似乎是一种享受。轻弹!

    3 回复  |  直到 15 年前
        1
  •  5
  •   Richard Hopton    15 年前

    在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.aspx http://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands_members.aspx

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

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

        2
  •  1
  •   Edward    15 年前

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

    private void Window_PreviewKeyUp(object sender, KeyEventArgs e){
      if (e.SystemKey == Key.BrowserBack)
        // Do something ...
    
        3
  •  1
  •   Jack Ukleja    15 年前

    我不想这么说,但这对我很管用:

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

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

    有没有可能有什么东西在拦截按键?