This source file includes following definitions.
- interpretKeyEvent
- shouldInsertCharacter
#include "config.h"
#include "core/editing/EditingBehavior.h"
#include "core/events/KeyboardEvent.h"
#include "platform/KeyboardCodes.h"
#include "platform/PlatformKeyboardEvent.h"
namespace WebCore {
static const unsigned CtrlKey = 1 << 0;
static const unsigned AltKey = 1 << 1;
static const unsigned ShiftKey = 1 << 2;
static const unsigned MetaKey = 1 << 3;
#if OS(MACOSX)
static const unsigned OptionKey = AltKey;
static const unsigned CommandKey = MetaKey;
#endif
struct KeyDownEntry {
unsigned virtualKey;
unsigned modifiers;
const char* name;
};
struct KeyPressEntry {
unsigned charCode;
unsigned modifiers;
const char* name;
};
static const KeyDownEntry keyDownEntries[] = {
{ VKEY_LEFT, 0, "MoveLeft" },
{ VKEY_LEFT, ShiftKey, "MoveLeftAndModifySelection" },
#if OS(MACOSX)
{ VKEY_LEFT, OptionKey, "MoveWordLeft" },
{ VKEY_LEFT, OptionKey | ShiftKey,
"MoveWordLeftAndModifySelection" },
#else
{ VKEY_LEFT, CtrlKey, "MoveWordLeft" },
{ VKEY_LEFT, CtrlKey | ShiftKey,
"MoveWordLeftAndModifySelection" },
#endif
{ VKEY_RIGHT, 0, "MoveRight" },
{ VKEY_RIGHT, ShiftKey, "MoveRightAndModifySelection" },
#if OS(MACOSX)
{ VKEY_RIGHT, OptionKey, "MoveWordRight" },
{ VKEY_RIGHT, OptionKey | ShiftKey, "MoveWordRightAndModifySelection" },
#else
{ VKEY_RIGHT, CtrlKey, "MoveWordRight" },
{ VKEY_RIGHT, CtrlKey | ShiftKey, "MoveWordRightAndModifySelection" },
#endif
{ VKEY_UP, 0, "MoveUp" },
{ VKEY_UP, ShiftKey, "MoveUpAndModifySelection" },
{ VKEY_PRIOR, ShiftKey, "MovePageUpAndModifySelection" },
{ VKEY_DOWN, 0, "MoveDown" },
{ VKEY_DOWN, ShiftKey, "MoveDownAndModifySelection" },
{ VKEY_NEXT, ShiftKey, "MovePageDownAndModifySelection" },
#if !OS(MACOSX)
{ VKEY_UP, CtrlKey, "MoveParagraphBackward" },
{ VKEY_UP, CtrlKey | ShiftKey, "MoveParagraphBackwardAndModifySelection" },
{ VKEY_DOWN, CtrlKey, "MoveParagraphForward" },
{ VKEY_DOWN, CtrlKey | ShiftKey, "MoveParagraphForwardAndModifySelection" },
{ VKEY_PRIOR, 0, "MovePageUp" },
{ VKEY_NEXT, 0, "MovePageDown" },
#endif
{ VKEY_HOME, 0, "MoveToBeginningOfLine" },
{ VKEY_HOME, ShiftKey,
"MoveToBeginningOfLineAndModifySelection" },
#if OS(MACOSX)
{ VKEY_PRIOR, OptionKey, "MovePageUp" },
{ VKEY_NEXT, OptionKey, "MovePageDown" },
#endif
#if !OS(MACOSX)
{ VKEY_HOME, CtrlKey, "MoveToBeginningOfDocument" },
{ VKEY_HOME, CtrlKey | ShiftKey,
"MoveToBeginningOfDocumentAndModifySelection" },
#endif
{ VKEY_END, 0, "MoveToEndOfLine" },
{ VKEY_END, ShiftKey, "MoveToEndOfLineAndModifySelection" },
#if !OS(MACOSX)
{ VKEY_END, CtrlKey, "MoveToEndOfDocument" },
{ VKEY_END, CtrlKey | ShiftKey,
"MoveToEndOfDocumentAndModifySelection" },
#endif
{ VKEY_BACK, 0, "DeleteBackward" },
{ VKEY_BACK, ShiftKey, "DeleteBackward" },
{ VKEY_DELETE, 0, "DeleteForward" },
#if OS(MACOSX)
{ VKEY_BACK, OptionKey, "DeleteWordBackward" },
{ VKEY_DELETE, OptionKey, "DeleteWordForward" },
#else
{ VKEY_BACK, CtrlKey, "DeleteWordBackward" },
{ VKEY_DELETE, CtrlKey, "DeleteWordForward" },
#endif
#if OS(MACOSX)
{ 'B', CommandKey, "ToggleBold" },
{ 'I', CommandKey, "ToggleItalic" },
#else
{ 'B', CtrlKey, "ToggleBold" },
{ 'I', CtrlKey, "ToggleItalic" },
#endif
{ 'U', CtrlKey, "ToggleUnderline" },
{ VKEY_ESCAPE, 0, "Cancel" },
{ VKEY_OEM_PERIOD, CtrlKey, "Cancel" },
{ VKEY_TAB, 0, "InsertTab" },
{ VKEY_TAB, ShiftKey, "InsertBacktab" },
{ VKEY_RETURN, 0, "InsertNewline" },
{ VKEY_RETURN, CtrlKey, "InsertNewline" },
{ VKEY_RETURN, AltKey, "InsertNewline" },
{ VKEY_RETURN, AltKey | ShiftKey, "InsertNewline" },
{ VKEY_RETURN, ShiftKey, "InsertLineBreak" },
{ VKEY_INSERT, CtrlKey, "Copy" },
{ VKEY_INSERT, ShiftKey, "Paste" },
{ VKEY_DELETE, ShiftKey, "Cut" },
#if !OS(MACOSX)
{ 'C', CtrlKey, "Copy" },
{ 'V', CtrlKey, "Paste" },
{ 'V', CtrlKey | ShiftKey, "PasteAndMatchStyle" },
{ 'X', CtrlKey, "Cut" },
{ 'A', CtrlKey, "SelectAll" },
{ 'Z', CtrlKey, "Undo" },
{ 'Z', CtrlKey | ShiftKey, "Redo" },
{ 'Y', CtrlKey, "Redo" },
#endif
{ VKEY_INSERT, 0, "OverWrite" },
};
static const KeyPressEntry keyPressEntries[] = {
{ '\t', 0, "InsertTab" },
{ '\t', ShiftKey, "InsertBacktab" },
{ '\r', 0, "InsertNewline" },
{ '\r', CtrlKey, "InsertNewline" },
{ '\r', ShiftKey, "InsertLineBreak" },
{ '\r', AltKey, "InsertNewline" },
{ '\r', AltKey | ShiftKey, "InsertNewline" },
};
const char* EditingBehavior::interpretKeyEvent(const KeyboardEvent& event) const
{
const PlatformKeyboardEvent* keyEvent = event.keyEvent();
if (!keyEvent)
return "";
static HashMap<int, const char*>* keyDownCommandsMap = 0;
static HashMap<int, const char*>* keyPressCommandsMap = 0;
if (!keyDownCommandsMap) {
keyDownCommandsMap = new HashMap<int, const char*>;
keyPressCommandsMap = new HashMap<int, const char*>;
for (unsigned i = 0; i < arraysize(keyDownEntries); i++) {
keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
}
for (unsigned i = 0; i < arraysize(keyPressEntries); i++) {
keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
}
}
unsigned modifiers = 0;
if (keyEvent->shiftKey())
modifiers |= ShiftKey;
if (keyEvent->altKey())
modifiers |= AltKey;
if (keyEvent->ctrlKey())
modifiers |= CtrlKey;
if (keyEvent->metaKey())
modifiers |= MetaKey;
if (keyEvent->type() == PlatformEvent::RawKeyDown) {
int mapKey = modifiers << 16 | event.keyCode();
return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
}
int mapKey = modifiers << 16 | event.charCode();
return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
}
bool EditingBehavior::shouldInsertCharacter(const KeyboardEvent& event) const
{
if (event.keyEvent()->text().length() != 1)
return true;
UChar ch = event.keyEvent()->text()[0U];
if (ch < ' ')
return false;
#if !OS(WIN)
if (ch < 0x80) {
if (event.keyEvent()->ctrlKey() && !event.keyEvent()->altKey())
return false;
#if OS(MACOSX)
if (event.keyEvent()->metaKey())
return false;
#endif
}
#endif
return true;
}
}