How to get out of fullscreen mode using ESC key

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to get out of fullscreen mode using ESC key

Postby fasecero » Thu Nov 19, 2015 8:19 pm

I need to enable ESCAPE key to exit fullscreen while playing a fullscreen video. Any idea?

Code: Select all
   virtual bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event, bool* is_keyboard_shortcut)  {

      if (os_event && os_event->message == WM_KEYUP) {

         if (os_event->wParam == VK_ESCAPE) {

            if (fullscreen)  // fullscreen: flag set to true inside CefDisplayHandler::OnFullscreenModeChange
            {
               // how can I set browser back to normal mode?
               // ...
            }
         }
      }

      return false;
   }
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: How to get out of fullscreen mode using ESC key

Postby magreenblatt » Thu Nov 19, 2015 9:04 pm

You should be able to exit full-screen mode via JavaScript (using ExecuteJavaScript method for example).
magreenblatt
Site Admin
 
Posts: 12404
Joined: Fri May 29, 2009 6:57 pm

Re: How to get out of fullscreen mode using ESC key

Postby fasecero » Fri Nov 20, 2015 12:31 am

Thanks for the help!

I know very little about javascript. I tried to call asynchronously the following function in the rendering process without success.

Code: Select all
bool AppProcessExitFS(CefRefPtr<CefBrowser> browser, CefRefPtr<CefProcessMessage> message)
{
   CefRefPtr<CefV8Value> ret;
   CefRefPtr<CefV8Exception> excep;

   CefRefPtr<CefFrame> FocusedFrame = browser->GetFocusedFrame();

   if (FocusedFrame)
   {
      CefRefPtr<CefV8Context> v8Context = FocusedFrame->GetV8Context();

      if (v8Context)
      {
         v8Context->Eval("alert('ESC KEY PRESSED')", ret, excep);

         v8Context->Eval("document.getElementsByTagName('video')[0].webkitExitFullScreen()", ret, excep);

         return true;
      }
   }

   return false;
}


When ESCAPE key is pressed the 'ESC KEY PRESSED' alert is displayed but the youtube video continues in fullscreen. What should be the correct js code?
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: How to get out of fullscreen mode using ESC key

Postby fasecero » Fri Nov 20, 2015 3:43 am

Solved. The text should be "document.webkitExitFullscreen()".
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: How to get out of fullscreen mode using ESC key

Postby cretz » Thu Aug 31, 2017 12:03 am

This solution does not work for me because it can be easily circumvented by a malicious actor via:

Code: Select all
document.webkitExitFullscreen = () => console.log('Stay full screen!');


Can I open an issue for programmatic fullscreen exit or is there a way to make sure a user can't circumvent the JS call?

EDIT: I suppose I can run the following in CefRenderProcessHandler::OnContextCreated:

Code: Select all
    Object.defineProperty(document, 'webkitExitFullscreen', {
      value: document.webkitExitFullscreen,
      writable: false
    });


Seems a bit hackish.
cretz
Techie
 
Posts: 34
Joined: Mon Jun 26, 2017 11:41 am


Return to Support Forum

Who is online

Users browsing this forum: delta42, Google [Bot] and 36 guests