4

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

4

3 回答 3

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
于 2009-06-29T14:26:42.993 回答
1

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

于 2008-09-24T15:48:29.493 回答
0

不是答案,而是评论。在 XBAP 中禁用退格键行为会很好,没有什么比在不在元素中时按退格键更烦人的了,浏览器会将您导航到上一个网页。

于 2008-09-24T21:23:53.400 回答