XBAP中的键盘快捷键

时间:2008-09-24 13:01:53

标签: c# wpf browser keyboard-shortcuts xbap

我想在我的WPF XBAP应用程序中支持键盘快捷键,例如 Ctrl + O 用于“打开”等。如何禁用内置的浏览器键盘快捷键并用我自己的替换它们?

3 个答案:

答案 0 :(得分:1)

您无法禁用浏览器内置的键处理功能。浏览器内容不是覆盖浏览器自己的快捷键的地方。

答案 1 :(得分:1)

如果你不顾一切地想做,那么你可以尝试添加一个Windows钩子并拦截你感兴趣的击键。

我们必须这样做以防止IE帮助开启(愿上帝怜悯我的灵魂)。

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

这里有一些代码(从我们的app.xaml.vb中删除)可能会有所帮助(对不起,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

答案 2 :(得分:0)

不是答案,而是评论。在XBAP中禁用Backspace键行为会很好,没有比在元素中没有按下退格键而浏览器更令人烦恼的事情,浏览器会导航到上一个网页。

相关问题