Skip to content

Commit

Permalink
Expose scancodes to PP_InputEvent_Key events that are independent of …
Browse files Browse the repository at this point in the history
…the input language/layout in effect, i.e. that represent the physical key pressed, independent of its meaning in the current context.

This will inherently rely on the nativeKeyCode field of Chrome's WebKeyboardEvents.

BUG=
TEST=

Review URL: http://codereview.chromium.org/6691066

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80918 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
wez@chromium.org committed Apr 8, 2011
1 parent 76670b6 commit 06b5f9d
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 5 deletions.
21 changes: 17 additions & 4 deletions ppapi/c/pp_input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,26 @@ struct PP_InputEvent_Key {
uint32_t modifier;

/**
* The key code.
* |key_code| reflects the deprecated DOM KeyboardEvent |keyCode| field.
* Chrome populates this with the Windows-style Virtual Key code of the key.
*/

// TODO(brettw) define what these actually are.
uint32_t key_code;

/**
* |native_key_code| reflects the hardware and/or platform specific code for
* the key.
*/
uint32_t native_key_code;

/**
* |usb_key_code| contains the equivalent USB HID Page and Usage codes for
* the key, in the high- and low-order 16-bit words respectively. See
* http://www.usb.org/developers/hidpage/ for tables of HID Usage codes.
* If the no USB HID Usage equivalent is known for the key, the code is zero.
*/
uint32_t usb_key_code;
};
PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES(PP_InputEvent_Key, 8);
PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES(PP_InputEvent_Key, 16);
/**
* @}
*/
Expand Down
20 changes: 19 additions & 1 deletion ppapi/example/example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#include <time.h>

#include <algorithm>
#include <sstream>

#include "ppapi/c/dev/ppb_console_dev.h"
#include "ppapi/c/dev/ppb_cursor_control_dev.h"
#include "ppapi/c/dev/ppp_printing_dev.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_input_event.h"
#include "ppapi/c/pp_rect.h"
#include "ppapi/c/ppb_var.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/dev/scriptable_object_deprecated.h"
#include "ppapi/cpp/graphics_2d.h"
Expand All @@ -26,7 +28,6 @@
#include "ppapi/cpp/rect.h"
#include "ppapi/cpp/url_loader.h"
#include "ppapi/cpp/url_request_info.h"
#include "ppapi/cpp/var.h"

static const int kStepsPerCircle = 800;

Expand Down Expand Up @@ -192,6 +193,22 @@ class MyInstance : public pp::Instance, public MyFetcherClient {
return true;
}

void HandleKeyEvent(const PP_InputEvent_Key& key_event) {
Log(PP_LOGLEVEL_LOG, "HandleKeyDownEvent");

// Stringify the Windows-style and Native key codes
std::ostringstream last_key_down_text;
last_key_down_text << "vkey=" << key_event.key_code
<< " native=" << key_event.native_key_code
<< " usb=" << std::hex << key_event.usb_key_code;

// Locate the field to update in the page DOM
pp::Var window = GetWindowObject();
pp::Var doc = window.GetProperty("document");
pp::Var last_key_down = doc.Call("getElementById", "lastKeyDown");
last_key_down.SetProperty("innerHTML", last_key_down_text.str());
}

