Keyboard Shortcuts in XBAP

I would like to support keyboard shortcuts in my WPF XBAP application, for example Ctrl + O for "Open", etc. How to disable embedded browser keyboard shortcuts and replace them with your own?

+4
source share
3 answers

You cannot disable browser-based key processing. This is not your place as browser content to override your own browser keyboard shortcuts.

+1
source

If you desperately want to do this, you can try adding a Windows hook and intercepting keystrokes that interest you.

We had to do this to prevent IE from helping (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 is some code (torn from our app.xaml.vb) that might 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 
+1
source

Not an answer, but a comment. It would be nice to disable the behavior of the Backspace keyword in XBAP, no more annoying than pressing the backspace key and not in the element, and the browser will take you to the previous web page.

0
source

Source: https://habr.com/ru/post/1277111/


All Articles