Trigger keyDown and keyUp for special keys

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.

Trigger keyDown and keyUp for special keys

Postby TMarr0503 » Wed Apr 09, 2014 4:21 pm

I am trying to forward keyboard events into an off-screen rendering application (the events come in as JavaScript codes, and whether it was keyDown, keyUp, or keyPress). The method for normal printable characters using keyPress seems to work fine:

Code: Select all
CefKeyEvent keyEvent;
keyEvent.character = charCode;
keyEvent.type = KEYEVENT_CHAR;
   
browser->GetHost()->SendKeyEvent(keyEvent);


However, triggering keyDown and keyUp for special keys (i.e. backspace, arrow keys, etc.) doesn't work as expected:

Code: Select all
int nativeCode = JavaScriptCodeToNativeCode(charCode);
CefKeyEvent keyEvent;
keyEvent.native_key_code = nativeCode;
if(state == "down") keyEvent.type = KEYEVENT_KEYDOWN;
else if(state == "up") keyEvent.type = KEYEVENT_KEYUP;
   
browser->GetHost()->SendKeyEvent(keyEvent);


When typing in a text field and press backspace, 2 characters are deleted. When playing a game that uses the arrow keys, press and hold works, but when I release, it just continues as if I still was pressing it. Even more strange, if I'm already holding down the arrow key before loading the app, then release the key, it triggers the keyDown functionality.
TMarr0503
Techie
 
Posts: 17
Joined: Tue Mar 18, 2014 9:51 pm

Re: Trigger keyDown and keyUp for special keys

Postby TMarr0503 » Thu Apr 10, 2014 11:54 am

Just wanted to add a little more information after digging into the off-screen rendering example in cefclient.
KeyUp is processed as I attempted, but keyDown is never triggered. Instead the following is executed:

Code: Select all
browser->GetHost()->HandleKeyEventBeforeTextInputClient(event);
browser->GetHost()->HandleKeyEventAfterTextInputClient(event);


However, event is not a CefKeyEvent, but rather a CefEventHandle. This seems to be OS dependent and I can't figure out how to create this object.
TMarr0503
Techie
 
Posts: 17
Joined: Tue Mar 18, 2014 9:51 pm

Re: Trigger keyDown and keyUp for special keys

Postby magreenblatt » Thu Apr 10, 2014 12:09 pm

It's not clear to me what you're trying to do. Are you trying to input special characters into an off-screen browser via SendKeyEvent? Do the special characters work when running cefclient with the --off-screen-rendering-enabled command-line flag? If so, then look at what cefclient is sending and do the same thing.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Trigger keyDown and keyUp for special keys

Postby TMarr0503 » Thu Apr 10, 2014 1:30 pm

I have an external webpage capturing user events and sending them to my CEF3 application via WebSockets. When my CEF3 application receives these messages, I want to trigger the events. This has worked well for mouse events, keyPress, and keyUp, but not for keyDown. I have looked at cefclient with off-screen-rendering-enabled, which is where I saw that it uses CefEventHandle instead of CefKeyEvent. This object is created by the OS when an actual key is pressed down (NSEvent on Mac OSX) and converted to CefEventHandle. However in my application no actual key is pressed down, therefore the OS never captures the event. I am trying to simulate this when I receive a message via WebSockets.
Also, I searched the entire cefclient project and KEYEVENT_KEYDOWN is never used. Why does this type exist if it's never used? I would expect this to imitate the JavaScript keyDown event, just like the KEYEVENT_KEYUP imitates the JavaScript keyUp event.
TMarr0503
Techie
 
Posts: 17
Joined: Tue Mar 18, 2014 9:51 pm

Re: Trigger keyDown and keyUp for special keys

Postby magreenblatt » Thu Apr 10, 2014 1:44 pm

TMarr0503 wrote:I have looked at cefclient with off-screen-rendering-enabled, which is where I saw that it uses CefEventHandle instead of CefKeyEvent. This object is created by the OS when an actual key is pressed down (NSEvent on Mac OSX) and converted to CefEventHandle.

CefBrowserHost::SendKeyEvent uses CefKeyEvent. Are you referring to the CefKeyboardHandler::OnKeyEvent callback? If so, you're correct that the CefEventHandle passed to OnKeyEvent may sometimes be null. What are you using OnKeyEvent for? What information do you need that's not represented in CefKeyEvent?

TMarr0503 wrote:Also, I searched the entire cefclient project and KEYEVENT_KEYDOWN is never used. Why does this type exist if it's never used? I would expect this to imitate the JavaScript keyDown event, just like the KEYEVENT_KEYUP imitates the JavaScript keyUp event.

