CEF3 and ProcessCmdKey of form

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

CEF3 and ProcessCmdKey of form

Postby amaranth » Mon May 20, 2013 2:51 pm

I've WinForms form with panel, there is CefWebBrowser control. How to make CefWebBrowser insensitive to keyboard and pass keys to form correctly and disable context menu?
http://stackoverflow.com/questions/1665 ... ey-of-form
amaranth
Techie
 
Posts: 13
Joined: Sat Nov 12, 2011 12:28 pm

Re: CEF3 and ProcessCmdKey of form

Postby fddima » Mon May 20, 2013 4:10 pm

You can intercept any key input from native windows, and pass needed keys to browser via CefBrowserHost.SendKeyEvent.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: CEF3 and ProcessCmdKey of form

Postby amaranth » Mon May 20, 2013 11:22 pm

I try to explain my question. I have .NET 4.0 WinForms application to show web pages as slides.
I have form in my class:
Code: Select all
public sealed partial class SliderManager
    {
        private SlideForm _form;
        private System.Windows.Forms.Panel _panel;
        private Xilium.CefGlue.WindowsForms.CefWebBrowser _Chrome;

        private class SlideForm : Form
        {
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                if (keyData == (Keys.Escape))
                {
                    this.BeginInvoke((MethodInvoker)delegate { this.Close(); });
                    return true;     
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

        public SliderManager()
        {
            this._form = new SlideForm();
            this._form.TopMost = true;
            this._form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this._form.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this._form.ShowInTaskbar = false;
            this._form.Size = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
            this._panel = new System.Windows.Forms.Panel();
            this._panel.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this._form.Controls.Add(this._panel);
            this._panel.Parent = this._form;
            this._panel.BackColor = System.Drawing.Color.LimeGreen;
            this._form.BackColor = System.Drawing.Color.Maroon;
            this._panel.Size = this._form.Size;
            this._Chrome = new Xilium.CefGlue.WindowsForms.CefWebBrowser();
            this._Chrome.StartUrl = "2ip.ru";
            this._Chrome.Parent = this._panel;
            this._Chrome.Dock = DockStyle.Fill;
            this._Chrome.BringToFront();
        }
       
        public void Show()
        {
            this._form.Show();
            Slide slide = new Slide();
        }

        public void SetDecreaseBounds(int leftMargin, int rightMargin, int topMargin, int bottomMargin)
        {
            this._panel.Top += topMargin;
            this._panel.Left += leftMargin;
            this._panel.Width -= 2*rightMargin;
            this._panel.Height -= 2*bottomMargin;
        }
    }

Well, I have 2 question:
1. Why ESCAPE key pressing not work? But Alt+F4 is close form.
2. And I want to disable context menu and hotkeys in browser.
P.S. I'm using CefGlue and CefGlue.WindowsForms Assemblies and copy main code from CefGlue.Client

Thank you for help.
amaranth
Techie
 
Posts: 13
Joined: Sat Nov 12, 2011 12:28 pm

Re: CEF3 and ProcessCmdKey of form

Postby fddima » Tue May 21, 2013 2:29 am

Escape key not working 'cause browser window handles input, not control or you form itself. There is no exist something like event bubbling from html in winforms.
You can try handle keys in browser with implementing CefKeyboardHandler class.
To disable context menu implement CefContextMenuHandler and in OnBeforeContextMenu clear menu model (read docs for method).
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: CEF3 and ProcessCmdKey of form

Postby amaranth » Tue May 21, 2013 3:14 am

Can you share a little example of using CefKeyboardHandler and CefContextMenuHandler ?
Thank you!
amaranth
Techie
 
Posts: 13
Joined: Sat Nov 12, 2011 12:28 pm

Re: CEF3 and ProcessCmdKey of form

Postby fddima » Tue May 21, 2013 6:52 am

amaranth wrote:Can you share a little example of using CefKeyboardHandler and CefContextMenuHandler ?
Thank you!

Implement this classes, and return implementations from CefWebClient (located in CefGlue.WindowsForms).
http://xilium.bitbucket.org/cefglue/doc ... A10D2C.htm
http://xilium.bitbucket.org/cefglue/doc ... E275B8.htm

https://bitbucket.org/fddima/janus/src/ ... at=default - sample implementation which adds 'copy link' item to menu, but you need just clear menu in OnBeforeContextMenu as it is specified in docs.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: CEF3 and ProcessCmdKey of form

Postby amaranth » Tue May 21, 2013 7:18 am

Is there a handler that lock follow link by user, such property in default WebBrowser named AllowNavigation? Thanks.
amaranth
Techie
 
Posts: 13
Joined: Sat Nov 12, 2011 12:28 pm

Re: CEF3 and ProcessCmdKey of form

Postby fddima » Tue May 21, 2013 9:27 am

amaranth wrote:Is there a handler that lock follow link by user, such property in default WebBrowser named AllowNavigation? Thanks.

OnBeforeNavigation in CefRendererHandler.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: CEF3 and ProcessCmdKey of form

Postby amaranth » Tue May 21, 2013 11:12 pm

fddima wrote:OnBeforeNavigation in CefRendererHandler.

I didn't find OnBeforeNavigation method in CefRenderHandler, but I found OnBeforeNavigation in CefRenderProcessHandler and when I'm add my handler:
Code: Select all
internal sealed class CefDeleteRenderProcessHandler: CefRenderProcessHandler
    {
        private readonly CefWebBrowser _core;

        public CefDeleteRenderProcessHandler(CefWebBrowser core)
        {
            _core = core;
        }

        protected override bool OnBeforeNavigation(CefBrowser browser, CefFrame frame, CefRequest request, CefNavigationType navigationType, bool isRedirect)
        {
            if (navigationType == CefNavigationType.LinkClicked)
            {
                return false;
            }
            else
            {
                return base.OnBeforeNavigation(browser, frame, request, navigationType, isRedirect);
            }
        }
    }

But CefClient haven't overridable method for CefRenderProcessHandler, only CefRenderHandler. Where I must look? Thanks.
amaranth
Techie
 
Posts: 13
Joined: Sat Nov 12, 2011 12:28 pm

Re: CEF3 and ProcessCmdKey of form

Postby amaranth » Wed May 22, 2013 12:37 am

FIXED. Well I've created CefRenderProcessHandler handler:
Code: Select all
internal sealed class CefDeleteRenderProcessHandler: CefRenderProcessHandler
    {
        private readonly CefWebBrowser _core;

        public CefDeleteRenderProcessHandler(CefWebBrowser core)
        {
            _core = core;
        }

        protected override bool OnBeforeNavigation(CefBrowser browser, CefFrame frame, CefRequest request, CefNavigationType navigationType, bool isRedirect)
        {
            if (navigationType == CefNavigationType.LinkClicked)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

And I added this handler to DemoApp in my application:
Code: Select all
internal sealed class DemoApp : CefApp
    {
        private readonly CefWebBrowser _core;
        private readonly CefDeleteRenderProcessHandler _delRenderProcessHandler;
        protected override void OnBeforeCommandLineProcessing(string processType, CefCommandLine commandLine)
        {
            ;
        }

        public DemoApp()
        {
            _delRenderProcessHandler = new CefDeleteRenderProcessHandler(_core);
        }

        protected override CefRenderProcessHandler GetRenderProcessHandler()
        {
            return _delRenderProcessHandler;
        }
    }

My handler is work, but I can't block user click on links. How to fix this?.
amaranth
Techie
 
Posts: 13
Joined: Sat Nov 12, 2011 12:28 pm

Next

Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 13 guests