Respond to the back and forward buttons in WPF

I planned to add support for the Back and Forward buttons found on many keyboards to my WPF application, but I'm trying my best to get them working.

I tried using the standard KeyBinding for BrowserBack and BrowserForward, without joy. I checked the code using the ESC key to make sure that it works in principle, and that key was ok.

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

I'm starting to think that PInvoke / trapping Real Window Messages will be the only option, but would I rather avoid this if anyone has any bright ideas?

Oh, and the keys themselves definitely work, and my keyboard is connected; -)

Update . They suggest using SystemKey so that I can understand how this works:

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

And this seems to work for the keyboard button, but it does not work for the corresponding β€œflick” touch (which mimics the next and back). These movies work great in browsers, but according to my KeyUp event, all they send is β€œLeftAlt” and not much more!

** Update again **: A great comment drew 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, heals .. flicks too!

+3
source share
3 answers

, , MediaCommands, NavigationCommands, ApplicationCommands, EditingCommands ComponentCommands WPF - 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

+5

PreviewKeyUp -

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

I'm sorry to say this, but it worked well 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 makes a paste.

Is it possible that something else intercepts a keystroke?

+1
source

Source: https://habr.com/ru/post/1731521/


All Articles