2022-08-11 20:29:46 +00:00
|
|
|
//@ts-check
|
|
|
|
|
2022-08-11 12:38:08 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} keyToListen
|
|
|
|
* @param {()=>void} callback
|
|
|
|
*/
|
|
|
|
export const onDocumentKeyUp = (keyToListen, callback) => {
|
|
|
|
document.addEventListener(
|
|
|
|
"keyup",
|
|
|
|
({ key }) => key === keyToListen && callback()
|
|
|
|
);
|
|
|
|
};
|
2022-08-11 20:29:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} keyToListen
|
|
|
|
* @param {()=>void} callback
|
|
|
|
*/
|
|
|
|
export const onDocumentKeyDown = (keyToListen, callback) => {
|
|
|
|
document.addEventListener(
|
|
|
|
"keydown",
|
|
|
|
({ key }) => key === keyToListen && callback()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} keyToListen
|
|
|
|
* @param {(down:boolean)=>void} callback
|
|
|
|
*/
|
|
|
|
export const onDocumentKey = (keyToListen, callback) => {
|
|
|
|
document.addEventListener(
|
|
|
|
"keyup",
|
|
|
|
({ key }) => key === keyToListen && callback(false)
|
|
|
|
);
|
|
|
|
document.addEventListener(
|
|
|
|
"keydown",
|
|
|
|
({ key }) => key === keyToListen && callback(true)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default onDocumentKey;
|