virtual bool HandleInputEvent(const PP_InputEvent& event) {
switch (event.type) {
case PP_INPUTEVENT_TYPE_MOUSEDOWN:
Expand All @@ -201,6 +218,7 @@ class MyInstance : public pp::Instance, public MyFetcherClient {
case PP_INPUTEVENT_TYPE_MOUSEMOVE:
return true;
case PP_INPUTEVENT_TYPE_KEYDOWN:
HandleKeyEvent(event.u.key);
return true;
default:
return false;
Expand Down
1 change: 1 addition & 0 deletions ppapi/example/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<button onclick='Test()'>Test</button>
<button onclick='ToggleSize()'>Toggle Size</button>
<div id="fps" style="background-color:white; font-weight:bold; padding:4px; width:200px;">FPS GOES HERE</div>
<div id="lastKeyDown" style="background-color:white; font-weight:bold; padding:4px; width:200px;">LAST KEY DOWN GOES HERE</div>
<div id="size" style="background-color:white; font-weight:bold; padding:4px; width:200px;"></div>
<object id="plugin" type="application/x-ppapi-example" width="400" height="400" border="2px"></object>
<hr>
Expand Down
2 changes: 2 additions & 0 deletions webkit/glue/webkit_glue.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@
'../plugins/ppapi/resource_tracker.h',
'../plugins/ppapi/string.cc',
'../plugins/ppapi/string.h',
'../plugins/ppapi/usb_code_for_event.cc',
'../plugins/ppapi/usb_code_for_event.h',
'../plugins/ppapi/var.cc',
'../plugins/ppapi/var.h',
'../plugins/sad_plugin.cc',
Expand Down
3 changes: 3 additions & 0 deletions webkit/plugins/ppapi/event_conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ppapi/c/pp_input_event.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/usb_code_for_event.h"

using WebKit::WebInputEvent;
using WebKit::WebKeyboardEvent;
Expand Down Expand Up @@ -76,6 +77,8 @@ void AppendKeyEvent(const WebInputEvent& event,
PP_InputEvent result = GetPPEventWithCommonFieldsAndType(event);
result.u.key.modifier = key_event.modifiers;
result.u.key.key_code = key_event.windowsKeyCode;
result.u.key.native_key_code = key_event.nativeKeyCode;
result.u.key.usb_key_code = UsbCodeForKeyboardEvent(key_event);
pp_events->push_back(result);
}

Expand Down
126 changes: 126 additions & 0 deletions webkit/plugins/ppapi/usb_code_for_event.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/plugins/ppapi/usb_code_for_event.h"

#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"

using WebKit::WebKeyboardEvent;

namespace webkit {
namespace ppapi {

namespace {

// TODO(wez): This table covers only USB HID Boot Protocol. It should be
// extended (e.g. with "media keys"), or derived automatically from the
// evdev USB-to-linux keycode mapping.
uint32_t linux_key_code_to_usb[256] = {
// 0x00-0x0f
0x000000, 0x070029, 0x07001e, 0x07001f,
0x070020, 0x070021, 0x070022, 0x070023,
0x070024, 0x070025, 0x070026, 0x070027,
0x07002d, 0x07002e, 0x07002a, 0x07002b,
// 0x10-0x1f
0x070014, 0x07001a, 0x070008, 0x070015,
0x070017, 0x07001c, 0x070018, 0x07000c,
0x070012, 0x070013, 0x07002f, 0x070030,
0x070028, 0x0700e0, 0x070004, 0x070016,
// 0x20-0x2f
0x070007, 0x070009, 0x07000a, 0x07000b,
0x07000d, 0x07000e, 0x07000f, 0x070033,
0x070034, 0x070035, 0x0700e1, 0x070032,
0x07001d, 0x07001b, 0x070006, 0x070019,
// 0x30-0x3f
0x070005, 0x070011, 0x070010, 0x070036,
0x070037, 0x070038, 0x0700e5, 0x070055,
0x0700e2, 0x07002c, 0x070039, 0x07003a,
0x07003b, 0x07003c, 0x07003d, 0x07003e,
// 0x40-0x4f
0x07003f, 0x070040, 0x070041, 0x070042,
0x070043, 0x070053, 0x070047, 0x07005f,
0x070060, 0x070061, 0x070056, 0x07005c,
0x07005d, 0x07005e, 0x070057, 0x070059,
// 0x50-0x5f
0x07005a, 0x07005b, 0x070062, 0x070063,
0x000000, 0x070094, 0x070064, 0x070044,
0x070045, 0x070087, 0x070092, 0x070093,
0x07008a, 0x070088, 0x07008b, 0x07008c,
// 0x60-0x6f
0x070058, 0x0700e4, 0x070054, 0x070046,
0x0700e6, 0x000000, 0x07004a, 0x070052,
0x07004b, 0x070050, 0x07004f, 0x07004d,
0x070051, 0x07004e, 0x070049, 0x07004c,
// 0x70-0x7f
0x000000, 0x0700ef, 0x0700ee, 0x0700ed,
0x070066, 0x070067, 0x000000, 0x070048,
0x000000, 0x070085, 0x070090, 0x070091,
0x070089, 0x0700e3, 0x0700e7, 0x070065,
// 0x80-0x8f
0x0700f3, 0x070079, 0x070076, 0x07007a,
0x070077, 0x07007c, 0x070074, 0x07007d,
0x0700f4, 0x07007b, 0x070075, 0x000000,
0x0700fb, 0x000000, 0x0700f8, 0x000000,
// 0x90-0x9f
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x0700f0, 0x000000,
0x0700f9, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x0700f1, 0x0700f2,
// 0xa0-0xaf
0x000000, 0x0700ec, 0x000000, 0x0700eb,
0x0700e8, 0x0700ea, 0x0700e9, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x0700fa, 0x000000, 0x000000,
// 0xb0-0xbf
0x0700f7, 0x0700f5, 0x0700f6, 0x000000,
0x000000, 0x000000, 0x000000, 0x070068,
0x070069, 0x07006a, 0x07006b, 0x07006c,
0x07006d, 0x07006e, 0x07006f, 0x070070,
// 0xc0-0xcf
0x070071, 0x070072, 0x070073, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
// 0xd0-0xdf
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
// 0xe0-0xef
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
// 0xf0-0xff
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
};

uint32_t UsbCodeForLinuxKeyboardEvent(const WebKeyboardEvent& key_event) {
if (key_event.nativeKeyCode < 0 || key_event.nativeKeyCode > 255)
return 0;
return linux_key_code_to_usb[key_event.nativeKeyCode];
}

uint32_t UsbCodeForX11EvdevKeyboardEvent(const WebKeyboardEvent& key_event) {
return UsbCodeForLinuxKeyboardEvent(key_event.nativeKeyCode - 8);
}

} // anonymous namespace

uint32_t UsbCodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
#if defined(OS_LINUX)
// TODO(wez): This code assumes that on Linux we're receiving events via
// the Xorg "evdev" driver. We should detect "XKB" or "kbd" at run-time and
// re-map accordingly, but that's not possible here, inside the sandbox.
return UsbCodeForX11EvdevKeyboardEvent(key_event);
#else
return 0;
#endif
}

} // namespace ppapi
} // namespace webkit
27 changes: 27 additions & 0 deletions webkit/plugins/ppapi/usb_code_for_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WEBKIT_PLUGINS_PPAPI_NATIVE_KEY_CODE_CONVERSION_H_
#define WEBKIT_PLUGINS_PPAPI_NATIVE_KEY_CODE_CONVERSION_H_

#include "ppapi/c/pp_stdint.h"

namespace WebKit {
class WebKeyboardEvent;
}

namespace webkit {
namespace ppapi {

// Returns a 32-bit "USB Code" for the key identifier by the supplied
// WebKeyboardEvent. The supplied event must be a KeyDown or KeyUp.
// The code consists of the USB Page (in the high-order 16-bit word) and
// USB Usage Id of the key. If no translation can be performed then zero
// is returned.
uint32_t UsbCodeForKeyboardEvent(const WebKit::WebKeyboardEvent& key_event);

} // namespace ppapi
} // namespace webkit

#endif // WEBKIT_PLUGINS_PPAPI_NATIVE_KEY_CODE_CONVERSION_H_

0 comments on commit 06b5f9d

Please sign in to comment.