void OnGUI() { if (CefInstance == null) { return; } if (CefInstance.ViewTexture == null) { return; } GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(Screen.width * .5f, Screen.height * .5f)); GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), CefInstance.ViewTexture); GUIUtility.ScaleAroundPivot(new Vector2(1f, 1f), new Vector2(Screen.width * .5f, Screen.height * .5f)); if (CaptureKeyboard) { var ev = Event.current; if (ev.type == EventType.KeyDown || ev.type == EventType.KeyUp) { int vcode = 0; if (CefKeys.KeyCodeMap.TryGetValue(ev.keyCode, out vcode)) { bool isChar = CefKeys.CharKeys.Contains(ev.keyCode); if (isChar && ev.type == EventType.KeyUp) { return; } // TODO: Refactor, we are now just checking if it's a A-Z vcode and simulating shift on capslock CefKeyboardEvent cefKeyboardEvent = new CefKeyboardEvent() { InstanceID = CefInstance.InstanceID, IsChar = isChar, IsDown = ev.type == EventType.KeyDown, Shift = Input.GetKey(KeyCode.LeftShift) || ((vcode >= 0x41 && vcode <= 0x5A) ? _capslock : false), Key = vcode }; InstanceManager.Instance.SendEvent(cefKeyboardEvent); } } } }
public void ReceiveEvent(CefEvent cefEvent) { #region Handle mouse event if (cefEvent is CefMouseEvent) { CefMouseEvent cefMouseEvent = (CefMouseEvent)cefEvent; MouseEvent mouseEvent; CefEventFlags mouseEventFlags = CefEventFlags.None; if (cefMouseEvent.MouseButton != -1) { MouseButtonType pressedButton; switch (cefMouseEvent.MouseButton) { case 0: pressedButton = MouseButtonType.Left; mouseEventFlags = CefEventFlags.LeftMouseButton; break; case 1: pressedButton = MouseButtonType.Right; mouseEventFlags = CefEventFlags.RightMouseButton; break; case 2: pressedButton = MouseButtonType.Middle; mouseEventFlags = CefEventFlags.MiddleMouseButton; break; default: pressedButton = MouseButtonType.Left; mouseEventFlags = CefEventFlags.LeftMouseButton; break; } mouseEvent = new MouseEvent(cefMouseEvent.MouseX, cefMouseEvent.MouseY, mouseEventFlags); _browser.GetBrowser().GetHost().SendMouseClickEvent(mouseEvent, pressedButton, !cefMouseEvent.MouseButtonDown, 1); } mouseEvent = new MouseEvent(cefMouseEvent.MouseX, cefMouseEvent.MouseY, mouseEventFlags); if (cefMouseEvent.ScollDeltaX != 0 || cefMouseEvent.ScollDeltaY != 0) { _browser.GetBrowser().GetHost().SendMouseWheelEvent(mouseEvent, cefMouseEvent.ScollDeltaX * 100, cefMouseEvent.ScollDeltaY * 100); } if (cefMouseEvent.MouseButton != -1) { return; } _browser.GetBrowser().GetHost().SendMouseMoveEvent(mouseEvent, false); return; } #endregion #region Handle keyboard event if (cefEvent is CefKeyboardEvent) { CefKeyboardEvent cefKeyboardEvent = (CefKeyboardEvent)cefEvent; // handle KeyEvent keyEvent = new KeyEvent() { Type = cefKeyboardEvent.IsChar ? KeyEventType.Char : (cefKeyboardEvent.IsDown ? KeyEventType.KeyDown : KeyEventType.KeyUp), WindowsKeyCode = cefKeyboardEvent.IsChar ? GetCharsFromKeys(cefKeyboardEvent.Key, cefKeyboardEvent.Shift)[0] : cefKeyboardEvent.Key, FocusOnEditableField = true, Modifiers = cefKeyboardEvent.Shift ? CefEventFlags.ShiftDown : CefEventFlags.None, IsSystemKey = false }; _browser.GetBrowser().GetHost().SendKeyEvent(keyEvent); return; } #endregion #region Handle javascript event if (cefEvent is CefEvalJavascriptEvent) { CefEvalJavascriptEvent javascriptEvent = (CefEvalJavascriptEvent)cefEvent; if (javascriptEvent.CallbackID.Length > 0) { Task <JavascriptResponse> task = _browser.EvaluateScriptAsync(javascriptEvent.Javascript); task.ContinueWith(result => SendCallback(javascriptEvent, result.Result.Result.ToString())); return; } _browser.EvaluateScriptAsync(javascriptEvent.Javascript); return; } #endregion #region Handle static call result event if (cefEvent is CefJavascriptResultEvent) { _staticBinding.HandleEvent((CefJavascriptResultEvent)cefEvent); return; } #endregion }