2 * Copyright (C) 2009, 2010, 2011 Research In Motion Limited. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "PlatformKeyboardEvent.h"
22 #include "NotImplemented.h"
23 #include "WindowsKeyboardCodes.h"
25 #include <BlackBerryPlatformKeyboardEvent.h>
26 #include <BlackBerryPlatformLog.h>
27 #include <BlackBerryPlatformScreen.h>
28 #include <wtf/CurrentTime.h>
29 #include <wtf/text/CString.h>
33 static String keyIdentifierForBlackBerryCharacter(unsigned character)
37 case KEYCODE_KP_ENTER:
39 case KEYCODE_BACKSPACE:
50 case KEYCODE_BACK_TAB:
56 case KEYCODE_KP_RIGHT:
64 case KEYCODE_KP_DELETE:
67 case KEYCODE_LEFT_ALT:
68 case KEYCODE_RIGHT_ALT:
74 case KEYCODE_KP_INSERT:
77 case KEYCODE_KP_PG_UP:
80 case KEYCODE_KP_PG_DOWN:
110 return String::format("U+%04X", WTF::toASCIIUpper(character));
114 static int windowsKeyCodeForBlackBerryCharacter(unsigned character)
118 case KEYCODE_KP_ENTER:
119 return VK_RETURN; // (0D) Return key
120 case KEYCODE_BACKSPACE:
121 return VK_BACK; // (08) BACKSPACE key
123 return VK_DELETE; // (2E) DEL key
247 return VK_OEM_PERIOD;
273 case KEYCODE_SCROLL_LOCK:
276 case KEYCODE_BACK_TAB:
279 case KEYCODE_KP_LEFT:
282 case KEYCODE_KP_RIGHT:
288 case KEYCODE_KP_DOWN:
290 case KEYCODE_KP_DELETE:
293 case KEYCODE_LEFT_ALT:
294 case KEYCODE_RIGHT_ALT:
297 case KEYCODE_KP_HOME:
300 case KEYCODE_KP_INSERT:
303 case KEYCODE_KP_PG_UP:
305 case KEYCODE_PG_DOWN:
306 case KEYCODE_KP_PG_DOWN:
311 case KEYCODE_CAPS_LOCK:
313 case KEYCODE_LEFT_SHIFT:
314 case KEYCODE_RIGHT_SHIFT:
316 case KEYCODE_LEFT_CTRL:
317 case KEYCODE_RIGHT_CTRL:
319 case KEYCODE_NUM_LOCK:
321 case KEYCODE_KP_PLUS:
323 case KEYCODE_KP_MINUS:
325 case KEYCODE_KP_MULTIPLY:
327 case KEYCODE_KP_DIVIDE:
329 case KEYCODE_KP_FIVE:
360 unsigned adjustCharacterFromOS(unsigned character)
362 // Use windows key character as ASCII value when possible to enhance readability.
364 case KEYCODE_BACKSPACE:
366 case KEYCODE_KP_DELETE:
372 case KEYCODE_KP_ENTER:
374 case KEYCODE_KP_PLUS:
376 case KEYCODE_KP_MINUS:
378 case KEYCODE_KP_MULTIPLY:
380 case KEYCODE_KP_DIVIDE:
382 case KEYCODE_KP_FIVE:
384 case KEYCODE_KP_HOME:
388 case KEYCODE_KP_INSERT:
390 case KEYCODE_KP_PG_UP:
391 case KEYCODE_PG_DOWN:
392 case KEYCODE_KP_PG_DOWN:
394 case KEYCODE_LEFT_ALT:
395 case KEYCODE_RIGHT_ALT:
396 case KEYCODE_CAPS_LOCK:
397 case KEYCODE_LEFT_SHIFT:
398 case KEYCODE_RIGHT_SHIFT:
399 case KEYCODE_LEFT_CTRL:
400 case KEYCODE_RIGHT_CTRL:
401 case KEYCODE_NUM_LOCK:
403 case KEYCODE_SCROLL_LOCK:
428 static inline PlatformKeyboardEvent::Type toWebCorePlatformKeyboardEventType(const BlackBerry::Platform::KeyboardEvent::Type type)
431 case BlackBerry::Platform::KeyboardEvent::KeyDown:
432 return PlatformEvent::KeyDown;
433 case BlackBerry::Platform::KeyboardEvent::KeyUp:
434 return PlatformEvent::KeyUp;
435 case BlackBerry::Platform::KeyboardEvent::KeyChar:
437 return PlatformEvent::Char;
441 PlatformKeyboardEvent::PlatformKeyboardEvent(const BlackBerry::Platform::KeyboardEvent& event)
442 : PlatformEvent(toWebCorePlatformKeyboardEventType(event.type()), event.shiftActive() || (event.character() == KEYCODE_BACK_TAB), event.ctrlActive(), event.altActive(), false, currentTime())
443 , m_keyIdentifier(keyIdentifierForBlackBerryCharacter(event.character()))
444 , m_windowsVirtualKeyCode(windowsKeyCodeForBlackBerryCharacter(event.character()))
445 , m_autoRepeat(false)
447 , m_unmodifiedCharacter(event.character())
449 unsigned character = adjustCharacterFromOS(event.character());
450 UChar utf16[3] = {0};
452 UErrorCode ec = U_ZERO_ERROR;
453 u_strFromUTF32(utf16, 3, &destLength, reinterpret_cast<UChar32*>(&character), 1, &ec);
455 BBLOG(BlackBerry::Platform::LogLevelCritical, "PlatformKeyboardEvent::PlatformKeyboardEvent Error converting 0x%x to string ec (%d).", character, ec);
458 m_text = String(utf16, destLength);
459 m_unmodifiedText = m_text;
461 if (event.character() == KEYCODE_BACK_TAB)
462 m_modifiers |= ShiftKey; // BackTab should be treated as Shift + Tab.
464 BBLOG(BlackBerry::Platform::LogLevelInfo, "Keyboard event received text=%lc, keyIdentifier=%s, windowsVirtualKeyCode=%d", event.character(), m_keyIdentifier.latin1().data(), m_windowsVirtualKeyCode);
467 bool PlatformKeyboardEvent::currentCapsLockState()
473 void PlatformKeyboardEvent::disambiguateKeyDownEvent(PlatformEvent::Type type, bool backwardCompatibilityMode)
475 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
476 ASSERT(m_type == PlatformEvent::KeyDown);
479 if (backwardCompatibilityMode)
482 if (type == PlatformEvent::RawKeyDown) {
484 m_unmodifiedText = String();
486 m_keyIdentifier = String();
487 m_windowsVirtualKeyCode = 0;
491 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
493 int modifiers = BlackBerry::Platform::Graphics::Screen::primaryScreen()->getCurrentModifiersState();
494 shiftKey = modifiers & KEYMOD_SHIFT;
495 ctrlKey = modifiers & KEYMOD_CTRL;
496 altKey = modifiers & KEYMOD_ALT;
500 } // namespace WebCore