Skip to content

Commit

Permalink
added space and arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
pranesh239 committed Jan 8, 2020
1 parent c4f3e6e commit 04f18bc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
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.2",
"version": "0.0.1-beta.3",
"main": "dist",
"license": "MIT",
"scripts": {
Expand Down
9 changes: 3 additions & 6 deletions src/useKeyCapture/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import reducer from './useKeyCaptureReducer';

import {
useKeyInitialState,
getActionType,
getAction,
useKeyActionTypes,
useEnhancedReducer
} from './useKeyCaptureUtils';
Expand All @@ -15,10 +15,7 @@ function useKeys() {
);

const dispatchWithActionDetails = event => {
dispatch({
payload: event,
type: getActionType(event)
});
dispatch(getAction(event));
};

/**
Expand All @@ -31,7 +28,7 @@ function useKeys() {
};

useEffect(() => {
// example for IS_TRUSTED
// example for IS_TRUSTED as false
// setTimeout(() => {
// document.dispatchEvent(new KeyboardEvent('keydown'), { key: 'Escape' });
// }, 2000);
Expand Down
15 changes: 15 additions & 0 deletions src/useKeyCapture/useKeyCaptureReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ export default function keyReducer(state, action) {
isNumber: true
};
}

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

case useKeyActionTypes.ARROWS: {
return {
...useKeyInitialState,
isArrows: true,
...action.payload
};
}
default:
return { ...useKeyInitialState };
}
Expand Down
55 changes: 47 additions & 8 deletions src/useKeyCapture/useKeyCaptureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const useKeyInitialState = {
isEscape: false,
isEnter: false,
isSpace: false,
isArrows: false,
isRightArrow: false,
isLeftArrow: false,
isUpArrow: false,
Expand All @@ -25,37 +26,70 @@ export const useKeyActionTypes = {
RESET_CAPTURES: 'RESET_CAPTURES',
CAPS_ALPHABET: 'CAPS',
SMALL_ALPHABET: 'SMALL',
NUMBER: 'NUMBER'
NUMBER: 'NUMBER',
SPACE: 'SPACE',
ARROWS: 'ARROWS'
};

const arrowKeysMapper = {
24: 'UpArrow',
25: 'DownArrow',
26: 'RightArrow',
27: 'LeftArrow'
};

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

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

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

export const getActionType = eventDetails => {
/**
* Returns the action type for the key pressed
* @param {KeyboardEvent} eventDetails keyboard event object
* @return {String} action type
*/
export const getAction = eventDetails => {
if (!eventDetails) {
throw new Error('Event called with no details');
}

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

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

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

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

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

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

return { type: 'SOME_OTHER_KEY' };
};

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

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

0 comments on commit 04f18bc

Please sign in to comment.