code_junkie

Keyboard shortcuts in an XBAP

Nov 11th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. Private Shared m_handle As IntPtr
  2. Private Shared m_hook As Interop.HwndSourceHook
  3. Private Shared m_hookCreated As Boolean = False
  4.  
  5. 'Call on application start
  6. Public Shared Sub SetWindowHook(ByVal visualSource As Visual)
  7. 'Add in a Win32 hook to stop the browser app from loading
  8. If Not m_hookCreated Then
  9. m_handle = DirectCast(PresentationSource.FromVisual(visualSource), Interop.HwndSource).Handle
  10. m_hook = New Interop.HwndSourceHook(AddressOf WindowProc)
  11. Interop.HwndSource.FromHwnd(m_handle).AddHook(m_hook)
  12. m_hookCreated = True
  13. End If
  14. End Sub
  15.  
  16. 'Call on application exit
  17. Public Shared Sub RemoveWindowHook()
  18. 'Remove the win32 hook
  19. If m_hookCreated AndAlso Not m_hook Is Nothing Then
  20. If Not Interop.HwndSource.FromHwnd(m_handle) Is Nothing Then
  21. Interop.HwndSource.FromHwnd(m_handle).RemoveHook(m_hook)
  22. End If
  23. m_hook = Nothing
  24. m_handle = IntPtr.Zero
  25. End If
  26. End Sub
  27.  
  28. 'Intercept key presses
  29. 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
  30. 'Stop the OS from handling help
  31. If msg = WM_HELP Then
  32. handled = True
  33. End If
  34. Return IntPtr.Zero
  35. End Function
Add Comment
Please, Sign In to add comment