views:

454

answers:

3

I would like to support keyboard shortcuts in my WPF XBAP application, such as Ctrl-O for 'Open' etc. How do I disable the browsers built-in keyboard shortcuts and replace them with my own?

+1  A: 

You can't disable the browser's built-in handling of keys. It's not your place as browser content to override the browser's own shortcut keys.

Judah Himango
A: 

Not an answer, but a comment. It would be nice to disable the Backspace key behavior in an XBAP, nothing more annoying than hitting the backspace key while not in an element and the browser navigates you to the previous web page.

Dylan
+1  A: 

If you're desperate to do it then you could try adding a windows hook and intercepting the keystrokes you're interested in.

We had to do it to prevent IE help from opening (may God have mercy on my soul).

See: http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndsource.addhook(VS.85).aspx

And here's some code (ripped out of our app.xaml.vb) which may help (sorry, VB):

Private Shared m_handle As IntPtr
Private Shared m_hook As Interop.HwndSourceHook
Private Shared m_hookCreated As Boolean = False

'Call on application start
Public Shared Sub SetWindowHook(ByVal visualSource As Visual)
    'Add in a Win32 hook to stop the browser app from loading
    If Not m_hookCreated Then
        m_handle = DirectCast(PresentationSource.FromVisual(visualSource), Interop.HwndSource).Handle
        m_hook = New Interop.HwndSourceHook(AddressOf WindowProc)
        Interop.HwndSource.FromHwnd(m_handle).AddHook(m_hook)
        m_hookCreated = True
    End If
End Sub

'Call on application exit
Public Shared Sub RemoveWindowHook()
   'Remove the win32 hook
    If m_hookCreated AndAlso Not m_hook Is Nothing Then
        If Not Interop.HwndSource.FromHwnd(m_handle) Is Nothing Then
            Interop.HwndSource.FromHwnd(m_handle).RemoveHook(m_hook)
        End If
        m_hook = Nothing
        m_handle = IntPtr.Zero
    End If
End Sub

'Intercept key presses
Private Shared Function WindowProc(ByVal hwnd As System.IntPtr, ByVal msg As Integer, ByVal wParam As System.IntPtr, ByVal lParam As System.IntPtr, ByRef handled As Boolean) As System.IntPtr
    'Stop the OS from handling help
    If msg = WM_HELP Then
        handled = True
    End If
    Return IntPtr.Zero
End Function
John Donoghue