Skip to content

Commit

Permalink
added special character
Browse files Browse the repository at this point in the history
  • Loading branch information
pranesh239 committed Jan 13, 2020
1 parent 69c9f55 commit 26c9bfc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 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.5",
"version": "0.0.1-beta.6",
"main": "dist",
"license": "MIT",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/useKeyCapture/useKeyCaptureReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export default function keyReducer(state, action) {
};
}

case useKeyActionTypes.SPECIAL: {
return {
...initialState,
isSpecialCharacter: true,
...action.payload
};
}

case useKeyActionTypes.ARROWS: {
return {
...initialState,
Expand Down
20 changes: 18 additions & 2 deletions src/useKeyCapture/useKeyCaptureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const initialState = {
isCaps: false,
isSmall: false,

// For special character
isSpecialCharacter: false
};

Expand All @@ -40,7 +41,8 @@ export const useKeyActionTypes = {
SMALL_ALPHABET: 'SMALL',
NUMBER: 'NUMBER',
SPACE: 'SPACE',
ARROWS: 'ARROWS'
ARROWS: 'ARROWS',
SPECIAL: 'SPECIAL'
};

const modifierKeys = {
Expand All @@ -64,6 +66,15 @@ const getArrowKeysPayload = key => {

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

const isSpecialCharacterPressed = key => {
return (
!isCapitalLetterPressed(key) &&
!isSmallLetterPressed(key) &&
!isNumberPressed(key)
);
};

const getModifierPayload = eventDetails => {
let modifierPayloadObj = {};
Expand Down Expand Up @@ -112,9 +123,14 @@ export const getAction = eventDetails => {
type = useKeyActionTypes.SMALL_ALPHABET;
}

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

if (isSpecialCharacterPressed(eventDetails.key)) {
type = useKeyActionTypes.SPECIAL;
}

if (!type) {
type = 'SOME_OTHER_KEY';
}
Expand Down

0 comments on commit 26c9bfc

Please sign in to comment.