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.

TChromium.OnPreKeyEvent forward to wonerform

Post Reply
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TChromium.OnPreKeyEvent forward to wonerform

Post by salvadordf »

Hi,

The MiniBrowser demo uses that event and TChromium.OnKeyEvent to hide and show the DevTools when the user presses F12.
https://github.com/salvadordf/CEF4Delph ... r.pas#L769

You can read all the information about the pressed key in the "event" parameter and send it to the form.

Read this page for all the information about that event :
https://magpcss.org/ceforum/apidocs3/pr ... ndler.html

if you don't receive all the key presses you can also intercept all windows messages using TChromium.RenderHandle with SetWindowLongPtr.

If your browser uses the OSR mode you can intercept all windows messages with a TApplicationEvents component.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TChromium.OnPreKeyEvent forward to wonerform

Post by salvadordf »

The CEF code comments for those events are these :

TChromium.OnPreKeyEvent

Code: Select all

  ///
  // 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).
  ///
TChromium.OnKeyEvent

Code: Select all

  ///
  // 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.
  ///
The event parameter is a pointer to this Delphi record :

Code: Select all

  TCefKeyEvent = record
    kind                    : TCefKeyEventType;  // called 'type' in the original CEF3 source code
    modifiers               : TCefEventFlags;
    windows_key_code        : Integer;
    native_key_code         : Integer;
    is_system_key           : Integer;
    character               : WideChar;
    unmodified_character    : WideChar;
    focus_on_editable_field : Integer;
  end;
...which has these CEF code comments :

Code: Select all

typedef struct _cef_key_event_t {
  ///
  // The type of keyboard event.
  ///
  cef_key_event_type_t type;

  ///
  // Bit flags describing any pressed modifier keys. See
  // cef_event_flags_t for values.
  ///
  uint32 modifiers;

  ///
  // The Windows key code for the key event. This value is used by the DOM
  // specification. Sometimes it comes directly from the event (i.e. on
  // Windows) and sometimes it's determined using a mapping function. See
  // WebCore/platform/chromium/KeyboardCodes.h for the list of values.
  ///
  int windows_key_code;

  ///
  // The actual key code genenerated by the platform.
  ///
  int native_key_code;

  ///
  // Indicates whether the event is considered a "system key" event (see
  // http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for details).
  // This value will always be false on non-Windows platforms.
  ///
  int is_system_key;

  ///
  // The character generated by the keystroke.
  ///
  char16 character;

  ///
  // Same as |character| but unmodified by any concurrently-held modifiers
  // (except shift). This is useful for working out shortcut keys.
  ///
  char16 unmodified_character;

  ///
  // True if the focus is currently on an editable field on the page. This is
  // useful for determining if standard key events should be intercepted.
  ///
  int focus_on_editable_field;
} cef_key_event_t;
The "event.modifiers" can have these bit flags :

Code: Select all

  EVENTFLAG_NONE                 = 0;
  EVENTFLAG_CAPS_LOCK_ON         = 1 shl 0;
  EVENTFLAG_SHIFT_DOWN           = 1 shl 1;
  EVENTFLAG_CONTROL_DOWN         = 1 shl 2;
  EVENTFLAG_ALT_DOWN             = 1 shl 3;
  EVENTFLAG_LEFT_MOUSE_BUTTON    = 1 shl 4;
  EVENTFLAG_MIDDLE_MOUSE_BUTTON  = 1 shl 5;
  EVENTFLAG_RIGHT_MOUSE_BUTTON   = 1 shl 6;
  EVENTFLAG_COMMAND_DOWN         = 1 shl 7;
  EVENTFLAG_NUM_LOCK_ON          = 1 shl 8;
  EVENTFLAG_IS_KEY_PAD           = 1 shl 9;
  EVENTFLAG_IS_LEFT              = 1 shl 10;
  EVENTFLAG_IS_RIGHT             = 1 shl 11;
If you just need to send a message to the form with all pressed key information then you only need to use TChromium.OnPreKeyEvent and set "Result" and "isKeyboardShortcut" to false. The "event" record has all the information you need.

However, if you need to intercept and block some key before it reaches the browser then you need to use TChromium.OnPreKeyEvent and TChromium.OnKeyEvent.

There are some rare cases where some keys or mouse buttons don't trigger those events. For example, some buttons in multimedia mice and keyboards.

In those cases you will need to use TChromium.RenderHandle with SetWindowLongPtr to intercept raw windows messages.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TChromium.OnPreKeyEvent forward to wonerform

Post by salvadordf »

Build the MiniBrowser demo and set a breakpoint inside the TMiniBrowserFrm.Chromium1PreKeyEvent procedure.

Edit the breakpoint properties to remove the tick from the "Break" checkbox inside the "Advanced" properties and set a custom log message.
Then run the demo, wait until google is loaded and then press control-x.

You will see several log entries in the "Events" log with the message you used in the breakpoint properties. Each of those entries was added for every keyboard event : "key up", "key down", etc.

I just tested this with Delphi 10.3 and control-x triggers that event correctly.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TChromium.OnPreKeyEvent forward to wonerform

Post by salvadordf »

Is the TChromium.OnPreKeyEvent event assigned at run-time after the TChromium.CreateBrowser call ?

If so, assign the event before the TChromium.CreateBrowser call.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TChromium.OnPreKeyEvent forward to wonerform

Post by salvadordf »

My fault. :oops:

Sorry I didn't explain it more clearly.

Setting "Result" and "isKeyboardShortcut" doesn't stop Chromium from capturing those key presses. It just makes Chromium to use or ignore those key presses.

You still need to send a custom windows message from TChromium.OnPreKeyEvent to your form when control-x is pressed. Use a custom message to avoid being captured by Chromium.
Post Reply