Skip to content

Commit

Permalink
added modifier keys and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pranesh239 committed Jan 13, 2020
1 parent 04f18bc commit 69c9f55
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
node_modules
__test__
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-key-capture",
"version": "0.0.1-beta.3",
"version": "0.0.1-beta.5",
"main": "dist",
"license": "MIT",
"scripts": {
Expand Down
38 changes: 22 additions & 16 deletions src/useKeyCapture/useKeyCaptureReducer.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,63 @@
import { useKeyInitialState, useKeyActionTypes } from './useKeyCaptureUtils';
import { initialState, useKeyActionTypes } from './useKeyCaptureUtils';

export default function keyReducer(state, action) {
switch (action.type) {
case useKeyActionTypes.ESCAPE_KEY: {
return {
...useKeyInitialState,
isEscape: true
...initialState,
isEscape: true,
...action.payload
};
}

case useKeyActionTypes.ENTER_KEY: {
return {
...useKeyInitialState,
isEnter: true
...initialState,
isEnter: true,
...action.payload
};
}

case useKeyActionTypes.CAPS_ALPHABET: {
return {
...useKeyInitialState,
isCaps: true
...initialState,
isCaps: true,
...action.payload
};
}

case useKeyActionTypes.SMALL_ALPHABET: {
return {
...useKeyInitialState,
isSmall: true
...initialState,
isSmall: true,
...action.payload
};
}

case useKeyActionTypes.NUMBER: {
return {
...useKeyInitialState,
isNumber: true
...initialState,
isNumber: true,
...action.payload
};
}

case useKeyActionTypes.SPACE: {
return {
...useKeyInitialState,
isSpace: true
...initialState,
isSpace: true,
...action.payload
};
}

case useKeyActionTypes.ARROWS: {
return {
...useKeyInitialState,
isArrows: true,
...initialState,
isArrow: true,
...action.payload
};
}
default:
return { ...useKeyInitialState };
return { ...initialState };
}
}
96 changes: 62 additions & 34 deletions src/useKeyCapture/useKeyCaptureUtils.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import { useCallback } from 'react';

export const useKeyInitialState = {
keyCode: null,
keyName: null,
export const initialState = {
// Pressed key
key: null,

isEscape: false,
isEnter: false,
isSpace: false,
isArrows: false,
isRightArrow: false,
isLeftArrow: false,
isUpArrow: false,
isDownArrow: false,

// Arrow keys
isArrow: false,
isArrowRight: false,
isArrowLeft: false,
isArrowUp: false,
isArrowDown: false,

isTab: false,

// Modifier keys
isWithShift: false,
isWithCtrl: false,
isWithMeta: false,
isWithAlt: false,

isNumber: false,

// Character varients
isCaps: false,
isSmall: false,

isSpecialCharacter: false
};

Expand All @@ -31,28 +43,41 @@ export const useKeyActionTypes = {
ARROWS: 'ARROWS'
};

const arrowKeysMapper = {
24: 'UpArrow',
25: 'DownArrow',
26: 'RightArrow',
27: 'LeftArrow'
const modifierKeys = {
ctrlKey: 'Ctrl',
shiftKey: 'Shift',
altKey: 'Alt',
metaKey: 'Meta'
};

const keyCodeMapper = {
13: useKeyActionTypes.ENTER_KEY,
27: useKeyActionTypes.ESCAPE_KEY,
32: useKeyActionTypes.Error
32: useKeyActionTypes.SPACE
};

const getArrowKeysPayload = keyCode => {
const getArrowKeysPayload = key => {
return {
[`is${arrowKeysMapper[keyCode]}`]: true
[`is${key}`]: true
};
};

const isCapitalLetterPressed = key => /^[A-Z]$/.test(key);
const isSmallLetterPressed = key => /^[a-z]$/.test(key);

const getModifierPayload = eventDetails => {
let modifierPayloadObj = {};
const modifierObjKeys = Object.keys(modifierKeys);

for (let key of modifierObjKeys) {
if (eventDetails[key]) {
modifierPayloadObj[`isWith${modifierKeys[key]}`] = true;
}
}

return modifierPayloadObj;
};

/**
* Returns the action type for the key pressed
* @param {KeyboardEvent} eventDetails keyboard event object
Expand All @@ -63,33 +88,41 @@ export const getAction = eventDetails => {
throw new Error('Event called with no details');
}

if (keyCodeMapper[eventDetails.keyCode]) {
return { type: useKeyActionTypes.SPACE };
if (eventDetails.key.includes('Arrow')) {
return {
type: useKeyActionTypes.ARROWS,
payload: {
...getArrowKeysPayload(eventDetails.key),
...getModifierPayload(eventDetails),
key: eventDetails.key
}
};
}

let type;

if (keyCodeMapper[eventDetails.keyCode])
return { type: keyCodeMapper[eventDetails.keyCode] };
type = keyCodeMapper[eventDetails.keyCode];

if (isCapitalLetterPressed(eventDetails.key)) {
return { type: useKeyActionTypes.CAPS_ALPHABET };
type = useKeyActionTypes.CAPS_ALPHABET;
}

if (isSmallLetterPressed(eventDetails.key)) {
return { type: useKeyActionTypes.SMALL_ALPHABET };
type = useKeyActionTypes.SMALL_ALPHABET;
}

if (eventDetails.keyCode >= 48 && eventDetails.keyCode <= 57) {
return { type: useKeyActionTypes.NUMBER };
type = useKeyActionTypes.NUMBER;
}

if (eventDetails.keyCode >= 24 && eventDetails.keyCode <= 27) {
return {
type: useKeyActionTypes.ARROWS,
payload: getArrowKeysPayload(eventDetails.keyCode)
};
if (!type) {
type = 'SOME_OTHER_KEY';
}

return { type: 'SOME_OTHER_KEY' };
return {
type,
payload: { ...getModifierPayload(eventDetails), key: eventDetails.key }
};
};

export const useEnhancedReducer = reducer =>
Expand All @@ -99,8 +132,3 @@ export const useEnhancedReducer = reducer =>
},
[reducer]
);

// {
// payload: event,
// type: getActionType(event)
// }

0 comments on commit 69c9f55

Please sign in to comment.