All files / roosterjs-content-model-dom/lib/domUtils/event eventUtils.ts

100% Statements 13/13
100% Branches 12/12
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 471x 1x 1x   1x                             1x 42x 42x 42x   42x                   1x 17x               1x 20x    
const CTRL_CHAR_CODE = 'Control';
const ALT_CHAR_CODE = 'Alt';
const META_CHAR_CODE = 'Meta';
 
const CursorMovingKeys = new Set<string>([
    'ArrowUp',
    'ArrowDown',
    'ArrowLeft',
    'ArrowRight',
    'Home',
    'End',
    'PageUp',
    'PageDown',
]);
 
/**
 * Returns true when the event was fired from a modifier key, otherwise false
 * @param event The keyboard event object
 */
export function isModifierKey(event: KeyboardEvent): boolean {
    const isCtrlKey = event.ctrlKey || event.key === CTRL_CHAR_CODE;
    const isAltKey = event.altKey || event.key === ALT_CHAR_CODE;
    const isMetaKey = event.metaKey || event.key === META_CHAR_CODE;
 
    return isCtrlKey || isAltKey || isMetaKey;
}
 
/**
 * Returns true when the event was fired from a key that produces a character value, otherwise false
 * This detection is not 100% accurate. event.key is not fully supported by all browsers, and in some browsers (e.g. IE),
 * event.key is longer than 1 for num pad input. But here we just want to improve performance as much as possible.
 * So if we missed some case here it is still acceptable.
 * @param event The keyboard event object
 */
export function isCharacterValue(event: KeyboardEvent): boolean {
    return !isModifierKey(event) && !!event.key && event.key.length == 1;
}
 
/**
 * Returns true if the given event is a cursor moving event (Left, Right, Up, Down, Home, End, Page Up, Page Down).
 * This does not check modifier keys (Ctrl, Alt, Meta). So if there are modifier keys pressed, it can still return true if one of the modifier key is pressed
 * @param event The keyboard event to check
 */
export function isCursorMovingKey(event: KeyboardEvent): boolean {
    return CursorMovingKeys.has(event.key);
}