0

Here is my code :

procedure PreKeyEvent(Sender: TObject; const browser: ICefBrowser;
  const event: PCefKeyEvent; osEvent: PMsg;
  out isKeyboardShortcut, Result: Boolean);
begin
    if (event.windows_key_code = VK_CONTROL) or
    (event.windows_key_code = VK_RMENU) or (event.windows_key_code = VK_LMENU) then
  begin
    event.windows_key_code := 0;
  end;
end;

The IF block works fine. But this line [event.windows_key_code := 0;] won't ignore the specified keys. How can I disable some keys using the event PreKeyEvent?

Edit : I have also tried to set Result parameter to true. still does not work.

Kermia
  • 3,963
  • 11
  • 56
  • 104
  • 1
    The [`OnPreKeyEvent`](http://magpcss.org/ceforum/apidocs3/projects/(default)/CefKeyboardHandler.html#OnPreKeyEvent) has no description, but I guess it works the same way as any other handler in the framework; to indicate that you've handled the event you return true to the result, false otherwise. – TLama Mar 22 '15 at 09:02
  • Tried that already. doesn't work. – Kermia Mar 22 '15 at 09:02
  • Well, you should say so in your question. If the rest of the framework works that way, I don't see why you chose the way of assigning 0 to the key code. Personally, as first I would try to return true to the result, if that won't work I would search the issue tracker. – TLama Mar 22 '15 at 09:06
  • @TLama However, none of these solutions work. any other suggestion? – Kermia Mar 22 '15 at 13:09
  • 1
    The `OnPreKeyEvent` event is commented in [`the source`](https://bitbucket.org/chromiumembedded/cef/src/d7f7c61ddd3428880e37459a6b0c7dd902bf9532/include/cef_keyboard_handler.h?at=master) as *"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 if the event was handled or false otherwise. If the event will be handled in OnKeyEvent() as a keyboard shortcut set |is_keyboard_shortcut| to true and return false."* – TLama Mar 22 '15 at 18:52
  • 1
    If your handler is properly attached, that is, your code executes and gets to the branch which returns `True` to the `Result` parameter, it should eat the key before it gets to the renderer. That way should work. If not, it might be a bug. However, I'm not sure which keys are handled that way. I have seen example which works with the `osEvent` parameter (there you should see the raw Windows message). But I don't have CEF nor Delphi by hand to test this right now. – TLama Mar 22 '15 at 18:54
  • 1
    P.S. my next (and last) guess is that the keys you are trying to handle are not processed this way, that the `windows_key_code` is not filled with any of these keys. Maybe you can try to inspect what's inside the `osEvent` parameter when the `WM_SYSKEYDOWN` message is sent ([`something along these lines`](http://stackoverflow.com/a/23669888/960757)). Or maybe you'll need to follow the comment about the `is_keyboard_shortcut` parameter is you're about to handle shortcuts. `` – TLama Mar 22 '15 at 18:58

1 Answers1

0

OK, As I realized it's not possible to disable the Ctrl or the Alt key separately. Instead, there is a native_key_code that presents an Integer value for any kind of pressed key on keyboard (shortcuts have different codes). You can simply print native_key_code values for yourself and see the related generated codes.

So in my case I just needed to disable Ctrl+A and Ctrl+C shortcuts :

procedure PreKeyEvent(Sender: TObject; const browser: ICefBrowser;
  const event: PCefKeyEvent; osEvent: PMsg;
  out isKeyboardShortcut, Result: Boolean);
const
  CtrlA = 1966081;
  CtrlC = 3014657;
begin
  if (event.native_key_code = CtrlA) or (event.native_key_code = CtrlC) then
  begin
    Result := true;
  end;
end;
Kermia
  • 3,963
  • 11
  • 56
  • 104