English 中文(简体)
XBAP中的键盘快捷键
原标题:
  • 时间:2008-09-24 13:01:53
  •  标签:

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

问题回答

您不能禁用浏览器内置的密钥处理功能。替代浏览器自己的快捷键不是你作为浏览器内容的地方。

如果你非常想这样做,那么你可以尝试添加一个windows钩子并拦截你感兴趣的按键。

我们这样做是为了防止IE帮助打开(愿上帝怜悯我的灵魂)。

See: 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

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





相关问题
热门标签