Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Closing the browser

Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Closing the browser

Post by Hitman »

Hi all,
i have just downloaded the component yesterday and installed into my Delphi Berlin.
Thanks for bringing this alive again, as a delphi developer i do very much appreciate this.

I have notice while using the following,
- Browsing an url on form show does not show the url
- Closing the application is impossible with out using a button. I have tried to close my application by using form shortcut and the browser simply does not die.

I had the previous version of this component made by Henri Gourvest which was working like a charm but too old unfortunately :(

I will be very pleased if someone could explain,
why browser cannot navigate a page on form show?
why browser cannot be terminated by using form short cut such as VK_ESCAPE?

Thanks!
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Closing the browser

Post by salvadordf »

Hi,
Hitman wrote: Sat May 20, 2017 11:15 am why browser cannot navigate a page on form show?
By default CEF4Delphi initializes CEF to use multiple processes and threads. It makes the browser more responsive but a little more complicated to work with.

As you can see in the demos, you need to call TChromium.CreateBrowser after the form is fully initialized to initialize CEF. TForm.OnShow is a good place to do that.

When CEF is ready you'll get a TChromium.OnAfterCreated event where you can send a message to the main form to load the first web page.

Take a look at the MiniBrowser demo for an example of all this.
Hitman wrote: Sat May 20, 2017 11:15 am why browser cannot be terminated by using form short cut such as VK_ESCAPE?
Use Form.Close or send a WM_CLOSE to the main form. If you have multiple browsers inside one app you should take a look at the MDIBrowser demo.

Please, read this too :
https://www.briskbard.com/index.php?lang=en&pageid=cef

A few things have changed since DCEF3 and that web page has the first steps to make CEF4Delphi work.
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Closing the browser

Post by Hitman »

Hi,
thanks for the response.

I have done all that and checked all demos but still cannot close it.

What i want to be able to do is,
when i press "ESC" on my keyboard, the application needs to die no matter what is open and what is active.

I am using "FormCreate" to create the browser and i normally use "FormShow" to show an url.
I am creating the browser on "FormCreate" but it shows nothing if i set "chromium.loadurl" on FormShow. I have to use a timer to fire it :cry:

I have read this real carefully and done exactly as said,
https://www.briskbard.com/index.php?lang=en&pageid=cef

Now,
can you please explain step by step, what should i do to kill it when i press "ESC" on my keyboard?
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Closing the browser

Post by salvadordf »

Hitman wrote: Sat May 20, 2017 1:04 pm I have done all that and checked all demos but still cannot close it.
What i want to be able to do is,
when i press "ESC" on my keyboard, the application needs to die no matter what is open and what is active.
I am using "FormCreate" to create the browser and i normally use "FormShow" to show an url.
I am creating the browser on "FormCreate" but it shows nothing if i set "chromium.loadurl" on FormShow. I have to use a timer to fire it :cry:
Follow this steps to load a URL :
  • Call "TChromium.CreateBrowser" in the TForm.OnShow event. (Example at line 378 of uMiniBrowser.pas)
  • Send a message to your main form in the TChromium.OnAfterCreated event. (Example at line 206 of uMiniBrowser.pas)
  • Create a function to handle that message and call TChromium.LoadURL in it. (Example at line 383 of uMiniBrowser.pas)
Hitman wrote: Sat May 20, 2017 1:04 pm can you please explain step by step, what should i do to kill it when i press "ESC" on my keyboard?
I didn't check this code but this is approximately what you have to do :
  • Implement the TChromium.OnPreKeyEvent event and set isKeyboardShortcut := True when (event.kind in [KEYEVENT_KEYDOWN, KEYEVENT_KEYUP]) and (event.windows_key_code = VK_ESCAPE)
  • Implement the TChromium.OnKeyEvent event and set Result to True if (osEvent.Message is WM_KEYUP or WM_KEYDOWN) and the key is VK_ESCAPE. In the case of a WM_KEYUP you can send a WM_CLOSE message to the main form to close it after you set Result to True.
  • Implement TApplication.OnMessage to handle WM_KEYDOWN and WM_KEYUP. You must set handled to True if the message has a VK_ESCAPE key code. In the case of a WM_KEYUP you can send a WM_CLOSE message to the main form to close it after you set handled to True.
This is the info about the OnPreKeyEvent in the /include/capi/cef_keyboard_handler_capi.h file of the CEF3 sources.
///
// Called before a keyboard event is sent to the renderer. |event| contains
// information about the keyboard event. |os_event| is the operating system
// event message, if any. Return true (1) if the event was handled or false
// (0) otherwise. If the event will be handled in on_key_event() as a keyboard
// shortcut set |is_keyboard_shortcut| to true (1) and return false (0).
///
This is the info about the OnKeyEvent
///
// Called after the renderer and JavaScript in the page has had a chance to
// handle the event. |event| contains information about the keyboard event.
// |os_event| is the operating system event message, if any. Return true (1)
// if the keyboard event was handled or false (0) otherwise.
///
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Closing the browser

Post by Hitman »

Thanks!

I am sorry :oops:
I really have bad english and not sure if i understand this at all :(

"I didn't check this code but this is approximately what you have to do :
Implement the TChromium.OnPreKeyEvent event and set isKeyboardShortcut := True when (event.kind in [KEYEVENT_KEYDOWN, KEYEVENT_KEYUP]) and (event.windows_key_code = VK_ESCAPE)
Implement the TChromium.OnKeyEvent event and set Result to True if (osEvent.Message is WM_KEYUP or WM_KEYDOWN) and the key is VK_ESCAPE.
Implement TApplication.OnMessage to handle WM_KEYDOWN and WM_KEYUP. You must set handled to True if the message has a VK_ESCAPE key code. In case of a WM_KEYUP you can send a WM_CLOSE message to the main form to close it after you set handled to True.
This is the info about the OnPreKeyEvent in the /include/capi/cef_keyboard_handler_capi.h file of the CEF3 sources."

What is wrong with form shortcut?
why that can't be used and why killing this browser is so complicated?

When form sends VK_ESCAPE where you say "Exit;" and "Application.Terminate" it should die.
Sending VK_ESCAPE from the form has no effect on this browser, looks like i am not controlling it at all :(

Can you please post me a sample of how you do this shortcut thing?
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Closing the browser

Post by salvadordf »

I'll add this to the MiniBrowser as soon as I can.

A web browser is very complex and if that browser has multiple threads and processes, it's even more complicated.

The creators of Chromium and CEF3 decided to capture key events but they allow you to handle them with the OnPreKeyEvent and OnKeyEvent events.
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Closing the browser

Post by salvadordf »

Hi,

I just uploaded a new version of the MiniBrowser demo which includes a keyboard shortcut to show/hide the developer tools.

Press F12 when the demo is running outside Delphi to show or hide the developer tools. F12 is used by the delphi debugger and the shortcut doesn't work when you are debugging.

You only have to modify the Chromium1PreKeyEvent, HandleKeyUp and HandleKeyDown functions.

In all those functions you can add more shortcuts the same way VK_F12 or just replace VK_F12 by VK_ESCAPE.

In HandleKeyUp you will have to send a WM_CLOSE message to the main form to close it if the key was VK_ESCAPE.
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Closing the browser

Post by Hitman »

Hi!
thanks, i will check it but my problem is about closing the browser not showing or hiding developer tools which really doesn't have anything to do with closing/terminating the browser.

I have nothing special in the browser that i am having, simply nothing.
It is only supposed to show a page which has no flash, no video nor anything special in it (my sample is using www.google.com).
When user press a key from the keyboard, it will terminate because the reason for the key is that the browser is running full screen and has no button or border icons to close with.

It is very simple,
show a page when it starts and close when "ESC" key is pressed from the keyboard, nothing more.
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Closing the browser

Post by Hitman »

Hi,
i have just checked your new demo,
i am unfortunately not able to make anything out of it.

Do you think you could upload a simple demo?
i mean a real simple demo where you only start the browser by opening a page and close it by pressing a shortcut key.

Your mini browser demo has too much in it and for a developer who doesn't have much knowledge, it is very hard to get anything out of how to terminate it.

I do apologise for not being able to use your demo.
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Closing the browser

Post by salvadordf »

I uploaded a new demo that creates child forms and they can be closed with the escape key.

The new demo can create as many windows as you want but to close them properly it's necessary to follow the steps described in the code comments. It takes a few seconds to close each form but if you load simple html you can reduce that time.

All child windows are toolboxes but you can use normal forms.
Post Reply