Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key Modifier not being sent to Browser #321

Merged
merged 1 commit into from
Apr 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CefSharp.Core/ManagedCefBrowserAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace CefSharp
}
}

bool SendKeyEvent(int message, int wParam)
bool SendKeyEvent(int message, int wParam, CefEventFlags modifiers)
{
auto cefHost = _renderClientAdapter->TryGetCefHost();

Expand All @@ -136,6 +136,8 @@ namespace CefSharp
message == WM_SYSKEYUP ||
message == WM_SYSCHAR;

keyEvent.modifiers = (uint32)modifiers;

cefHost->SendKeyEvent(keyEvent);
return true;
}
Expand Down
33 changes: 29 additions & 4 deletions CefSharp.Wpf/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace CefSharp.Wpf
{
public class WebView : ContentControl, IRenderWebBrowser, IWpfWebBrowser
{
private static readonly Key[] KeysToSendtoBrowser = new[] { Key.Tab, Key.Home, Key.End, Key.Left, Key.Right, Key.Up, Key.Down };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could put these on separate lines, for readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jornh, you fix? 😛


private HwndSource source;
private HwndSourceHook sourceHook;
private DispatcherTimer tooltipTimer;
Expand Down Expand Up @@ -443,7 +445,7 @@ private IntPtr SourceHook(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam
return IntPtr.Zero;
}

if (managedCefBrowserAdapter.SendKeyEvent(message, wParam.ToInt32()))
if (managedCefBrowserAdapter.SendKeyEvent(message, wParam.ToInt32(), 0))
{
handled = true;
}
Expand Down Expand Up @@ -555,6 +557,28 @@ private static CefEventFlags GetModifiers(MouseEventArgs e)
return modifiers;
}

private static CefEventFlags GetModifiers(KeyEventArgs e)
{
CefEventFlags modifiers = 0;

if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift))
{
modifiers |= CefEventFlags.ShiftDown;
}

if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
{
modifiers |= CefEventFlags.AltDown;
}

if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control))
{
modifiers |= CefEventFlags.ControlDown;
}

return modifiers;
}

private void SetPopupSizeAndPositionImpl(int width, int height, int x, int y)
{
popup.Width = width;
Expand Down Expand Up @@ -628,12 +652,13 @@ private void OnPreviewKey(KeyEventArgs e)
// we have to handle these extra keys here. Hooking the Tab key like this makes the tab focusing in essence work like
// KeyboardNavigation.TabNavigation="Cycle"; you will never be able to Tab out of the web browser control.

if (e.Key == Key.Tab ||
new[] { Key.Left, Key.Right, Key.Up, Key.Down }.Contains(e.Key))
if (KeysToSendtoBrowser.Contains(e.Key))
{
var message = (int)(e.IsDown ? WM.KEYDOWN : WM.KEYUP);
var virtualKey = KeyInterop.VirtualKeyFromKey(e.Key);
managedCefBrowserAdapter.SendKeyEvent(message, virtualKey);

var modifiers = GetModifiers(e);
managedCefBrowserAdapter.SendKeyEvent(message, virtualKey, modifiers);
e.Handled = true;
}

Expand Down