This value may be passed to the CefKeyboardHandler::OnKeyEvent callback.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Trigger keyDown and keyUp for special keys

Postby TMarr0503 » Thu Apr 10, 2014 2:48 pm

Here's how I initially tried to trigger keyDown events:

Code: Select all
void KeyDown(CefRefPrt<CefBrowser> browser, unsigned short JSCode){
    int nativeCode = JavaScriptCodeToNativeCode(JSCode);
    CefKeyEvent keyEvent;
    keyEvent.native_key_code = nativeCode;
    keyEvent.type = KEYEVENT_KEYDOWN;
   
    browser->GetHost()->SendKeyEvent(keyEvent);
}


And keyUp events:

Code: Select all
void KeyUp(CefRefPrt<CefBrowser> browser, unsigned short JSCode){
    int nativeCode = JavaScriptCodeToNativeCode(JSCode);
    CefKeyEvent keyEvent;
    keyEvent.native_key_code = nativeCode;
    keyEvent.type = KEYEVENT_KEYUP;
   
    browser->GetHost()->SendKeyEvent(keyEvent);
}


However, this results in strange behavior. Exmaple: click backspace (press and relsease) then each function is triggered once, but 2 characters are deleted.
In cefclient, I noticed that there never is a SendKeyEvent when the type is KEYEVENT_KEYDOWN. Instead the keyDown function calls the following with NSEvent (rather than CefKeyEvent):

Code: Select all
browser->GetHost()->HandleKeyEventBeforeTextInputClient(event);
browser->GetHost()->HandleKeyEventAfterTextInputClient(event);


In order to mimic this behavior, I am wondering how to create a data structure for NSEvent for keyDown given a JavaScript or native key code.
TMarr0503
Techie
 
Posts: 17
Joined: Tue Mar 18, 2014 9:51 pm

Re: Trigger keyDown and keyUp for special keys

Postby magreenblatt » Thu Apr 10, 2014 3:12 pm

Ah, those methods were added for IME support. You can try the older version of the keyDown method: https://code.google.com/p/chromiumembed ... get_mac.mm. Unfortunately IME support on OS X may require the NSEvent representation, but your app might not require IME support.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Trigger keyDown and keyUp for special keys

Postby TMarr0503 » Thu Apr 10, 2014 4:07 pm

Thanks for the link to the previous version. This very closely correlates to what I have tried. Here is a demo of what happens when I type in a text input box:

  • type text: "Hello"
  • Place cursor between the two "l"s
  • click (press and release) backspace
  • 2 characters are deleted (both "e" and "l")

Here is the code that would be triggered by clicking the backspace key:

Code: Select all
CefKeyEvent keyEvent;
keyEvent.character = 0;
keyEvent.unmodified_character = 0;
keyEvent.native_key_code = 51; // native code for backspace
keyEvent.modifiers = 0;   
keyEvent.type = KEYEVENT_KEYDOWN;
   
browser->GetHost()->SendKeyEvent(keyEvent);


Code: Select all
CefKeyEvent keyEvent;
keyEvent.character = 0;
keyEvent.unmodified_character = 0;
keyEvent.native_key_code = 51; // native code for backspace
keyEvent.modifiers = 0;   
keyEvent.type = KEYEVENT_KEYUP;
   
browser->GetHost()->SendKeyEvent(keyEvent);


This is not unique to the backspace. If I click an arrow key the cursor moves two positions. If I remove either KeyUp or KeyDown events it seems to behave more as expected (at least for text entry), but does not work for games that uses special keys holding down and later releasing (http://www.spacegoo.com/wingsuit/#)
TMarr0503
Techie
 
Posts: 17
Joined: Tue Mar 18, 2014 9:51 pm

Re: Trigger keyDown and keyUp for special keys

Postby magreenblatt » Thu Apr 10, 2014 4:25 pm

How does it behave in cefclient if you use the non-IME approach with off-screen rendering?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Trigger keyDown and keyUp for special keys

Postby TMarr0503 » Thu Apr 10, 2014 4:36 pm

cefclient works as expected. I added print statements for keyEvent.character and keyEvent.unmodified_character in cefclient and noticed that they are non-zero unlike my example (i.e. for backspace they are 127). Changing this makes KEYEVENT_KEYUP and KEYEVENT_KEY_DOWN work as expected in my app!
Do you know where I can find a table of values for each key, since they aren't all 127?
TMarr0503
Techie
 
Posts: 17
Joined: Tue Mar 18, 2014 9:51 pm

Next

Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 85 guests